Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-1#57 from laney0808/update-cr…
Browse files Browse the repository at this point in the history
…eate-commands

Update create command and delete command
  • Loading branch information
jovantanyk authored Mar 22, 2024
2 parents 76abd3a + 4efc421 commit 066d15b
Show file tree
Hide file tree
Showing 44 changed files with 388 additions and 285 deletions.
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.AddressBook;
import seedu.address.model.ImmuniMate;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyImmuniMate;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;
import seedu.address.model.util.SampleDataUtil;
Expand Down Expand Up @@ -75,8 +75,8 @@ public void init() throws Exception {
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.info("Using data file : " + storage.getAddressBookFilePath());

Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
Optional<ReadOnlyImmuniMate> addressBookOptional;
ReadOnlyImmuniMate initialData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
Expand All @@ -87,7 +87,7 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
initialData = new AddressBook();
initialData = new ImmuniMate();
}

return new ModelManager(initialData, userPrefs);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyImmuniMate;
import seedu.address.model.person.Person;

/**
Expand All @@ -26,9 +26,9 @@ public interface Logic {
/**
* Returns the AddressBook.
*
* @see seedu.address.model.Model#getAddressBook()
* @see seedu.address.model.Model#getImmuniMate()
*/
ReadOnlyAddressBook getAddressBook();
ReadOnlyImmuniMate getAddressBook();

/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.ImmuniMateParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyImmuniMate;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;

Expand All @@ -31,27 +31,27 @@ public class LogicManager implements Logic {

private final Model model;
private final Storage storage;
private final AddressBookParser addressBookParser;
private final ImmuniMateParser immuniMateParser;

/**
* Constructs a {@code LogicManager} with the given {@code Model} and {@code Storage}.
*/
public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
addressBookParser = new AddressBookParser();
immuniMateParser = new ImmuniMateParser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
Command command = immuniMateParser.parseCommand(commandText);
commandResult = command.execute(model);

try {
storage.saveAddressBook(model.getAddressBook());
storage.saveAddressBook(model.getImmuniMate());
} catch (AccessDeniedException e) {
throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e);
} catch (IOException ioe) {
Expand All @@ -62,8 +62,8 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return model.getAddressBook();
public ReadOnlyImmuniMate getAddressBook() {
return model.getImmuniMate();
}

@Override
Expand All @@ -73,7 +73,7 @@ public ObservableList<Person> getFilteredPersonList() {

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
return model.getImmunimateFilePath();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_NRIC_NOT_FOUND = "The NRIC provided is not found in the system";
public static final String MESSAGE_PERSON_NOT_FOUND = "The person provided was not found";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Objects.requireNonNull;

import seedu.address.model.AddressBook;
import seedu.address.model.ImmuniMate;
import seedu.address.model.Model;

/**
Expand All @@ -17,7 +17,7 @@ public class ClearCommand extends Command {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
model.setImmuniMate(new ImmuniMate());
return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ALLERGIES;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BLOODTYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONDITION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATEOFBIRTH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DIAGNOSIS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SYMPTOM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -16,37 +25,51 @@
/**
* Adds a person to the address book.
*/
public class AddCommand extends Command {
public static final String COMMAND_WORD = "add";
public class CreateCommand extends Command {
public static final String COMMAND_WORD = "create";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Creates a patient to the Immunimate System. "
+ "Parameters: "
+ PREFIX_NRIC + "NRIC "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_DATEOFBIRTH + "DATEOFBIRTH "
+ PREFIX_SEX + "SEX "
+ PREFIX_STATUS + "STATUS "
+ PREFIX_EMAIL + "EMAIL "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ PREFIX_ALLERGIES + "ALLERGIES "
+ PREFIX_BLOODTYPE + "BLOODTYPE "
+ PREFIX_CONDITION + "CONDITION "
+ PREFIX_SYMPTOM + "SYMPTOM "
+ PREFIX_DIAGNOSIS + "DIAGNOSIS "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NRIC + "S1234567A "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";
+ PREFIX_DATEOFBIRTH + "01-01-1990 "
+ PREFIX_SEX + "M "
+ PREFIX_STATUS + "PENDING "
+ PREFIX_TAG + "Long term medication "
+ PREFIX_TAG + "High blood pressure ";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
public static final String MESSAGE_SUCCESS = "New patient added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the system";

private final Person toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddCommand(Person person) {
public CreateCommand(Person person) {
requireNonNull(person);
toAdd = person;
}

//TODO test cases
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand All @@ -66,12 +89,12 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof AddCommand)) {
if (!(other instanceof CreateCommand)) {
return false;
}

AddCommand otherAddCommand = (AddCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
CreateCommand otherCreateCommand = (CreateCommand) other;
return toAdd.equals(otherCreateCommand.toAdd);
}

@Override
Expand Down
38 changes: 20 additions & 18 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.index.Index;
import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;

/**
Expand All @@ -19,28 +18,30 @@ public class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
+ ": Deletes the person identified by the NRIC.\n"
+ "Parameters: NRIC\n"
+ "Example: " + COMMAND_WORD + " S1234567B";

private final Index targetIndex;
public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Patient: %1$s";

public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
private final Nric targetNric;
//TODO test cases
public DeleteCommand(Nric targetNric) {
this.targetNric = targetNric;
}

//TODO test cases
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_PERSON_NOT_FOUND);
ObservableList<Person> persons = model.getFilteredPersonList();
if (!model.hasPerson(Person.createPersonWithNric(targetNric))) {
throw new CommandException(Messages.MESSAGE_NRIC_NOT_FOUND);
}

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
//Difference between filteredPersons.contains and model.hasPerson: first checks if the instance is in the list,
//second checks if the NRIC is in the list
Person personToDelete = persons.filtered(person -> person.getNric().equals(targetNric)).get(0);
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}
Expand All @@ -57,13 +58,14 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
return targetNric.equals(otherDeleteCommand.targetNric);
}

//TODO test cases
@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.add("targetNric", targetNric)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CreateCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.DateOfBirth;
Expand All @@ -35,22 +35,23 @@
/**
* Parses input arguments and creates a new AddCommand object
*/
public class AddCommandParser implements Parser<AddCommand> {
public class CreateCommandParser implements Parser<CreateCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
//TODO test cases
public CreateCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS,
PREFIX_DATEOFBIRTH, PREFIX_SEX, PREFIX_STATUS, PREFIX_TAG, PREFIX_EMAIL, PREFIX_COUNTRY,
PREFIX_DATEOFADMISSION, PREFIX_ALLERGIES, PREFIX_BLOODTYPE, PREFIX_CONDITION, PREFIX_SYMPTOM,
PREFIX_DIAGNOSIS);
if (!arePrefixesPresent(argMultimap, PREFIX_NRIC, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_DATEOFBIRTH,
PREFIX_SEX, PREFIX_STATUS) || !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CreateCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS,
Expand All @@ -62,7 +63,7 @@ public AddCommand parse(String args) throws ParseException {
DateOfBirth dob = ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DATEOFBIRTH).get());
Sex sex = ParserUtil.parseSex(argMultimap.getValue(PREFIX_SEX).get());
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
//TODO: add optional fields
//TODO (later): assersion to make sure optinal values don't generate errors
Person person = new Person(nric, name, phone, address, dob, sex, status);
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Expand Down Expand Up @@ -91,7 +92,7 @@ public AddCommand parse(String args) throws ParseException {
person.setDiagnosis(ParserUtil.parseDiagnosis(argMultimap.getValue(PREFIX_DIAGNOSIS).get()));
}

return new AddCommand(person);
return new CreateCommand(person);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Nric;

/**
* Parses input arguments and creates a new DeleteCommand object
Expand All @@ -18,8 +18,8 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
*/
public DeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
Nric nric = ParserUtil.parseNric(args);
return new DeleteCommand(nric);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
Expand Down
Loading

0 comments on commit 066d15b

Please sign in to comment.