diff --git a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java index 24beb79b6e5..ae9cc7aa53a 100644 --- a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java @@ -11,6 +11,7 @@ import seedu.address.model.Model; import seedu.address.model.appointment.Appointment; import seedu.address.model.appointment.exceptions.InvalidAppointmentException; +import seedu.address.model.person.exceptions.PersonNotFoundException; /** @@ -50,9 +51,12 @@ public CommandResult execute(Model model) throws CommandException { if (model.hasAppointment(toAdd)) { throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT); } - - if (!model.isValidAppointment(toAdd)) { - throw new InvalidAppointmentException(); + try { + if (!model.isValidAppointment(toAdd)) { + throw new InvalidAppointmentException(); + } + } catch (PersonNotFoundException e) { + throw new CommandException("The provided Doctor / Patient is not registered in the system"); } model.addAppointment(toAdd); diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 4856b4ab480..bb1a3537305 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -12,6 +12,7 @@ import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.model.person.UniquePersonList; +import seedu.address.model.person.exceptions.PersonNotFoundException; /** * Wraps all data at the address-book level @@ -87,7 +88,7 @@ public boolean hasPerson(Person person) { return persons.contains(person); } - public Person getPersonByNric(Nric nricObj) { + public Person getPersonByNric(Nric nricObj) throws PersonNotFoundException { requireNonNull(nricObj); return persons.getPersonByNric(nricObj); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 23cd912f9df..c04a138c957 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -15,6 +15,7 @@ import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.model.person.Type; +import seedu.address.model.person.exceptions.PersonNotFoundException; /** * Represents the in-memory model of the address book data. @@ -189,10 +190,9 @@ public boolean equals(Object other) { * @param appointment appointment to check validity of * @return boolean indicating if appointment is valid */ - public boolean isValidAppointment(Appointment appointment) { + public boolean isValidAppointment(Appointment appointment) throws PersonNotFoundException { Nric doctorNric = appointment.getDoctorNric(); Nric patientNric = appointment.getPatientNric(); - Person doctor = addressBook.getPersonByNric(doctorNric); Person patient = addressBook.getPersonByNric(patientNric); diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index 414ebb515dd..d810b02770c 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -160,7 +160,7 @@ private boolean personsAreUnique(List persons) { return true; } - public Person getPersonByNric(Nric nricObj) { + public Person getPersonByNric(Nric nricObj) throws PersonNotFoundException { ArrayList personList = new ArrayList(internalList); for (Person p : personList) { diff --git a/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java index 4a022807203..796ac41cf5a 100644 --- a/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java @@ -30,6 +30,13 @@ void execute_validCommand_executesCommand() throws CommandException, ParseExcept assertTrue(modelManager.getFilteredAppointmentList().size() == 1); } + @Test + void execute_invalidCommand_missingPerson() throws CommandException, ParseException { + Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01")); + AddAppointmentCommand ad = new AddAppointmentCommand(appt); + assertThrows(CommandException.class, () -> ad.execute(modelManager)); + } + @Test void execute_invalidCommand_throwsInvalidAppointmentException() throws ParseException { modelManager.addPerson(ALICE);