From ecef01f2317ae6c0e0093564c76e3cf928d1606c Mon Sep 17 00:00:00 2001 From: zaidansani Date: Thu, 7 Nov 2024 01:09:29 +0800 Subject: [PATCH] Add method to check for personId existing in address book --- .../logic/commands/AddAppointmentCommand.java | 23 ++++++++++++++----- .../java/seedu/address/model/AddressBook.java | 6 ++++- src/main/java/seedu/address/model/Model.java | 5 ++++ .../seedu/address/model/ModelManager.java | 5 ++++ .../address/model/ReadOnlyAddressBook.java | 5 ++++ .../model/person/UniquePersonList.java | 9 ++++++++ .../commands/AddAppointmentCommandTest.java | 5 ++++ .../logic/commands/AddPersonCommandTest.java | 5 ++++ .../seedu/address/model/AddressBookTest.java | 6 +++++ .../seedu/address/model/ModelManagerTest.java | 15 ++++++++++++ .../storage/JsonAdaptedAppointmentTest.java | 6 +++++ .../JsonAppointmentBookStorageTest.java | 6 +++++ .../JsonSerializableAppointmentBookTest.java | 6 +++++ 13 files changed, 95 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java index f830f7a88db..e6c7d1cdffc 100644 --- a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java @@ -62,7 +62,22 @@ public AddAppointmentCommand(AppointmentDescriptor appointmentDescriptor, int pe */ @Override protected boolean alreadyExists(Model model) { - return model.hasAppointment(appointmentDescriptor); + return model.hasAppointment(appointmentDescriptor) && checkPersonIdExists(model); + } + + /** + * Gets the person and throws exception if it does not exist. + */ + protected Person getPerson(Model model) throws CommandException { + Optional personOptional = model.findPerson(personId); + return personOptional.orElseThrow(() -> new CommandException(getPersonIdDoesNotExistMessage())); + } + + /** + * Checks if personId exists inside model + */ + protected boolean checkPersonIdExists(Model model) { + return model.hasPersonWithPersonId(personId); } /** @@ -73,11 +88,7 @@ protected boolean alreadyExists(Model model) { */ @Override protected void addEntity(Model model) throws CommandException { - Optional personOptional = model.findPerson(personId); - if (personOptional.isEmpty()) { - throw new CommandException(getPersonIdDoesNotExistMessage()); - } - model.addAppointment(personOptional.get(), appointmentDescriptor); + model.addAppointment(getPerson(model), appointmentDescriptor); } /** diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 6c5949a342a..7e68d443932 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -89,7 +89,6 @@ public boolean hasPerson(PersonDescriptor personDescriptor) { return persons.contains(personDescriptor); } - /** * Adds a person to the address book. * The person must not already exist in the address book. @@ -134,6 +133,11 @@ public Optional findPerson(int personId) { return persons.findPerson(personId); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return persons.hasPersonWithPersonId(personId); + } + //// util methods @Override diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 98f8096a8c9..8b6d47d09d7 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -74,6 +74,11 @@ public interface Model { */ Optional findPerson(int personId); + /** + * Checks if the model has a person with the personId + */ + boolean hasPersonWithPersonId(int personId); + /** * Deletes the given person. * The person must exist in the address book. diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index edf62c30ca1..a7f8ca78eca 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -139,6 +139,11 @@ public Optional findPerson(int personId) { return addressBook.findPerson(personId); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return addressBook.hasPersonWithPersonId(personId); + } + @Override public Person addPerson(PersonDescriptor personDescriptor) { Person person = addressBook.addPerson(personDescriptor); diff --git a/src/main/java/seedu/address/model/ReadOnlyAddressBook.java b/src/main/java/seedu/address/model/ReadOnlyAddressBook.java index 2f7ab79877a..379777507e7 100644 --- a/src/main/java/seedu/address/model/ReadOnlyAddressBook.java +++ b/src/main/java/seedu/address/model/ReadOnlyAddressBook.java @@ -22,6 +22,11 @@ public interface ReadOnlyAddressBook { */ Optional findPerson(int personId); + /** + * Returns true if personId exists in the person observable list + */ + boolean hasPersonWithPersonId(int personId); + /** * Returns the next person ID */ diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index 5ee3f608b08..5e7db6c9c5c 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -114,6 +114,15 @@ public Optional findPerson(int personId) { .filter(person -> person.getPersonId() == personId) .findFirst(); } + + /** + * Finds the person with corresponding person ID, if exists. + */ + public boolean hasPersonWithPersonId(int personId) { + return internalList.stream() + .anyMatch(person -> person.getPersonId() == personId); + } + /** * Returns the backing list as an unmodifiable {@code ObservableList}. */ diff --git a/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java index dc9db90bf6a..1bae8e78590 100644 --- a/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java @@ -213,6 +213,11 @@ public Optional findPerson(int personId) { return Optional.of(BENSON_P); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return true; + } + @Override public void deleteAppointment(Appointment target) { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java b/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java index d1747d1fa96..e5f47531ebd 100644 --- a/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java @@ -204,6 +204,11 @@ public Optional findPerson(int personId) { throw new AssertionError("This method should not be called."); } + @Override + public boolean hasPersonWithPersonId(int personId) { + throw new AssertionError("This method should not be called."); + } + @Override public void deleteAppointment(Appointment target) { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/model/AddressBookTest.java b/src/test/java/seedu/address/model/AddressBookTest.java index 3b44d724036..b836c5020a8 100644 --- a/src/test/java/seedu/address/model/AddressBookTest.java +++ b/src/test/java/seedu/address/model/AddressBookTest.java @@ -114,6 +114,12 @@ public Optional findPerson(int personId) { .findFirst(); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return persons.stream() + .anyMatch(person -> person.getPersonId() == personId); + } + @Override public int getNextPersonId() { return persons.size(); diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index 1cff6bbd5d4..80dc45f4b77 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -92,6 +92,21 @@ public void hasPerson_personInAddressBook_returnsTrue() { assertTrue(modelManager.hasPerson(ALICE)); } + @Test + public void hasPersonWithPersonId_returnsFalse() { + // EP: invalid personId value + assertFalse(modelManager.hasPersonWithPersonId(-1)); + // EP: personId not existing in book + assertFalse(modelManager.hasPersonWithPersonId(1000)); + } + + @Test + public void hasPersonWithPersonId_returnsTrue() { + // EP: has person inside + modelManager.addPerson(ALICE); + assertTrue(modelManager.hasPersonWithPersonId(0)); + } + @Test public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException() { assertThrows(UnsupportedOperationException.class, () -> modelManager.getFilteredPersonList().remove(0)); diff --git a/src/test/java/seedu/address/storage/JsonAdaptedAppointmentTest.java b/src/test/java/seedu/address/storage/JsonAdaptedAppointmentTest.java index e1eb3519d01..4e9794d7bdc 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedAppointmentTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedAppointmentTest.java @@ -159,6 +159,12 @@ public Optional findPerson(int personId) { .findFirst(); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return persons.stream() + .anyMatch(person -> person.getPersonId() == personId); + } + @Override public int getNextPersonId() { return persons.size(); diff --git a/src/test/java/seedu/address/storage/JsonAppointmentBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAppointmentBookStorageTest.java index 8ab930d76a5..77098410fd4 100644 --- a/src/test/java/seedu/address/storage/JsonAppointmentBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAppointmentBookStorageTest.java @@ -110,6 +110,12 @@ public Optional findPerson(int personId) { .findFirst(); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return persons.stream() + .anyMatch(person -> person.getPersonId() == personId); + } + @Override public int getNextPersonId() { return persons.size(); diff --git a/src/test/java/seedu/address/storage/JsonSerializableAppointmentBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableAppointmentBookTest.java index c7f8facaaef..8af618f4d35 100644 --- a/src/test/java/seedu/address/storage/JsonSerializableAppointmentBookTest.java +++ b/src/test/java/seedu/address/storage/JsonSerializableAppointmentBookTest.java @@ -105,6 +105,12 @@ public Optional findPerson(int personId) { .findFirst(); } + @Override + public boolean hasPersonWithPersonId(int personId) { + return persons.stream() + .anyMatch(person -> person.getPersonId() == personId); + } + @Override public int getNextPersonId() { return persons.size();