diff --git a/src/main/java/seedu/address/logic/commands/AppointmentCommand.java b/src/main/java/seedu/address/logic/commands/AppointmentCommand.java index e29d32f8c87..67841e33ed2 100644 --- a/src/main/java/seedu/address/logic/commands/AppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/AppointmentCommand.java @@ -1,15 +1,17 @@ package seedu.address.logic.commands; +import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.util.List; +import java.util.Optional; -import seedu.address.commons.core.index.Index; 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.Appointment; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; /** @@ -23,43 +25,49 @@ public class AppointmentCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sets an appointment for the person identified by the index number used in the displayed person list.\n" - + "Parameters: INDEX (must be a positive integer), a/APPOINTMENT (in the format DD-MM-YYYY HH:MM)\n" + + "Parameters: NRIC, a/APPOINTMENT (in the format DD-MM-YYYY HH:MM)\n" + "Example: " + COMMAND_WORD + " 1 a/25-12-2024 14:30"; public static final String MESSAGE_SET_APPOINTMENT_SUCCESS = "Set Appointment for Person: %1$s"; public static final String MESSAGE_INVALID_APPOINTMENT_FORMAT = "Please use DD-MM-YYYY HH:MM."; public static final String MESSAGE_NO_APPOINTMENT_PROVIDED = "Please provide a valid appointment date."; - private final Index index; + //private final Index index; + private final Nric nric; private final String appointmentString; // Store appointment string to convert to LocalDateTime /** * Constructor to create an AppointmentCommand. * - * @param targetIndex Index of the person to set the appointment for. + * @param targetNric NRIC of the person to set the appointment for. * @param appointmentString String of the appointment in the format a/DD-MM-YYYY HH:MM. */ - public AppointmentCommand(Index targetIndex, String appointmentString) { - this.index = targetIndex; - this.appointmentString = appointmentString.trim(); + public AppointmentCommand(Nric targetNric, String appointmentString) { + this.nric = targetNric; + this.appointmentString = appointmentString; } @Override public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); List lastShownList = model.getFilteredPersonList(); - if (this.index.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> nric.equals(person.getNric())) + .findFirst(); + + if (personWithMatchingNric.isPresent()) { + Person personToEdit = personWithMatchingNric.get(); + Person editedPerson = new Person( + personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), + personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(), + new Appointment(this.appointmentString), personToEdit.getLogEntries()); + model.setPerson(personToEdit, editedPerson); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + return new CommandResult("Set appointment for " + personToEdit.getName() + " on " + this.appointmentString); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); } - - Person personToEdit = lastShownList.get(index.getZeroBased()); - Person editedPerson = new Person( - personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), - personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(), - new Appointment(this.appointmentString), personToEdit.getLogEntries()); - model.setPerson(personToEdit, editedPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult("Set appointment for " + personToEdit.getName() + " on " + this.appointmentString); } @Override @@ -73,14 +81,15 @@ public boolean equals(Object other) { } AppointmentCommand otherCommand = (AppointmentCommand) other; - return index.equals(otherCommand.index) + return nric.equals(otherCommand.nric) && appointmentString.equals(otherCommand.appointmentString); } @Override public String toString() { return new ToStringBuilder(this) - .add("targetIndex", index) + //.add("targetIndex", index) + .add("targetNric", nric) .add("appointmentString", appointmentString) .toString(); } diff --git a/src/main/java/seedu/address/logic/commands/DeleteCommand.java b/src/main/java/seedu/address/logic/commands/DeleteCommand.java index 91df5fbb844..ca558089211 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteCommand.java @@ -3,12 +3,13 @@ import static java.util.Objects.requireNonNull; import java.util.List; +import java.util.Optional; -import seedu.address.commons.core.index.Index; 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; /** @@ -23,15 +24,15 @@ public class DeleteCommand extends Command { 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" + + "Parameters: NRIC\n" + "Example: " + COMMAND_WORD + " 1"; public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s"; - private final Index targetIndex; + private final Nric targetNric; - public DeleteCommand(Index targetIndex) { - this.targetIndex = targetIndex; + public DeleteCommand(Nric targetNric) { + this.targetNric = targetNric; } @Override @@ -39,13 +40,17 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredPersonList(); - if (targetIndex.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); - } + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> targetNric.equals(person.getNric())) + .findFirst(); - Person personToDelete = lastShownList.get(targetIndex.getZeroBased()); - model.deletePerson(personToDelete); - return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); + if (personWithMatchingNric.isPresent()) { + Person personToDelete = personWithMatchingNric.get(); + model.deletePerson(personToDelete); + return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); + } } @Override @@ -60,13 +65,13 @@ public boolean equals(Object other) { } DeleteCommand otherDeleteCommand = (DeleteCommand) other; - return targetIndex.equals(otherDeleteCommand.targetIndex); + return targetNric.equals(otherDeleteCommand.targetNric); } @Override public String toString() { return new ToStringBuilder(this) - .add("targetIndex", targetIndex) + .add("targetNric", targetNric) .toString(); } } diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 8f72650d32e..108ff9a631e 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -17,7 +17,6 @@ import java.util.Optional; import java.util.Set; -import seedu.address.commons.core.index.Index; import seedu.address.commons.util.CollectionUtil; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -45,9 +44,9 @@ public class EditCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified " - + "by the index number used in the displayed person list. " + + "by the patient's NRIC. " + "Existing values will be overwritten by the input values.\n" - + "Parameters: INDEX (must be a positive integer) " + + "Parameters: NRIC " + "[" + PREFIX_NAME + "NAME] " + "[" + PREFIX_PHONE + "PHONE] " + "[" + PREFIX_EMAIL + "EMAIL] " @@ -55,7 +54,7 @@ public class EditCommand extends Command { + "[" + PREFIX_ADDRESS + "ADDRESS] " + "[" + PREFIX_TAG + "TAG]..." + "[" + PREFIX_APPOINTMENT + "APPOINTMENT]\n" - + "Example: " + COMMAND_WORD + " 1 " + + "Example: " + COMMAND_WORD + " S1234567A " + PREFIX_PHONE + "91234567 " + PREFIX_EMAIL + "johndoe@example.com" + PREFIX_NRIC + "S1231231D"; @@ -64,18 +63,20 @@ public class EditCommand extends Command { public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book."; - private final Index index; + //private final Index index; + private final Nric nric; private final EditPersonDescriptor editPersonDescriptor; /** - * @param index of the person in the filtered person list to edit + * @param nric of the person in the filtered person list to edit * @param editPersonDescriptor details to edit the person with */ - public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) { - requireNonNull(index); + public EditCommand(Nric nric, EditPersonDescriptor editPersonDescriptor) { + requireNonNull(nric); requireNonNull(editPersonDescriptor); - this.index = index; + //this.index = index; + this.nric = nric; this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor); } @@ -84,20 +85,24 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredPersonList(); - if (index.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); - } + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> nric.equals(person.getNric())) + .findFirst(); - Person personToEdit = lastShownList.get(index.getZeroBased()); - Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor); + if (personWithMatchingNric.isPresent()) { + Person personToEdit = personWithMatchingNric.get(); + Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor); - if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { - throw new CommandException(MESSAGE_DUPLICATE_PERSON); - } + if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { + throw new CommandException(MESSAGE_DUPLICATE_PERSON); + } - model.setPerson(personToEdit, editedPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson))); + model.setPerson(personToEdit, editedPerson); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson))); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); + } } /** @@ -133,14 +138,14 @@ public boolean equals(Object other) { } EditCommand otherEditCommand = (EditCommand) other; - return index.equals(otherEditCommand.index) + return nric.equals(otherEditCommand.nric) && editPersonDescriptor.equals(otherEditCommand.editPersonDescriptor); } @Override public String toString() { return new ToStringBuilder(this) - .add("index", index) + .add("nric", nric) .add("editPersonDescriptor", editPersonDescriptor) .toString(); } diff --git a/src/main/java/seedu/address/logic/commands/LogCommand.java b/src/main/java/seedu/address/logic/commands/LogCommand.java index ab3cac4f809..40d9fe10363 100644 --- a/src/main/java/seedu/address/logic/commands/LogCommand.java +++ b/src/main/java/seedu/address/logic/commands/LogCommand.java @@ -4,13 +4,14 @@ import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.util.List; +import java.util.Optional; -import seedu.address.commons.core.index.Index; 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.Log; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; /** @@ -19,22 +20,22 @@ public class LogCommand extends Command { public static final String COMMAND_WORD = "log"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Logs information of a patient.\n" - + "Parameters: INDEX (must be a positive integer), " + + "Parameters: NRIC, " + "TIMESTAMP (in the format DD-MM-YYYY HH:MM)," + "INFO (non-empty)\n" - + "Example: " + COMMAND_WORD + " 1 25-12-2024 14:30 Attended appointment"; + + "Example: " + COMMAND_WORD + " S1234567A 25-12-2024 14:30 Attended appointment"; - private final Index index; + private final Nric nric; private final Log log; /** * Creates a LogCommand to add the specified {@code Log} to the person at the specified {@code Index}. * - * @param targetIndex The index of the person in the filtered person list. + * @param targetNric The nric of the person in the filtered person list. * @param log The log entry to add. */ - public LogCommand(Index targetIndex, Log log) { - this.index = targetIndex; + public LogCommand(Nric targetNric, Log log) { + this.nric = targetNric; this.log = log; } @@ -51,18 +52,22 @@ public CommandResult execute(Model model) throws CommandException { List lastShownList = model.getFilteredPersonList(); - if (index.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); - } + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> nric.equals(person.getNric())) + .findFirst(); - Person personToEdit = lastShownList.get(index.getZeroBased()); - Person editedPerson = new Person( - personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), - personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(), - personToEdit.getAppointment(), personToEdit.getLogEntries().addLog(log)); - model.setPerson(personToEdit, editedPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult("Log added to " + personToEdit.getName()); + if (personWithMatchingNric.isPresent()) { + Person personToEdit = personWithMatchingNric.get(); + Person editedPerson = new Person( + personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), + personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(), + personToEdit.getAppointment(), personToEdit.getLogEntries().addLog(log)); + model.setPerson(personToEdit, editedPerson); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + return new CommandResult("Log added to " + personToEdit.getName()); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); + } } /** @@ -75,7 +80,7 @@ public CommandResult execute(Model model) throws CommandException { public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof LogCommand // instanceof handles nulls - && index.equals(((LogCommand) other).index) + && nric.equals(((LogCommand) other).nric) && log.equals(((LogCommand) other).log)); } @@ -87,7 +92,7 @@ public boolean equals(Object other) { @Override public String toString() { return new ToStringBuilder(this) - .add("index", index) + .add("nric", nric) .add("log", log) .toString(); } diff --git a/src/main/java/seedu/address/logic/commands/RemarkCommand.java b/src/main/java/seedu/address/logic/commands/RemarkCommand.java index f43dedc419c..f5f3a0d6d55 100644 --- a/src/main/java/seedu/address/logic/commands/RemarkCommand.java +++ b/src/main/java/seedu/address/logic/commands/RemarkCommand.java @@ -5,11 +5,12 @@ import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.util.List; +import java.util.Optional; -import seedu.address.commons.core.index.Index; 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; import seedu.address.model.person.Remark; @@ -22,27 +23,27 @@ public class RemarkCommand extends Command { public static final String COMMAND_WORD_SHORT = "r"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the remark of the person identified " - + "by the index number used in the last person listing. " + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the remarks section of the patient identified " + + "by the patient's NRIC. " + "Existing remark will be overwritten by the input.\n" - + "Parameters: INDEX (must be a positive integer) " + + "Parameters: NRIC " + PREFIX_REMARK + "[REMARK]\n" + "Example: " + COMMAND_WORD + " 1 " + PREFIX_REMARK + "Likes to swim."; public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added remark to Person: %1$s"; public static final String MESSAGE_DELETE_REMARK_SUCCESS = "Removed remark from Person: %1$s"; - private final Index index; + private final Nric nric; private final Remark remark; /** - * @param index of the person in the filtered person list to edit the remark + * @param nric of the person in the filtered person list to edit the remark * @param remark of the person to be updated to */ - public RemarkCommand(Index index, Remark remark) { - requireAllNonNull(index, remark); + public RemarkCommand(Nric nric, Remark remark) { + requireAllNonNull(nric, remark); - this.index = index; + this.nric = nric; this.remark = remark; } @@ -50,20 +51,24 @@ public RemarkCommand(Index index, Remark remark) { public CommandResult execute(Model model) throws CommandException { List lastShownList = model.getFilteredPersonList(); - if (index.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); - } + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> nric.equals(person.getNric())) + .findFirst(); - Person personToEdit = lastShownList.get(index.getZeroBased()); - Person editedPerson = new Person( - personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), - personToEdit.getAddress(), remark, personToEdit.getTags(), - personToEdit.getAppointment(), personToEdit.getLogEntries()); + if (personWithMatchingNric.isPresent()) { + Person personToEdit = personWithMatchingNric.get(); + Person editedPerson = new Person( + personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(), + personToEdit.getAddress(), remark, personToEdit.getTags(), personToEdit.getAppointment(), + personToEdit.getLogEntries()); - model.setPerson(personToEdit, editedPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + model.setPerson(personToEdit, editedPerson); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult(generateSuccessMessage(editedPerson)); + return new CommandResult(generateSuccessMessage(editedPerson)); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); + } } /** @@ -88,7 +93,7 @@ public boolean equals(Object other) { } RemarkCommand e = (RemarkCommand) other; - return index.equals(e.index) + return nric.equals(e.nric) && remark.equals(e.remark); } } diff --git a/src/main/java/seedu/address/logic/parser/AppointmentCommandParser.java b/src/main/java/seedu/address/logic/parser/AppointmentCommandParser.java index 0fe82a84eeb..333942be5db 100644 --- a/src/main/java/seedu/address/logic/parser/AppointmentCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AppointmentCommandParser.java @@ -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.AppointmentCommand; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Nric; /** * Parses input arguments and creates a new AppointmentCommand object. @@ -98,17 +98,12 @@ public AppointmentCommand parse(String args) throws ParseException { throw new ParseException("Hour and minute must be valid."); } - // Parse the index - String indexString = argParts[0]; - Index index; + String nricString = argParts[0]; + Nric nric; + try { - int indexValue = Integer.parseInt(indexString); - if (indexValue <= 0) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, - AppointmentCommand.MESSAGE_USAGE)); - } - index = Index.fromZeroBased(indexValue - 1); // Adjust to zero-based index - } catch (NumberFormatException e) { + nric = new Nric(nricString); + } catch (IllegalArgumentException e) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } @@ -121,6 +116,6 @@ public AppointmentCommand parse(String args) throws ParseException { } // Return the constructed AppointmentCommand - return new AppointmentCommand(index, appointmentDateTime); + return new AppointmentCommand(nric, appointmentDateTime); } } diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index 3527fe76a3e..b51ed42d7e2 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -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 @@ -18,8 +18,8 @@ public class DeleteCommandParser implements Parser { */ 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); diff --git a/src/main/java/seedu/address/logic/parser/EditCommandParser.java b/src/main/java/seedu/address/logic/parser/EditCommandParser.java index bdfb0c6ca74..0b7280bee1e 100644 --- a/src/main/java/seedu/address/logic/parser/EditCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/EditCommandParser.java @@ -15,10 +15,10 @@ import java.util.Optional; import java.util.Set; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Nric; import seedu.address.model.tag.Tag; /** @@ -37,10 +37,11 @@ public EditCommand parse(String args) throws ParseException { ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_NRIC, PREFIX_ADDRESS, PREFIX_TAG, PREFIX_APPOINTMENT); - Index index; + //Index index; + Nric nric; try { - index = ParserUtil.parseIndex(argMultimap.getPreamble()); + nric = ParserUtil.parseNric(argMultimap.getPreamble()); } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe); } @@ -75,7 +76,7 @@ public EditCommand parse(String args) throws ParseException { throw new ParseException(EditCommand.MESSAGE_NOT_EDITED); } - return new EditCommand(index, editPersonDescriptor); + return new EditCommand(nric, editPersonDescriptor); } /** diff --git a/src/main/java/seedu/address/logic/parser/LogCommandParser.java b/src/main/java/seedu/address/logic/parser/LogCommandParser.java index 8ebf166b067..6132aabe330 100644 --- a/src/main/java/seedu/address/logic/parser/LogCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/LogCommandParser.java @@ -8,11 +8,11 @@ import java.util.Arrays; import java.util.stream.Collectors; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.AppointmentCommand; import seedu.address.logic.commands.LogCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Log; +import seedu.address.model.person.Nric; /** * Parses input arguments and creates a new LogCommand object. @@ -42,18 +42,14 @@ public LogCommand parse(String args) throws ParseException { String.format(MESSAGE_INVALID_COMMAND_FORMAT, LogCommand.MESSAGE_USAGE)); } - // Parse the index - String indexString = argParts[0]; - Index index; + //Parse the nric + String nricString = argParts[0]; + Nric nric; + try { - int indexValue = Integer.parseInt(indexString); - if (indexValue <= 0) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, - AppointmentCommand.MESSAGE_USAGE)); - } - index = Index.fromZeroBased(indexValue - 1); // Adjust to zero-based index - } catch (NumberFormatException e) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, LogCommand.MESSAGE_USAGE)); + nric = new Nric(nricString); + } catch (IllegalArgumentException e) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } // Parse the date and time @@ -69,6 +65,6 @@ public LogCommand parse(String args) throws ParseException { String logString = Arrays.stream(argParts, 3, argParts.length) .collect(Collectors.joining(" ")); - return new LogCommand(index, new Log(logString, dateTime)); + return new LogCommand(nric, new Log(logString, dateTime)); } } diff --git a/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java b/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java index 9b3595ee359..d3458411225 100644 --- a/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java @@ -3,10 +3,9 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; -import seedu.address.commons.core.index.Index; -import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.logic.commands.RemarkCommand; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Nric; import seedu.address.model.person.Remark; /** @@ -21,13 +20,14 @@ public class RemarkCommandParser implements Parser { public RemarkCommand parse(String args) throws ParseException { requireNonNull(args); ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_REMARK); - Index index; + //Index index; + Nric nric; try { - index = ParserUtil.parseIndex(argMultimap.getPreamble()); - } catch (IllegalValueException ive) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE), ive); + nric = ParserUtil.parseNric(argMultimap.getPreamble()); + } catch (ParseException pe) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE), pe); } String remark = argMultimap.getValue(PREFIX_REMARK).orElse(""); - return new RemarkCommand(index, new Remark(remark)); + return new RemarkCommand(nric, new Remark(remark)); } } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index ee2ea028a32..c8e4d2b29f1 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -1,7 +1,7 @@ package seedu.address.logic; import static org.junit.jupiter.api.Assertions.assertEquals; -import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; +import static seedu.address.logic.Messages.MESSAGE_NO_PERSON_FOUND; import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND; import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_AMY; @@ -61,8 +61,8 @@ public void execute_invalidCommandFormat_throwsParseException() { @Test public void execute_commandExecutionError_throwsCommandException() { - String deleteCommand = "delete 9"; - assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + String deleteCommand = "delete A0000000A"; + assertCommandException(deleteCommand, MESSAGE_NO_PERSON_FOUND); } @Test diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index eec8b95fd2f..b1b029f06bb 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -1,6 +1,7 @@ package seedu.address.logic.commands; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; import static seedu.address.logic.parser.CliSyntax.PREFIX_APPOINTMENT; @@ -19,8 +20,10 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.AddressBook; import seedu.address.model.Model; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.model.person.predicates.NameContainsKeywordsPredicate; +import seedu.address.model.person.predicates.NricMatchesPredicate; import seedu.address.testutil.EditPersonDescriptorBuilder; /** @@ -143,4 +146,23 @@ public static void showPersonAtIndex(Model model, Index targetIndex) { assertEquals(1, model.getFilteredPersonList().size()); } + /** + * Updates {@code model}'s filtered list to show only the person with the given {@code nric} in the + * {@code model}'s address book. + */ + public static void showPersonByNric(Model model, Nric nric) { + // Find the person with the given NRIC + Person person = model.getFilteredPersonList().stream() + .filter(p -> p.getNric().equals(nric)) + .findFirst() + .orElse(null); + + assertNotNull(person); + + // Use the found person's NRIC to update the filtered list + model.updateFilteredPersonList(new NricMatchesPredicate(nric.toString())); + + assertEquals(1, model.getFilteredPersonList().size()); + } + } diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index b6f332eabca..b6b09a46d8a 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -2,21 +2,24 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; -import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; -import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.logic.commands.CommandTestUtil.showPersonByNric; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import java.util.Optional; + import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; /** @@ -28,9 +31,17 @@ public class DeleteCommandTest { private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); @Test - public void execute_validIndexUnfilteredList_success() { - Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON); + public void execute_validNricUnfilteredList_success() { + Optional personWithMatchingNric = model.getFilteredPersonList().stream() + .filter(person -> NRIC_FIRST_PERSON.equals(person.getNric())) + .findFirst(); + Person personToDelete; + + personToDelete = personWithMatchingNric.orElse(null); + + assertNotNull(personToDelete); + + DeleteCommand deleteCommand = new DeleteCommand(NRIC_FIRST_PERSON); String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)); @@ -42,20 +53,25 @@ public void execute_validIndexUnfilteredList_success() { } @Test - public void execute_invalidIndexUnfilteredList_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + public void execute_invalidNricUnfilteredList_throwsCommandException() { + Nric invalidNric = new Nric("A0000000A"); + DeleteCommand deleteCommand = new DeleteCommand(invalidNric); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(deleteCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } @Test - public void execute_validIndexFilteredList_success() { - showPersonAtIndex(model, INDEX_FIRST_PERSON); + public void execute_validNricFilteredList_success() { + showPersonByNric(model, NRIC_FIRST_PERSON); + + Optional personWithMatchingNric = model.getFilteredPersonList().stream() + .filter(person -> NRIC_FIRST_PERSON.equals(person.getNric())) + .findFirst(); + Person personToDelete = personWithMatchingNric.orElse(null); - Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON); + assertNotNull(personToDelete); + DeleteCommand deleteCommand = new DeleteCommand(NRIC_FIRST_PERSON); String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)); @@ -67,28 +83,32 @@ public void execute_validIndexFilteredList_success() { } @Test - public void execute_invalidIndexFilteredList_throwsCommandException() { - showPersonAtIndex(model, INDEX_FIRST_PERSON); + public void execute_invalidNricFilteredList_throwsCommandException() { + showPersonByNric(model, NRIC_FIRST_PERSON); - Index outOfBoundIndex = INDEX_SECOND_PERSON; - // ensures that outOfBoundIndex is still in bounds of address book list - assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); + Nric invalidNric = NRIC_SECOND_PERSON; + // ensures that invalidNric is still in bounds of address book list + Person invalidNricPerson = model.getAddressBook().getPersonList().stream() + .filter(p -> p.getNric().equals(NRIC_SECOND_PERSON)) + .findFirst() + .orElse(null); + assertNotNull(invalidNricPerson); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + DeleteCommand deleteCommand = new DeleteCommand(invalidNric); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(deleteCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } @Test public void equals() { - DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON); - DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON); + DeleteCommand deleteFirstCommand = new DeleteCommand(NRIC_FIRST_PERSON); + DeleteCommand deleteSecondCommand = new DeleteCommand(NRIC_SECOND_PERSON); // same object -> returns true assertTrue(deleteFirstCommand.equals(deleteFirstCommand)); // same values -> returns true - DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON); + DeleteCommand deleteFirstCommandCopy = new DeleteCommand(NRIC_FIRST_PERSON); assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy)); // different types -> returns false @@ -103,9 +123,9 @@ public void equals() { @Test public void toStringMethod() { - Index targetIndex = Index.fromOneBased(1); - DeleteCommand deleteCommand = new DeleteCommand(targetIndex); - String expected = DeleteCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}"; + Nric targetNric = new Nric("S1231231D"); + DeleteCommand deleteCommand = new DeleteCommand(targetNric); + String expected = DeleteCommand.class.getCanonicalName() + "{targetNric=" + targetNric + "}"; assertEquals(expected, deleteCommand.toString()); } diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index 469dd97daa7..14035eb4423 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.DESC_BOB; @@ -12,18 +13,19 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; -import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; import seedu.address.model.AddressBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.testutil.EditPersonDescriptorBuilder; import seedu.address.testutil.PersonBuilder; @@ -39,94 +41,151 @@ public class EditCommandTest { public void execute_allFieldsSpecifiedUnfilteredList_success() { Person editedPerson = new PersonBuilder().build(); EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(editedPerson).build(); - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor); + EditCommand editCommand = new EditCommand(NRIC_FIRST_PERSON, descriptor); String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); - expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + // Find the person in the filtered list with the specified NRIC + Person personToEdit = model.getFilteredPersonList().stream() + .filter(p -> p.getNric().equals(NRIC_FIRST_PERSON)) + .findFirst() + .orElse(null); + + // Ensure the person exists in the filtered list + assertNotNull(personToEdit); + + // Update the person in the expected model + expectedModel.setPerson(personToEdit, editedPerson); assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } @Test public void execute_someFieldsSpecifiedUnfilteredList_success() { - Index indexLastPerson = Index.fromOneBased(model.getFilteredPersonList().size()); - Person lastPerson = model.getFilteredPersonList().get(indexLastPerson.getZeroBased()); + // Assuming NRIC is available as a unique identifier for the person we want to edit + Person lastPerson = model.getFilteredPersonList().get(model.getFilteredPersonList().size() - 1); + Nric targetNric = lastPerson.getNric(); + // Build the edited person with updated fields PersonBuilder personInList = new PersonBuilder(lastPerson); Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) .withTags(VALID_TAG_HUSBAND).build(); - EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB) - .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); - EditCommand editCommand = new EditCommand(indexLastPerson, descriptor); + // Create the EditPersonDescriptor with the fields to edit + EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder() + .withName(VALID_NAME_BOB) + .withPhone(VALID_PHONE_BOB) + .withTags(VALID_TAG_HUSBAND) + .build(); + // Create the edit command using the NRIC instead of an index + EditCommand editCommand = new EditCommand(targetNric, descriptor); + + // Define the expected success message String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + // Set up the expected model with the edited person Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); expectedModel.setPerson(lastPerson, editedPerson); + // Execute the command and check if it succeeds assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } @Test public void execute_noFieldSpecifiedUnfilteredList_success() { - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, new EditPersonDescriptor()); - Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + // Retrieve the first person in the list and their NRIC + Person firstPerson = model.getFilteredPersonList().get(0); + Nric targetNric = firstPerson.getNric(); + + // Create an edit command using the NRIC and an empty descriptor (no fields specified for editing) + EditCommand editCommand = new EditCommand(targetNric, new EditPersonDescriptor()); + // Since no fields are edited, the edited person should be the same as the original + Person editedPerson = firstPerson; + + // Define the expected success message String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + // Set up the expected model, which remains unchanged Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + // Execute the command and check if it succeeds assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } @Test public void execute_filteredList_success() { + // Show the person at the first index in the filtered list showPersonAtIndex(model, INDEX_FIRST_PERSON); - Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + // Retrieve the person in the filtered list and get their NRIC + Person personInFilteredList = model.getFilteredPersonList().get(0); + Nric targetNric = personInFilteredList.getNric(); + + // Create an edited version of the person with the updated name Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).build(); - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, - new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + // Create the edit command using the NRIC and the name descriptor + EditCommand editCommand = new EditCommand(targetNric, + new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + + // Define the expected success message String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + // Set up the expected model with the edited person Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); - expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + expectedModel.setPerson(personInFilteredList, editedPerson); + // Execute the command and check if it succeeds assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } @Test public void execute_duplicatePersonUnfilteredList_failure() { - Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + // Retrieve the first and second persons in the list by their NRICs + Person firstPerson = model.getFilteredPersonList().get(0); + Nric secondPersonNric = model.getFilteredPersonList().get(1).getNric(); + + // Create an EditPersonDescriptor using the details of the first person (to simulate duplication) EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).build(); - EditCommand editCommand = new EditCommand(INDEX_SECOND_PERSON, descriptor); + // Create the edit command with the NRIC of the second person, attempting to duplicate the first person + EditCommand editCommand = new EditCommand(secondPersonNric, descriptor); + + // Assert that the command fails with a duplicate person message assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); } @Test public void execute_duplicatePersonFilteredList_failure() { + // Show the person at the first index in the filtered list showPersonAtIndex(model, INDEX_FIRST_PERSON); - // edit person in filtered list into a duplicate in address book - Person personInList = model.getAddressBook().getPersonList().get(INDEX_SECOND_PERSON.getZeroBased()); - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, + // Retrieve the NRICs of the first person in the filtered list and the second person in the address book + Person personInFilteredList = model.getFilteredPersonList().get(0); + Nric targetNric = personInFilteredList.getNric(); + + Person personInList = model.getAddressBook().getPersonList().get(1); + + // Create an EditCommand to edit the first person to have the same details as the second person (duplicate) + EditCommand editCommand = new EditCommand(targetNric, new EditPersonDescriptorBuilder(personInList).build()); + // Assert that the command fails with a duplicate person message assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); } @Test - public void execute_invalidPersonIndexUnfilteredList_failure() { - Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); + public void execute_invalidPersonNricUnfilteredList_failure() { + // Create a fake NRIC that doesn't exist in the address book + Nric invalidNric = new Nric("A1234567A"); // Ensure this NRIC is not in the test data EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build(); - EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor); + EditCommand editCommand = new EditCommand(invalidNric, descriptor); - assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + // Assert that the command fails with an invalid person message + assertCommandFailure(editCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } /** @@ -134,25 +193,32 @@ public void execute_invalidPersonIndexUnfilteredList_failure() { * but smaller than size of address book */ @Test - public void execute_invalidPersonIndexFilteredList_failure() { + public void execute_invalidPersonNricFilteredList_failure() { + // Show only the first person in the filtered list showPersonAtIndex(model, INDEX_FIRST_PERSON); - Index outOfBoundIndex = INDEX_SECOND_PERSON; - // ensures that outOfBoundIndex is still in bounds of address book list - assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); - EditCommand editCommand = new EditCommand(outOfBoundIndex, + // Get the NRIC of a person who is not in the filtered list but is in the full address book + Person outOfFilteredListPerson = model.getAddressBook().getPersonList().get(1); + Nric outOfFilteredListNric = outOfFilteredListPerson.getNric(); + + // Ensure that outOfFilteredListPerson is actually outside of the filtered list's bounds + assertTrue(model.getFilteredPersonList().stream().noneMatch(p -> p.getNric().equals(outOfFilteredListNric))); + + // Create an EditCommand using the NRIC of the person outside the filtered list + EditCommand editCommand = new EditCommand(outOfFilteredListNric, new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); - assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + // Assert that the command fails with an invalid person message + assertCommandFailure(editCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } @Test public void equals() { - final EditCommand standardCommand = new EditCommand(INDEX_FIRST_PERSON, DESC_AMY); + final EditCommand standardCommand = new EditCommand(NRIC_FIRST_PERSON, DESC_AMY); // same values -> returns true EditPersonDescriptor copyDescriptor = new EditPersonDescriptor(DESC_AMY); - EditCommand commandWithSameValues = new EditCommand(INDEX_FIRST_PERSON, copyDescriptor); + EditCommand commandWithSameValues = new EditCommand(NRIC_FIRST_PERSON, copyDescriptor); assertTrue(standardCommand.equals(commandWithSameValues)); // same object -> returns true @@ -165,18 +231,18 @@ public void equals() { assertFalse(standardCommand.equals(new ClearCommand())); // different index -> returns false - assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_PERSON, DESC_AMY))); + assertFalse(standardCommand.equals(new EditCommand(NRIC_SECOND_PERSON, DESC_AMY))); // different descriptor -> returns false - assertFalse(standardCommand.equals(new EditCommand(INDEX_FIRST_PERSON, DESC_BOB))); + assertFalse(standardCommand.equals(new EditCommand(NRIC_FIRST_PERSON, DESC_BOB))); } @Test public void toStringMethod() { - Index index = Index.fromOneBased(1); + Nric nric = NRIC_FIRST_PERSON; EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); - EditCommand editCommand = new EditCommand(index, editPersonDescriptor); - String expected = EditCommand.class.getCanonicalName() + "{index=" + index + ", editPersonDescriptor=" + EditCommand editCommand = new EditCommand(NRIC_FIRST_PERSON, editPersonDescriptor); + String expected = EditCommand.class.getCanonicalName() + "{nric=" + nric + ", editPersonDescriptor=" + editPersonDescriptor + "}"; assertEquals(expected, editCommand.toString()); } diff --git a/src/test/java/seedu/address/logic/commands/LogCommandTest.java b/src/test/java/seedu/address/logic/commands/LogCommandTest.java index 7062da75889..2947edca327 100644 --- a/src/test/java/seedu/address/logic/commands/LogCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/LogCommandTest.java @@ -6,38 +6,53 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import java.util.List; +import java.util.Optional; + import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; import seedu.address.model.person.Log; +import seedu.address.model.person.Nric; +import seedu.address.model.person.Person; public class LogCommandTest { @Test - public void execute_validIndexAndLog_addsLogSuccessfully() throws Exception { + public void execute_validNricAndLog_addsLogSuccessfully() throws Exception { Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); Log validLog = new Log("25-12-2024 14:30 Attended appointment"); - Index validIndex = Index.fromOneBased(1); - LogCommand logCommand = new LogCommand(validIndex, validLog); + Nric validNric = new Nric("S1234567A"); + LogCommand logCommand = new LogCommand(validNric, validLog); CommandResult commandResult = logCommand.execute(model); - assertEquals("Log added to " + model.getFilteredPersonList() - .get(validIndex.getZeroBased()).getName(), commandResult.getFeedbackToUser()); - assertTrue(model.getFilteredPersonList().get(validIndex.getZeroBased()) - .getLogEntries().getLogs().contains(validLog)); + List lastShownList = model.getFilteredPersonList(); + Optional personWithMatchingNric = lastShownList.stream() + .filter(person -> validNric.equals(person.getNric())) + .findFirst(); + + Person personWithLogAdded; + if (personWithMatchingNric.isPresent()) { + personWithLogAdded = personWithMatchingNric.get(); + } else { + throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND); + } + + assertEquals("Log added to " + personWithLogAdded.getName(), commandResult.getFeedbackToUser()); + assertTrue(personWithLogAdded.getLogEntries().getLogs().contains(validLog)); } @Test public void execute_invalidIndex_throwsCommandException() { Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); Log validLog = new Log("25-12-2024 14:30 Attended appointment"); - Index invalidIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - LogCommand logCommand = new LogCommand(invalidIndex, validLog); + Nric invalidNric = new Nric("A0000000A"); + LogCommand logCommand = new LogCommand(invalidNric, validLog); assertThrows(CommandException.class, () -> logCommand.execute(model)); } @@ -45,8 +60,8 @@ public void execute_invalidIndex_throwsCommandException() { @Test public void equals_sameObject_returnsTrue() { Log validLog = new Log("25-12-2024 14:30 Attended appointment"); - Index validIndex = Index.fromOneBased(1); - LogCommand logCommand = new LogCommand(validIndex, validLog); + Nric validNric = new Nric("S1234567A"); + LogCommand logCommand = new LogCommand(validNric, validLog); assertTrue(logCommand.equals(logCommand)); } @@ -54,8 +69,8 @@ public void equals_sameObject_returnsTrue() { @Test public void equals_differentObject_returnsFalse() { Log validLog = new Log("25-12-2024 14:30 Attended appointment"); - Index validIndex = Index.fromOneBased(1); - LogCommand logCommand = new LogCommand(validIndex, validLog); + Nric validNric = new Nric("S1234567A"); + LogCommand logCommand = new LogCommand(validNric, validLog); assertFalse(logCommand.equals(null)); assertFalse(logCommand.equals(new ClearCommand())); @@ -64,9 +79,9 @@ public void equals_differentObject_returnsFalse() { @Test public void equals_sameValues_returnsTrue() { Log validLog = new Log("25-12-2024 14:30 Attended appointment"); - Index validIndex = Index.fromOneBased(1); - LogCommand logCommand1 = new LogCommand(validIndex, validLog); - LogCommand logCommand2 = new LogCommand(validIndex, validLog); + Nric validNric = new Nric("S1234567A"); + LogCommand logCommand1 = new LogCommand(validNric, validLog); + LogCommand logCommand2 = new LogCommand(validNric, validLog); assertTrue(logCommand1.equals(logCommand2)); } @@ -75,10 +90,10 @@ public void equals_sameValues_returnsTrue() { public void equals_differentValues_returnsFalse() { Log validLog1 = new Log("25-12-2024 14:30 Attended appointment"); Log validLog2 = new Log("26-12-2024 15:30 Attended appointment"); - Index validIndex1 = Index.fromOneBased(1); - Index validIndex2 = Index.fromOneBased(2); - LogCommand logCommand1 = new LogCommand(validIndex1, validLog1); - LogCommand logCommand2 = new LogCommand(validIndex2, validLog2); + Nric validNric1 = new Nric("S1234567A"); + Nric validNric2 = new Nric("S1234567B"); + LogCommand logCommand1 = new LogCommand(validNric1, validLog1); + LogCommand logCommand2 = new LogCommand(validNric2, validLog2); assertFalse(logCommand1.equals(logCommand2)); } diff --git a/src/test/java/seedu/address/logic/commands/RemarkCommandTest.java b/src/test/java/seedu/address/logic/commands/RemarkCommandTest.java index a61244fb48e..e90139524ea 100644 --- a/src/test/java/seedu/address/logic/commands/RemarkCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/RemarkCommandTest.java @@ -1,24 +1,27 @@ package seedu.address.logic.commands; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_BOB; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; +import static seedu.address.logic.commands.CommandTestUtil.showPersonByNric; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; -import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; import seedu.address.model.AddressBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.model.person.Remark; import seedu.address.testutil.PersonBuilder; @@ -35,17 +38,18 @@ public class RemarkCommandTest { public void execute_addRemarkUnfilteredList_success() { Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); Person editedPerson = new PersonBuilder(firstPerson).withRemark(REMARK_STUB).build(); - RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_PERSON, new Remark(editedPerson.getRemark().value)); + RemarkCommand remarkCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(editedPerson.getRemark().value)); String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); expectedModel.setPerson(firstPerson, editedPerson); assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel); } + @Test public void execute_deleteRemarkUnfilteredList_success() { Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); Person editedPerson = new PersonBuilder(firstPerson).withRemark("").build(); - RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_PERSON, + RemarkCommand remarkCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(editedPerson.getRemark().toString())); String expectedMessage = String.format(RemarkCommand.MESSAGE_DELETE_REMARK_SUCCESS, editedPerson); @@ -61,7 +65,7 @@ public void execute_filteredList_success() { Person editedPerson = new PersonBuilder(model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased())) .withRemark(REMARK_STUB).build(); - RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_PERSON, new Remark(editedPerson.getRemark().value)); + RemarkCommand remarkCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(editedPerson.getRemark().value)); String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); @@ -70,31 +74,39 @@ public void execute_filteredList_success() { assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel); } + @Test public void execute_invalidPersonIndexUnfilteredList_failure() { - Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - RemarkCommand remarkCommand = new RemarkCommand(outOfBoundIndex, new Remark(VALID_REMARK_BOB)); - assertCommandFailure(remarkCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + Nric invalidNric = new Nric("A0000000A"); + RemarkCommand remarkCommand = new RemarkCommand(invalidNric, new Remark(VALID_REMARK_BOB)); + assertCommandFailure(remarkCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } + /** * Edit filtered list where index is larger than size of filtered list, * but smaller than size of address book */ @Test public void execute_invalidPersonIndexFilteredList_failure() { - showPersonAtIndex(model, INDEX_FIRST_PERSON); - Index outOfBoundIndex = INDEX_SECOND_PERSON; - // ensures that outOfBoundIndex is still in bounds of address book list - assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); - RemarkCommand remarkCommand = new RemarkCommand(outOfBoundIndex, new Remark(VALID_REMARK_BOB)); + showPersonByNric(model, NRIC_FIRST_PERSON); + Nric invalidNric = NRIC_SECOND_PERSON; + // ensures that invalidNric is still in bounds of address book list + Person invalidNricPerson = model.getAddressBook().getPersonList().stream() + .filter(p -> p.getNric().equals(NRIC_SECOND_PERSON)) + .findFirst() + .orElse(null); + assertNotNull(invalidNricPerson); + + RemarkCommand remarkCommand = new RemarkCommand(invalidNric, new Remark(VALID_REMARK_BOB)); - assertCommandFailure(remarkCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(remarkCommand, model, Messages.MESSAGE_NO_PERSON_FOUND); } + @Test public void equals() { - final RemarkCommand standardCommand = new RemarkCommand(INDEX_FIRST_PERSON, new Remark(VALID_REMARK_AMY)); + final RemarkCommand standardCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(VALID_REMARK_AMY)); // same values -> returns true - RemarkCommand commandWithSameValues = new RemarkCommand(INDEX_FIRST_PERSON, new Remark(VALID_REMARK_AMY)); + RemarkCommand commandWithSameValues = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(VALID_REMARK_AMY)); assertTrue(standardCommand.equals(commandWithSameValues)); // same object -> returns true assertTrue(standardCommand.equals(standardCommand)); @@ -103,8 +115,8 @@ public void equals() { // different types -> returns false assertFalse(standardCommand.equals(new ClearCommand())); // different index -> returns false - assertFalse(standardCommand.equals(new RemarkCommand(INDEX_SECOND_PERSON, new Remark(VALID_REMARK_AMY)))); + assertFalse(standardCommand.equals(new RemarkCommand(NRIC_SECOND_PERSON, new Remark(VALID_REMARK_AMY)))); // different remark -> returns false - assertFalse(standardCommand.equals(new RemarkCommand(INDEX_FIRST_PERSON, new Remark(VALID_REMARK_BOB)))); + assertFalse(standardCommand.equals(new RemarkCommand(NRIC_FIRST_PERSON, new Remark(VALID_REMARK_BOB)))); } } diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 1839eb2e33c..69fad02763f 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -6,7 +6,7 @@ import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND; import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; import static seedu.address.testutil.Assert.assertThrows; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; import java.util.Arrays; import java.util.List; @@ -55,8 +55,8 @@ public void parseCommand_clear() throws Exception { @Test public void parseCommand_delete() throws Exception { DeleteCommand command = (DeleteCommand) parser.parseCommand( - DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased()); - assertEquals(new DeleteCommand(INDEX_FIRST_PERSON), command); + DeleteCommand.COMMAND_WORD + " " + NRIC_FIRST_PERSON); + assertEquals(new DeleteCommand(NRIC_FIRST_PERSON), command); } @Test @@ -64,8 +64,8 @@ public void parseCommand_edit() throws Exception { Person person = new PersonBuilder().build(); EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(person).build(); EditCommand command = (EditCommand) parser.parseCommand(EditCommand.COMMAND_WORD + " " - + INDEX_FIRST_PERSON.getOneBased() + " " + PersonUtil.getEditPersonDescriptorDetails(descriptor)); - assertEquals(new EditCommand(INDEX_FIRST_PERSON, descriptor), command); + + NRIC_FIRST_PERSON + " " + PersonUtil.getEditPersonDescriptorDetails(descriptor)); + assertEquals(new EditCommand(NRIC_FIRST_PERSON, descriptor), command); } @Test @@ -104,9 +104,9 @@ public void parseCommand_schedule() throws Exception { public void parseCommand_remark() throws Exception { final Remark remark = new Remark("Some remark."); RemarkCommand command = (RemarkCommand) parser.parseCommand(RemarkCommand.COMMAND_WORD + " " - + INDEX_FIRST_PERSON.getOneBased() + " " + PREFIX_REMARK + remark.value); + + NRIC_FIRST_PERSON + " " + PREFIX_REMARK + remark.value); - assertEquals(new RemarkCommand(INDEX_FIRST_PERSON, remark), command); + assertEquals(new RemarkCommand(NRIC_FIRST_PERSON, remark), command); } @Test diff --git a/src/test/java/seedu/address/logic/parser/AppointmentCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AppointmentCommandParserTest.java index 344e80f83f1..8b528e09111 100644 --- a/src/test/java/seedu/address/logic/parser/AppointmentCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AppointmentCommandParserTest.java @@ -6,8 +6,8 @@ import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.AppointmentCommand; +import seedu.address.model.person.Nric; public class AppointmentCommandParserTest { @@ -15,89 +15,81 @@ public class AppointmentCommandParserTest { @Test public void parse_allFieldsPresent_success() { - // valid input with valid index, date, and time - assertParseSuccess(parser, "1 a/15-08-2024 14:30", - new AppointmentCommand(Index.fromZeroBased(0), "15-08-2024 14:30")); + // valid input with valid nric, date, and time + assertParseSuccess(parser, "S1231231D a/15-08-2024 14:30", + new AppointmentCommand(new Nric("S1231231D"), "15-08-2024 14:30")); - // valid input with non-zero index - assertParseSuccess(parser, "3 a/01-01-2025 09:45", - new AppointmentCommand(Index.fromZeroBased(2), "01-01-2025 09:45")); + // valid input with non-zero nric + assertParseSuccess(parser, "T1234567D a/01-01-2025 09:45", + new AppointmentCommand(new Nric("T1234567D"), "01-01-2025 09:45")); } @Test public void parse_invalidDateFormat_failure() { // invalid date format with missing hyphens - assertParseFailure(parser, "1 a/15082024 14:30", + assertParseFailure(parser, "S1231231D a/15082024 14:30", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); // invalid date format with non-numeric date components - assertParseFailure(parser, "1 a/aa-bb-cccc 14:30", + assertParseFailure(parser, "S1231231D a/aa-bb-cccc 14:30", "Invalid date format. Day, month, and year must be numbers."); // invalid date format with incorrect day/month range - assertParseFailure(parser, "1 a/32-12-2024 14:30", + assertParseFailure(parser, "S1231231D a/32-12-2024 14:30", "Day and months must be valid."); // invalid date format with incorrect month range - assertParseFailure(parser, "1 a/15-13-2024 14:30", + assertParseFailure(parser, "S1231231D a/15-13-2024 14:30", "Day and months must be valid."); } @Test public void parse_invalidTimeFormat_failure() { // invalid time format with missing colon - assertParseFailure(parser, "1 a/15-08-2024 1430", + assertParseFailure(parser, "S1231231D a/15-08-2024 1430", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); // invalid time format with non-numeric time components - assertParseFailure(parser, "1 a/15-08-2024 aa:bb", + assertParseFailure(parser, "S1231231D a/15-08-2024 aa:bb", "Invalid time format. Hour and minute must be numbers."); // invalid hour range - assertParseFailure(parser, "1 a/15-08-2024 25:30", + assertParseFailure(parser, "S1231231D a/15-08-2024 25:30", "Hour and minute must be valid."); // invalid minute range - assertParseFailure(parser, "1 a/15-08-2024 14:60", + assertParseFailure(parser, "S1231231D a/15-08-2024 14:60", "Hour and minute must be valid."); } @Test - public void parse_invalidIndex_failure() { - // invalid non-numeric index - assertParseFailure(parser, "a a/15-08-2024 14:30", - String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); - - // zero index (not allowed) - assertParseFailure(parser, "0 a/15-08-2024 14:30", - String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); - - // negative index - assertParseFailure(parser, "-1 a/15-08-2024 14:30", + public void parse_invalidNric_failure() { + // invalid nric + assertParseFailure(parser, "1 a/15-08-2024 14:30", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } @Test public void parse_missingDateAndTime_failure() { // missing date and time - assertParseFailure(parser, "1 a/", + assertParseFailure(parser, "S1231231D a/", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); // empty date and time - assertParseFailure(parser, "1 a/ ", + assertParseFailure(parser, "S1231231D a/ ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } @Test public void parse_missingAppointmentPrefix_failure() { // missing 'a/' prefix for appointment - assertParseFailure(parser, "1 15-08-2024 14:30", + assertParseFailure(parser, "S1231231D 15-08-2024 14:30", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } @Test - public void parse_missingIndex_failure() { - // missing index + public void parse_missingNric_failure() { + // missing nric assertParseFailure(parser, "a/15-08-2024 14:30", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } @@ -105,7 +97,7 @@ public void parse_missingIndex_failure() { @Test public void parse_extraArguments_failure() { // extra arguments after the valid appointment command - assertParseFailure(parser, "1 a/15-08-2024 14:30 extraArg", + assertParseFailure(parser, "S1231231D a/15-08-2024 14:30 extraArg", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AppointmentCommand.MESSAGE_USAGE)); } } diff --git a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java index 6a40e14a649..332bd0bd82d 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java @@ -3,7 +3,7 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; import org.junit.jupiter.api.Test; @@ -22,7 +22,7 @@ public class DeleteCommandParserTest { @Test public void parse_validArgs_returnsDeleteCommand() { - assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON)); + assertParseSuccess(parser, "S1234567A", new DeleteCommand(NRIC_FIRST_PERSON)); } @Test diff --git a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java index 33cf90eead2..668e284be1d 100644 --- a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java @@ -26,19 +26,19 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; -import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; -import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_SECOND_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_THIRD_PERSON; import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; import seedu.address.model.person.Address; import seedu.address.model.person.Appointment; import seedu.address.model.person.Email; import seedu.address.model.person.Name; +import seedu.address.model.person.Nric; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; import seedu.address.testutil.EditPersonDescriptorBuilder; @@ -54,132 +54,128 @@ public class EditCommandParserTest { @Test public void parse_missingParts_failure() { - // no index specified + // no nric specified assertParseFailure(parser, VALID_NAME_AMY, MESSAGE_INVALID_FORMAT); // no field specified - assertParseFailure(parser, "1", EditCommand.MESSAGE_NOT_EDITED); + assertParseFailure(parser, "S1234567A", EditCommand.MESSAGE_NOT_EDITED); - // no index and no field specified + // no nric and no field specified assertParseFailure(parser, "", MESSAGE_INVALID_FORMAT); } @Test public void parse_invalidPreamble_failure() { - // negative index + // invalid nric assertParseFailure(parser, "-5" + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT); - // zero index - assertParseFailure(parser, "0" + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT); - // invalid arguments being parsed as preamble - assertParseFailure(parser, "1 some random string", MESSAGE_INVALID_FORMAT); + assertParseFailure(parser, "S1234567A some random string", MESSAGE_INVALID_FORMAT); // invalid prefix being parsed as preamble - assertParseFailure(parser, "1 j/ string", MESSAGE_INVALID_FORMAT); + assertParseFailure(parser, "S1234567A j/ string", MESSAGE_INVALID_FORMAT); } @Test public void parse_invalidValue_failure() { - assertParseFailure(parser, "1" + INVALID_NAME_DESC, Name.MESSAGE_CONSTRAINTS); // invalid name - assertParseFailure(parser, "1" + INVALID_PHONE_DESC, Phone.MESSAGE_CONSTRAINTS); // invalid phone - assertParseFailure(parser, "1" + INVALID_EMAIL_DESC, Email.MESSAGE_CONSTRAINTS); // invalid email - assertParseFailure(parser, "1" + INVALID_ADDRESS_DESC, Address.MESSAGE_CONSTRAINTS); // invalid address - assertParseFailure(parser, "1" + INVALID_TAG_DESC, Tag.MESSAGE_CONSTRAINTS); // invalid tag - assertParseFailure(parser, "1" + INVALID_APPOINTMENT_DESC, + assertParseFailure(parser, "S1234567A" + INVALID_NAME_DESC, Name.MESSAGE_CONSTRAINTS); // invalid name + assertParseFailure(parser, "S1234567A" + INVALID_PHONE_DESC, Phone.MESSAGE_CONSTRAINTS); // invalid phone + assertParseFailure(parser, "S1234567A" + INVALID_EMAIL_DESC, Email.MESSAGE_CONSTRAINTS); // invalid email + assertParseFailure(parser, "S1234567A" + INVALID_ADDRESS_DESC, Address.MESSAGE_CONSTRAINTS); // invalid address + assertParseFailure(parser, "S1234567A" + INVALID_TAG_DESC, Tag.MESSAGE_CONSTRAINTS); // invalid tag + assertParseFailure(parser, "S1234567A" + INVALID_APPOINTMENT_DESC, Appointment.MESSAGE_CONSTRAINTS); // invalid appointment // invalid phone followed by valid email - assertParseFailure(parser, "1" + INVALID_PHONE_DESC + EMAIL_DESC_AMY, Phone.MESSAGE_CONSTRAINTS); + assertParseFailure(parser, "S1234567A" + INVALID_PHONE_DESC + EMAIL_DESC_AMY, Phone.MESSAGE_CONSTRAINTS); // while parsing {@code PREFIX_TAG} alone will reset the tags of the {@code Person} being edited, // parsing it together with a valid tag results in error - assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_DESC_HUSBAND + TAG_EMPTY, + assertParseFailure(parser, "S1234567A" + TAG_DESC_FRIEND + TAG_DESC_HUSBAND + TAG_EMPTY, Tag.MESSAGE_CONSTRAINTS); - assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_EMPTY + TAG_DESC_HUSBAND, + assertParseFailure(parser, "S1234567A" + TAG_DESC_FRIEND + TAG_EMPTY + TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS); - assertParseFailure(parser, "1" + TAG_EMPTY + TAG_DESC_FRIEND + TAG_DESC_HUSBAND, + assertParseFailure(parser, "S1234567A" + TAG_EMPTY + TAG_DESC_FRIEND + TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS); // multiple invalid values, but only the first invalid value is captured - assertParseFailure(parser, "1" + INVALID_NAME_DESC + INVALID_EMAIL_DESC + VALID_ADDRESS_AMY + assertParseFailure(parser, "S1234567A" + INVALID_NAME_DESC + INVALID_EMAIL_DESC + VALID_ADDRESS_AMY + VALID_PHONE_AMY, Name.MESSAGE_CONSTRAINTS); } @Test public void parse_allFieldsSpecified_success() { - Index targetIndex = INDEX_SECOND_PERSON; - String userInput = targetIndex.getOneBased() + PHONE_DESC_BOB + TAG_DESC_HUSBAND + Nric targetNric = NRIC_SECOND_PERSON; + String userInput = targetNric + PHONE_DESC_BOB + TAG_DESC_HUSBAND + EMAIL_DESC_AMY + ADDRESS_DESC_AMY + NAME_DESC_AMY + TAG_DESC_FRIEND + APPOINTMENT_DESC_AMY; EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY) .withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY) .withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).withAppointment(VALID_APPOINTMENT_AMY).build(); - EditCommand expectedCommand = new EditCommand(targetIndex, descriptor); + EditCommand expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); } @Test public void parse_someFieldsSpecified_success() { - Index targetIndex = INDEX_FIRST_PERSON; - String userInput = targetIndex.getOneBased() + PHONE_DESC_BOB + EMAIL_DESC_AMY + APPOINTMENT_DESC_AMY; + Nric targetNric = NRIC_FIRST_PERSON; + String userInput = targetNric + PHONE_DESC_BOB + EMAIL_DESC_AMY + APPOINTMENT_DESC_AMY; EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withPhone(VALID_PHONE_BOB) .withEmail(VALID_EMAIL_AMY).withAppointment(VALID_APPOINTMENT_AMY).build(); - EditCommand expectedCommand = new EditCommand(targetIndex, descriptor); + EditCommand expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); } @Test public void parse_oneFieldSpecified_success() { - // name - Index targetIndex = INDEX_THIRD_PERSON; - String userInput = targetIndex.getOneBased() + NAME_DESC_AMY; + Nric targetNric = NRIC_THIRD_PERSON; + String userInput = targetNric + NAME_DESC_AMY; EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY).build(); - EditCommand expectedCommand = new EditCommand(targetIndex, descriptor); + EditCommand expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); // phone - userInput = targetIndex.getOneBased() + PHONE_DESC_AMY; + userInput = targetNric + PHONE_DESC_AMY; descriptor = new EditPersonDescriptorBuilder().withPhone(VALID_PHONE_AMY).build(); - expectedCommand = new EditCommand(targetIndex, descriptor); + expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); // email - userInput = targetIndex.getOneBased() + EMAIL_DESC_AMY; + userInput = targetNric + EMAIL_DESC_AMY; descriptor = new EditPersonDescriptorBuilder().withEmail(VALID_EMAIL_AMY).build(); - expectedCommand = new EditCommand(targetIndex, descriptor); + expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); // address - userInput = targetIndex.getOneBased() + ADDRESS_DESC_AMY; + userInput = targetNric + ADDRESS_DESC_AMY; descriptor = new EditPersonDescriptorBuilder().withAddress(VALID_ADDRESS_AMY).build(); - expectedCommand = new EditCommand(targetIndex, descriptor); + expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); // appointment - userInput = targetIndex.getOneBased() + APPOINTMENT_DESC_AMY; + userInput = targetNric + APPOINTMENT_DESC_AMY; descriptor = new EditPersonDescriptorBuilder().withAppointment(VALID_APPOINTMENT_AMY).build(); - expectedCommand = new EditCommand(targetIndex, descriptor); + expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); // tags - userInput = targetIndex.getOneBased() + TAG_DESC_FRIEND; + userInput = targetNric + TAG_DESC_FRIEND; descriptor = new EditPersonDescriptorBuilder().withTags(VALID_TAG_FRIEND).build(); - expectedCommand = new EditCommand(targetIndex, descriptor); + expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); } @Test public void parse_resetTags_success() { - Index targetIndex = INDEX_THIRD_PERSON; - String userInput = targetIndex.getOneBased() + TAG_EMPTY; + Nric targetNric = NRIC_THIRD_PERSON; + String userInput = targetNric + TAG_EMPTY; EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withTags().build(); - EditCommand expectedCommand = new EditCommand(targetIndex, descriptor); + EditCommand expectedCommand = new EditCommand(targetNric, descriptor); assertParseSuccess(parser, userInput, expectedCommand); } diff --git a/src/test/java/seedu/address/logic/parser/LogCommandParserTest.java b/src/test/java/seedu/address/logic/parser/LogCommandParserTest.java index 11eab4c01c2..343da6e6f9e 100644 --- a/src/test/java/seedu/address/logic/parser/LogCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/LogCommandParserTest.java @@ -7,36 +7,36 @@ import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.LogCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Log; +import seedu.address.model.person.Nric; public class LogCommandParserTest { @Test public void parse_validArgs_returnsLogCommand() throws Exception { LogCommandParser parser = new LogCommandParser(); - LogCommand command = parser.parse("1 25-12-2024 14:30 Attended appointment"); - assertEquals(new LogCommand(Index.fromZeroBased(0), new Log("Attended appointment", + LogCommand command = parser.parse("S1234567A 25-12-2024 14:30 Attended appointment"); + assertEquals(new LogCommand(new Nric("S1234567A"), new Log("Attended appointment", LocalDateTime.of(2024, 12, 25, 14, 30))), command); } @Test public void parse_missingArgs_throwsParseException() { LogCommandParser parser = new LogCommandParser(); - assertThrows(ParseException.class, () -> parser.parse("1 25-12-2024 14:30")); + assertThrows(ParseException.class, () -> parser.parse("S1234567A 25-12-2024 14:30")); } @Test - public void parse_invalidIndex_throwsParseException() { + public void parse_invalidNric_throwsParseException() { LogCommandParser parser = new LogCommandParser(); - assertThrows(ParseException.class, () -> parser.parse("a 25-12-2024 14:30 Attended appointment")); + assertThrows(ParseException.class, () -> parser.parse("1234567 25-12-2024 14:30 Attended appointment")); } @Test public void parse_invalidDateTime_throwsParseException() { LogCommandParser parser = new LogCommandParser(); - assertThrows(ParseException.class, () -> parser.parse("1 25-12-2024 25:30 Attended appointment")); + assertThrows(ParseException.class, () -> parser.parse("S1234567A 25-12-2024 25:30 Attended appointment")); } @Test diff --git a/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java b/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java index d37cdfd950c..58c2947abc0 100644 --- a/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java @@ -3,27 +3,28 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalNrics.NRIC_FIRST_PERSON; import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.RemarkCommand; +import seedu.address.model.person.Nric; import seedu.address.model.person.Remark; public class RemarkCommandParserTest { private RemarkCommandParser parser = new RemarkCommandParser(); private final String nonEmptyRemark = "Some remark."; @Test - public void parse_indexSpecified_success() { + public void parse_nricSpecified_success() { // have remark - Index targetIndex = INDEX_FIRST_PERSON; - String userInput = targetIndex.getOneBased() + " " + PREFIX_REMARK + nonEmptyRemark; - RemarkCommand expectedCommand = new RemarkCommand(INDEX_FIRST_PERSON, new Remark(nonEmptyRemark)); + Nric targetNric = NRIC_FIRST_PERSON; + String userInput = targetNric + " " + PREFIX_REMARK + nonEmptyRemark; + RemarkCommand expectedCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark(nonEmptyRemark)); assertParseSuccess(parser, userInput, expectedCommand); + // no remark - userInput = targetIndex.getOneBased() + " " + PREFIX_REMARK; - expectedCommand = new RemarkCommand(INDEX_FIRST_PERSON, new Remark("")); + userInput = targetNric + " " + PREFIX_REMARK; + expectedCommand = new RemarkCommand(NRIC_FIRST_PERSON, new Remark("")); assertParseSuccess(parser, userInput, expectedCommand); } @Test @@ -31,7 +32,7 @@ public void parse_missingCompulsoryField_failure() { String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE); // no parameters assertParseFailure(parser, RemarkCommand.COMMAND_WORD, expectedMessage); - // no index + // no nric assertParseFailure(parser, RemarkCommand.COMMAND_WORD + " " + nonEmptyRemark, expectedMessage); } } diff --git a/src/test/java/seedu/address/testutil/TypicalNrics.java b/src/test/java/seedu/address/testutil/TypicalNrics.java new file mode 100644 index 00000000000..81bfd67ca9e --- /dev/null +++ b/src/test/java/seedu/address/testutil/TypicalNrics.java @@ -0,0 +1,12 @@ +package seedu.address.testutil; + +import seedu.address.model.person.Nric; + +/** + * A utility class containing a list of {@code Nric} objects to be used in tests. + */ +public class TypicalNrics { + public static final Nric NRIC_FIRST_PERSON = new Nric("S1234567A"); + public static final Nric NRIC_SECOND_PERSON = new Nric("S1234567B"); + public static final Nric NRIC_THIRD_PERSON = new Nric("S1234567C"); +}