From 27207f8f5b7d420747f1b773bc8c5c15bd0f93a2 Mon Sep 17 00:00:00 2001 From: Kb-tay Date: Thu, 2 Nov 2023 15:20:29 +0800 Subject: [PATCH 1/3] Fix bugs in Complete Command Previously, the complete command shows success message for NullAppointments and for appointments with no matching dates Update the complete command to show error messages for the two cases This is to inform users that complete command did not execute for invalid inputs --- .../logic/commands/CompleteCommand.java | 19 ++++++++++++++----- .../java/seedu/address/model/AddressBook.java | 4 ++-- src/main/java/seedu/address/model/Model.java | 2 +- .../seedu/address/model/ModelManager.java | 10 +++++++--- .../seedu/address/model/person/Person.java | 4 ++++ .../model/person/UniquePersonList.java | 6 +++++- .../logic/commands/AddCommandTest.java | 2 +- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/CompleteCommand.java b/src/main/java/seedu/address/logic/commands/CompleteCommand.java index e2bd71ec6ca..6e2f049767a 100644 --- a/src/main/java/seedu/address/logic/commands/CompleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/CompleteCommand.java @@ -34,6 +34,10 @@ public class CompleteCommand extends Command { public static final String MESSAGE_COMPLETE_SUCCESS = "Appointments Completed!"; public static final String MESSAGE_INVALID_DATE_FORMAT = "Input Date should be in format of dd-MM-yyyy"; public static final String MESSAGE_INVALID_DATE = "Please input a valid Date"; + public static final String MESSAGE_PERSON_NO_APPOINTMENT = "No Appointment Found:" + + " Selected Person currently has no appointment scheduled"; + public static final String MESSAGE_DATE_NO_APPOINTMENT = "No Appointment Found:" + + " No Appointments found with the current date"; private final CompleteDescriptor completeDescriptor; public CompleteCommand(CompleteDescriptor completeDescriptor) { this.completeDescriptor = completeDescriptor; @@ -51,22 +55,27 @@ public CommandResult execute(Model model) throws CommandException { model.setPerson(personToEdit, edittedPerson); }; - Consumer editByDate = date -> { - model.clearAppointments(date); - }; - Optional index = completeDescriptor.getIndex(); if (index.isPresent()) { if (index.get().getZeroBased() >= lastShownList.size()) { throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } + + Person person = lastShownList.get(index.get().getZeroBased()); + if (person.hasNullAppointment()) { + throw new CommandException(MESSAGE_PERSON_NO_APPOINTMENT); + } } // if Index present, edit Model by Person Index completeDescriptor.getIndex().ifPresent(editByIndex); // if Date present, edit Model by Date; - completeDescriptor.getDate().ifPresent(editByDate); + if (completeDescriptor.getDate().isPresent()) { + if (!model.clearAppointments(completeDescriptor.getDate().orElse(null))) { + throw new CommandException(MESSAGE_DATE_NO_APPOINTMENT); + } + } model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); return new CommandResult(MESSAGE_COMPLETE_SUCCESS); diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 69ae97466fc..9a915c54401 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -102,8 +102,8 @@ public String gatherEmails(GatherEmailPrompt prompt) { return persons.gatherEmails(prompt); }; - public void clearAppointments(LocalDate date) { - persons.clearAppointments(date); + public boolean clearAppointments(LocalDate date) { + return persons.clearAppointments(date); } //// util methods diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index b6d6b9b901f..c75eb723483 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -106,5 +106,5 @@ public interface Model { */ String gatherEmails(GatherEmailPrompt prompt); - void clearAppointments(LocalDate date); + boolean clearAppointments(LocalDate date); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 53bbade0fe6..10019ff6520 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -170,9 +170,13 @@ public void setAppointmentList() { } @Override - public void clearAppointments(LocalDate date) { - addressBook.clearAppointments(date); - setAppointmentList(); + public boolean clearAppointments(LocalDate date) { + boolean isCleared = addressBook.clearAppointments(date); + if (isCleared) { + setAppointmentList(); + } + + return isCleared; } @Override diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index 73a6105f14f..39e217db587 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -150,6 +150,10 @@ public Person clearAppointment() { tags, NullAppointment.getNullAppointment()); } + public boolean hasNullAppointment() { + return appointment.equals(NullAppointment.getNullAppointment()); + } + /** * Returns true if both persons have the same identity and data fields. * This defines a stronger notion of equality between two persons. diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index 55d93253354..76879758d30 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -118,12 +118,16 @@ public String gatherEmails(GatherEmailPrompt prompt) { /** * Clears all appointments of {@code Person} in {@code persons} that match the given {@code LocalDate date}. */ - public void clearAppointments(LocalDate date) { + public boolean clearAppointments(LocalDate date) { + boolean isCleared = false; for (Person person: internalList) { if (person.isSameAppointmentDate(date)) { setPerson(person, person.clearAppointment()); + isCleared = true; } } + + return isCleared; } /** diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 6918ca6850c..58082052f75 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -179,7 +179,7 @@ public String gatherEmails(GatherEmailPrompt prompt) { } @Override - public void clearAppointments(LocalDate date) { + public boolean clearAppointments(LocalDate date) { throw new AssertionError("This method should not be called."); } } From 787b144fe11a0ef486247d309148e4e9b88af447 Mon Sep 17 00:00:00 2001 From: Kb-tay Date: Thu, 2 Nov 2023 17:40:36 +0800 Subject: [PATCH 2/3] Add tests for Complete Command. Command should show error when date input has no matching appointments or when person indexed has no appointment --- .../address/logic/commands/SortCommand.java | 2 -- .../logic/commands/CompleteCommandTest.java | 28 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java index 2bcfd472bdb..e13574ce262 100644 --- a/src/main/java/seedu/address/logic/commands/SortCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -10,8 +10,6 @@ import seedu.address.model.person.Person; - - /** * Sorts all persons in the list by lexicographical order barring capitalisation. */ diff --git a/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java b/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java index 2922064d107..410e8ef71a3 100644 --- a/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java @@ -2,8 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse; 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.CompleteCommand.MESSAGE_COMPLETE_SUCCESS; +import static seedu.address.logic.commands.CompleteCommand.MESSAGE_DATE_NO_APPOINTMENT; +import static seedu.address.logic.commands.CompleteCommand.MESSAGE_PERSON_NO_APPOINTMENT; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.BENSON; @@ -11,9 +14,12 @@ import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import java.time.LocalDate; +import java.util.logging.Logger; import org.junit.jupiter.api.Test; +import seedu.address.commons.core.LogsCenter; +import seedu.address.logic.LogicManager; import seedu.address.logic.commands.CompleteCommand.CompleteDescriptor; import seedu.address.model.AddressBook; import seedu.address.model.Model; @@ -45,8 +51,8 @@ public void execute_dateSpecified_success() { Person editedPerson2 = new PersonBuilder(CARL).withNullAppointment().build(); String expectedMessage = MESSAGE_COMPLETE_SUCCESS; CompleteDescriptor completeDescriptor = new CompleteDescriptor(); - CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); completeDescriptor.setDate(LocalDate.of(2023, 05, 01)); + CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); expectedModel.setPerson(model.getFilteredPersonList().get(1), editedPerson1); @@ -55,6 +61,26 @@ public void execute_dateSpecified_success() { assertCommandSuccess(completeCommand, model, expectedMessage, expectedModel); } + @Test + public void execute_noMatchingDate_failure() { + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + CompleteDescriptor completeDescriptor = new CompleteDescriptor(); + completeDescriptor.setDate(LocalDate.of(2023, 02,10)); //no matching appointment date + CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); + + assertCommandFailure(completeCommand, expectedModel, MESSAGE_DATE_NO_APPOINTMENT); + } + + @Test + public void execute_personNoAppointment_failure() { + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + CompleteDescriptor completeDescriptor = new CompleteDescriptor(); + completeDescriptor.setIndex(INDEX_FIRST_PERSON); //person has no appointment + CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); + + assertCommandFailure(completeCommand, expectedModel, MESSAGE_PERSON_NO_APPOINTMENT); + } + @Test public void equals() { CompleteDescriptor completeDescriptor1 = new CompleteDescriptor(); From 163358811fbbe2a17bac190b1dca3426380ab72a Mon Sep 17 00:00:00 2001 From: Kb-tay Date: Thu, 2 Nov 2023 17:43:13 +0800 Subject: [PATCH 3/3] Fix checkstyle --- .../seedu/address/logic/commands/CompleteCommandTest.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java b/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java index 410e8ef71a3..e25f9de5ebd 100644 --- a/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CompleteCommandTest.java @@ -14,12 +14,9 @@ import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import java.time.LocalDate; -import java.util.logging.Logger; import org.junit.jupiter.api.Test; -import seedu.address.commons.core.LogsCenter; -import seedu.address.logic.LogicManager; import seedu.address.logic.commands.CompleteCommand.CompleteDescriptor; import seedu.address.model.AddressBook; import seedu.address.model.Model; @@ -65,7 +62,7 @@ public void execute_dateSpecified_success() { public void execute_noMatchingDate_failure() { Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); CompleteDescriptor completeDescriptor = new CompleteDescriptor(); - completeDescriptor.setDate(LocalDate.of(2023, 02,10)); //no matching appointment date + completeDescriptor.setDate(LocalDate.of(2023, 02, 10)); //no matching appointment date CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); assertCommandFailure(completeCommand, expectedModel, MESSAGE_DATE_NO_APPOINTMENT); @@ -75,7 +72,7 @@ public void execute_noMatchingDate_failure() { public void execute_personNoAppointment_failure() { Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); CompleteDescriptor completeDescriptor = new CompleteDescriptor(); - completeDescriptor.setIndex(INDEX_FIRST_PERSON); //person has no appointment + completeDescriptor.setIndex(INDEX_FIRST_PERSON); //person has no appointment CompleteCommand completeCommand = new CompleteCommand(completeDescriptor); assertCommandFailure(completeCommand, expectedModel, MESSAGE_PERSON_NO_APPOINTMENT);