diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 24707ecf687a..d37e59a42efa 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -17,6 +17,7 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.appointment.Appointment; import seedu.address.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.address.model.appointment.exceptions.DuplicateDateTimeException; import seedu.address.model.person.Nric; import seedu.address.model.person.Person; import seedu.address.model.person.exceptions.DuplicateNricException; @@ -114,6 +115,7 @@ public class AddCommand extends UndoableCommand { public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in Medeina."; public static final String MESSAGE_DUPLICATE_NRIC = "This is already someone with this NRIC."; public static final String MESSAGE_DUPLICATE_APPOINTMENT = "This particular appointment already exists in Medeina."; + public static final String MESSAGE_DUPLICATE_DATETIME = "This date time is already taken by another appointment."; public static final String MESSAGE_DUPLICATE_PET_PATIENT = "This pet patient already exists in Medeina"; public static final String MESSAGE_INVALID_NRIC = "The specified NRIC does not belong to anyone in Medeina." + " Please add a new person."; @@ -204,6 +206,8 @@ public CommandResult executeUndoableCommand() throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PET_PATIENT); } catch (DuplicateAppointmentException e) { throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT); + } catch (DuplicateDateTimeException e) { + throw new CommandException(MESSAGE_DUPLICATE_DATETIME); } } @@ -228,7 +232,8 @@ private CommandResult addNewPetPatient() throws DuplicatePetPatientException, Co /** * Add a new appointment for an existing pet patient under an existing person. */ - private CommandResult addNewAppt() throws CommandException, DuplicateAppointmentException { + private CommandResult addNewAppt() + throws CommandException, DuplicateAppointmentException, DuplicateDateTimeException { Person owner = getPersonWithNric(); PetPatient pet = getPetPatientWithNricAndName(); if (owner != null) { @@ -253,7 +258,7 @@ private CommandResult addNewAppt() throws CommandException, DuplicateAppointment * (New appointment for the new patient under a new person). */ private CommandResult addAllNew() throws DuplicatePersonException, DuplicateNricException, - DuplicatePetPatientException, DuplicateAppointmentException { + DuplicatePetPatientException, DuplicateAppointmentException, DuplicateDateTimeException { model.addPerson(toAddOwner); model.addPetPatient(toAddPet); model.addAppointment(toAddAppt); diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 5115433f0e91..5281abea9602 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -18,6 +18,7 @@ import seedu.address.model.appointment.exceptions.AppointmentDependencyNotEmptyException; import seedu.address.model.appointment.exceptions.AppointmentNotFoundException; import seedu.address.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.address.model.appointment.exceptions.DuplicateDateTimeException; import seedu.address.model.person.Person; import seedu.address.model.person.UniquePersonList; import seedu.address.model.person.exceptions.DuplicateNricException; @@ -76,7 +77,8 @@ public void setTags(Set tags) { this.tags.setTags(tags); } - public void setAppointments(List appointments) throws DuplicateAppointmentException { + public void setAppointments(List appointments) + throws DuplicateAppointmentException, DuplicateDateTimeException { this.appointments.setAppointments(appointments); } @@ -109,6 +111,8 @@ public void resetData(ReadOnlyAddressBook newData) { try { setAppointments(syncedAppointmentList); } catch (DuplicateAppointmentException dae) { + throw new AssertionError("AddressBook should not have duplicate appointments."); + } catch (DuplicateDateTimeException ddte) { throw new AssertionError("AddressBook should not have appointments on the same slot"); } @@ -169,7 +173,7 @@ public void updatePerson(Person target, Person editedPerson) * * @throws DuplicateAppointmentException if an equivalent person already exists. */ - public void addAppointment(Appointment a) throws DuplicateAppointmentException { + public void addAppointment(Appointment a) throws DuplicateAppointmentException, DuplicateDateTimeException { Appointment appointment = syncWithAppointmentMasterTagList(a); // TODO: the tags master list will be updated even though the below line fails. // This can cause the tags master list to have additional tags that are not tagged to any appointment diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 1dbb644240ad..47a635b68cbd 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -7,6 +7,7 @@ import seedu.address.model.appointment.exceptions.AppointmentDependencyNotEmptyException; import seedu.address.model.appointment.exceptions.AppointmentNotFoundException; import seedu.address.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.address.model.appointment.exceptions.DuplicateDateTimeException; import seedu.address.model.person.Person; import seedu.address.model.person.exceptions.DuplicateNricException; import seedu.address.model.person.exceptions.DuplicatePersonException; @@ -56,7 +57,7 @@ void updatePerson(Person target, Person editedPerson) void deleteTag(Tag tag); /** Adds the given appointment */ - void addAppointment(Appointment appointment) throws DuplicateAppointmentException; + void addAppointment(Appointment appointment) throws DuplicateAppointmentException, DuplicateDateTimeException; /** Deletes the given appointment. */ void deleteAppointment(Appointment target) throws AppointmentNotFoundException; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index a8d45b51ac71..12e482bab0d5 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -16,6 +16,7 @@ import seedu.address.model.appointment.exceptions.AppointmentDependencyNotEmptyException; import seedu.address.model.appointment.exceptions.AppointmentNotFoundException; import seedu.address.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.address.model.appointment.exceptions.DuplicateDateTimeException; import seedu.address.model.person.Person; import seedu.address.model.person.exceptions.DuplicateNricException; import seedu.address.model.person.exceptions.DuplicatePersonException; @@ -131,7 +132,8 @@ public synchronized void deleteAppointment(Appointment target) throws Appointmen } @Override - public synchronized void addAppointment(Appointment appointment) throws DuplicateAppointmentException { + public synchronized void addAppointment(Appointment appointment) + throws DuplicateAppointmentException, DuplicateDateTimeException { addressBook.addAppointment(appointment); updateFilteredAppointmentList(PREDICATE_SHOW_ALL_APPOINTMENTS); indicateAddressBookChanged(); diff --git a/src/main/java/seedu/address/model/appointment/UniqueAppointmentList.java b/src/main/java/seedu/address/model/appointment/UniqueAppointmentList.java index 846781f911b3..95cc113bfedf 100644 --- a/src/main/java/seedu/address/model/appointment/UniqueAppointmentList.java +++ b/src/main/java/seedu/address/model/appointment/UniqueAppointmentList.java @@ -10,6 +10,7 @@ import javafx.collections.ObservableList; import seedu.address.model.appointment.exceptions.AppointmentNotFoundException; import seedu.address.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.address.model.appointment.exceptions.DuplicateDateTimeException; /** * A list of appointments that enforces uniqueness between its elements and does not allow nulls. @@ -35,11 +36,17 @@ public boolean contains(Appointment toCheck) { * * @throws DuplicateAppointmentException if the person to add is a duplicate of an existing person in the list. */ - public void add(Appointment toAdd) throws DuplicateAppointmentException { + public void add(Appointment toAdd) throws DuplicateAppointmentException, DuplicateDateTimeException { requireNonNull(toAdd); if (contains(toAdd)) { throw new DuplicateAppointmentException(); } + + for (Appointment a : internalList) { + if (a.getDateTime().equals(toAdd.getDateTime())) { + throw new DuplicateDateTimeException(); + } + } internalList.add(toAdd); } @@ -84,7 +91,8 @@ public void setAppointments(UniqueAppointmentList replacement) { this.internalList.setAll(replacement.internalList); } - public void setAppointments(List appointments) throws DuplicateAppointmentException { + public void setAppointments(List appointments) + throws DuplicateAppointmentException, DuplicateDateTimeException { requireAllNonNull(appointments); final UniqueAppointmentList replacement = new UniqueAppointmentList(); for (final Appointment appointment : appointments) { diff --git a/src/main/java/seedu/address/model/appointment/exceptions/DuplicateAppointmentException.java b/src/main/java/seedu/address/model/appointment/exceptions/DuplicateAppointmentException.java index c224529c211a..89890ced67b2 100644 --- a/src/main/java/seedu/address/model/appointment/exceptions/DuplicateAppointmentException.java +++ b/src/main/java/seedu/address/model/appointment/exceptions/DuplicateAppointmentException.java @@ -7,6 +7,6 @@ */ public class DuplicateAppointmentException extends DuplicateDataException { public DuplicateAppointmentException() { - super("Operation would result in duplicate persons"); + super("Operation would result in duplicate appointments"); } } diff --git a/src/main/java/seedu/address/model/appointment/exceptions/DuplicateDateTimeException.java b/src/main/java/seedu/address/model/appointment/exceptions/DuplicateDateTimeException.java new file mode 100644 index 000000000000..71e79a3beb71 --- /dev/null +++ b/src/main/java/seedu/address/model/appointment/exceptions/DuplicateDateTimeException.java @@ -0,0 +1,12 @@ +package seedu.address.model.appointment.exceptions; + +import seedu.address.commons.exceptions.DuplicateDataException; + +/** + * Signals that the operation will result in double booking. + */ +public class DuplicateDateTimeException extends DuplicateDataException { + public DuplicateDateTimeException() { + super("Operation would result in multiple bookings in the same time slot"); + } +}