Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T15-1#110 from alfaloo/debug
Browse files Browse the repository at this point in the history
Fix appointment bug
  • Loading branch information
officialchengyud authored Apr 3, 2024
2 parents 107a1a5 + b8de4a7 commit f9f8204
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private boolean personsAreUnique(List<Person> persons) {
return true;
}

public Person getPersonByNric(Nric nricObj) {
public Person getPersonByNric(Nric nricObj) throws PersonNotFoundException {
ArrayList<Person> personList = new ArrayList<Person>(internalList);

for (Person p : personList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f9f8204

Please sign in to comment.