Skip to content

Commit

Permalink
Add method to check for personId existing in address book
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidansani committed Nov 6, 2024
1 parent 5b5fca9 commit ecef01f
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> 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);
}

/**
Expand All @@ -73,11 +88,7 @@ protected boolean alreadyExists(Model model) {
*/
@Override
protected void addEntity(Model model) throws CommandException {
Optional<Person> personOptional = model.findPerson(personId);
if (personOptional.isEmpty()) {
throw new CommandException(getPersonIdDoesNotExistMessage());
}
model.addAppointment(personOptional.get(), appointmentDescriptor);
model.addAppointment(getPerson(model), appointmentDescriptor);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -134,6 +133,11 @@ public Optional<Person> findPerson(int personId) {
return persons.findPerson(personId);
}

@Override
public boolean hasPersonWithPersonId(int personId) {
return persons.hasPersonWithPersonId(personId);
}

//// util methods

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public interface Model {
*/
Optional<Person> 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.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public Optional<Person> 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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public interface ReadOnlyAddressBook {
*/
Optional<Person> findPerson(int personId);

/**
* Returns true if personId exists in the person observable list
*/
boolean hasPersonWithPersonId(int personId);

/**
* Returns the next person ID
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ public Optional<Person> 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}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ public Optional<Person> 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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public Optional<Person> 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.");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/seedu/address/model/AddressBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public Optional<Person> 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();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public Optional<Person> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public Optional<Person> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public Optional<Person> 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();
Expand Down

0 comments on commit ecef01f

Please sign in to comment.