Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T15-1#109 from ararchch/branch-Ca…
Browse files Browse the repository at this point in the history
…tchAppointmentErrors

Branch catch appointment errors
  • Loading branch information
alfaloo authored Apr 3, 2024
2 parents a5e0995 + 246e4e9 commit 107a1a5
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public CommandResult execute(Model model) throws CommandException {
}

Appointment appointmentToDelete = lastShownList.get(targetIndex.getZeroBased());

model.deleteAppointment(appointmentToDelete);

return new CommandResult(String.format(MESSAGE_DELETE_APPOINTMENT_SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.appointment.AppointmentDate;
Expand Down Expand Up @@ -62,9 +63,9 @@ public CommandResult execute(Model model) throws CommandException {
}

Appointment appointmentToEdit = lastShownList.get(index.getZeroBased());
Appointment editedAppointment = createEditedAppointment(appointmentToEdit, editAppointmentDescriptor);

if (!appointmentToEdit.isSameAppointment(editedAppointment) && model.hasAppointment(editedAppointment)) {
Appointment editedAppointment = createEditedAppointment(appointmentToEdit, editAppointmentDescriptor);
if (appointmentToEdit.isSameAppointment(editedAppointment) && model.hasAppointment(editedAppointment)) {
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT);
}

Expand All @@ -77,16 +78,21 @@ public CommandResult execute(Model model) throws CommandException {
* Creates and returns a {@code Appointment} with the details of {@code appointmentToEdit}
* edited with {@code editAppointmentDescriptor}.
*/
private static Appointment createEditedAppointment(Appointment appointmentToEdit,
EditAppointmentDescriptor editAppointmentDescriptor) {
private static Appointment createEditedAppointment(
Appointment appointmentToEdit,
EditAppointmentDescriptor editAppointmentDescriptor) throws CommandException {
assert appointmentToEdit != null;

Nric doctorNric = appointmentToEdit.getDoctorNric();
Nric patientNric = appointmentToEdit.getPatientNric();
AppointmentDate updatedDate = editAppointmentDescriptor
.getDate().orElse(appointmentToEdit.getAppointmentDate());

return new Appointment(doctorNric, patientNric, updatedDate);
try {
return new Appointment(doctorNric, patientNric, updatedDate);
} catch (ParseException e) {
throw new CommandException("Unable to edit appointment due to invalid inputs");
}
}

@Override
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.appointment.UniqueAppointmentList;
import seedu.address.model.appointment.exceptions.DuplicateAppointmentException;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
Expand Down Expand Up @@ -58,17 +59,22 @@ public void setPersons(List<Person> persons) {
* Replaces the contents of the appointment list with {@code appointments}.
* {@code appointments} must not contain duplicate appointments.
*/
public void setAppointments(List<Appointment> appointments) {
public void setAppointments(List<Appointment> appointments) throws DuplicateAppointmentException {
this.appointments.setAppointments(appointments);
}

public void setAppointmentsExistingBook(List<Appointment> appointments) {
this.appointments.setAppointmentsExistingBook(appointments);
}

/**
* Resets the existing data of this {@code AddressBook} with {@code newData}.
*/
public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);
setPersons(newData.getPersonList());
setAppointments(newData.getAppointmentList());

setAppointmentsExistingBook(newData.getAppointmentList());
}

//// person-level operations
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public void setPerson(Person target, Person editedPerson) {
@Override
public void setAppointment(Appointment target, Appointment editedAppointment) {
requireAllNonNull(target, editedAppointment);

addressBook.setAppointment(target, editedAppointment);
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/model/appointment/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalDate;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -36,9 +37,16 @@ public class Appointment {
* @param patientNric patient of the appointment
* @param appointmentDate date of the appointment
*/
public Appointment(Nric doctorNric, Nric patientNric, AppointmentDate appointmentDate) {
public Appointment(
Nric doctorNric, Nric patientNric, AppointmentDate appointmentDate) throws ParseException {
requireAllNonNull(doctorNric, patientNric, appointmentDate);
checkArgument(isValidAppointment(appointmentDate), MESSAGE_CONSTRAINTS_INVALID_DATE);

try {
checkArgument(isValidAppointment(appointmentDate), MESSAGE_CONSTRAINTS_INVALID_DATE);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}

this.doctorNric = doctorNric;
this.patientNric = patientNric;
this.appointmentDate = appointmentDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public AppointmentDate(LocalDate date) {
*/
public static boolean isValidDate(String dateStr) {
try {
LocalDate.parse(dateStr);
LocalDate temp = LocalDate.parse(dateStr);
//LocalDate today = LocalDate.now();
//return temp.isAfter(today);
} catch (DateTimeParseException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.appointment.exceptions.AppointmentNotFoundException;
import seedu.address.model.appointment.exceptions.DuplicateAppointmentException;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -56,9 +55,6 @@ public List<Appointment> contains(Person person) {
*/
public void add(Appointment toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
throw new DuplicateAppointmentException();
}
internalList.add(toAdd);
}

Expand All @@ -72,13 +68,6 @@ public void setAppointment(Appointment target, Appointment editedAppointment) {
requireAllNonNull(target, editedAppointment);

int index = internalList.indexOf(target);
if (index == -1) {
throw new AppointmentNotFoundException();
}

if (!target.isSameAppointment(editedAppointment) && contains(editedAppointment)) {
throw new DuplicateAppointmentException();
}

internalList.set(index, editedAppointment);
}
Expand All @@ -89,9 +78,10 @@ public void setAppointment(Appointment target, Appointment editedAppointment) {
*/
public void remove(Appointment toRemove) {
requireNonNull(toRemove);
if (!internalList.remove(toRemove)) {
throw new AppointmentNotFoundException();
}
// if (!internalList.remove(toRemove)) {
// throw new AppointmentNotFoundException();
// }
internalList.remove(toRemove);
}

public void setPersons(UniqueAppointmentList replacement) {
Expand All @@ -103,7 +93,7 @@ public void setPersons(UniqueAppointmentList replacement) {
* Replaces the contents of this list with {@code appointments}.
* {@code appointments} must not contain duplicate appointments.
*/
public void setAppointments(List<Appointment> appointments) {
public void setAppointments(List<Appointment> appointments) throws DuplicateAppointmentException {
requireAllNonNull(appointments);
if (!appointmentsAreUnique(appointments)) {
throw new DuplicateAppointmentException();
Expand All @@ -112,6 +102,11 @@ public void setAppointments(List<Appointment> appointments) {
internalList.setAll(appointments);
}

public void setAppointmentsExistingBook(List<Appointment> appointments) {
requireAllNonNull(appointments);
internalList.setAll(appointments);
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package seedu.address.model.appointment.exceptions;

import seedu.address.logic.commands.exceptions.CommandException;

/**
* Signals that the operation is unable to find the specified person.
*/
public class AppointmentNotFoundException extends RuntimeException {
public class AppointmentNotFoundException extends CommandException {
public AppointmentNotFoundException() {
super("Unable to locate appointment");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package seedu.address.model.appointment.exceptions;

import seedu.address.logic.commands.exceptions.CommandException;

/**
* Signals that the operation will result in duplicate Persons (Persons are considered duplicates if they have the same
* identity).
*/
public class DuplicateAppointmentException extends RuntimeException {
public class DuplicateAppointmentException extends CommandException {
public DuplicateAppointmentException() {
super("Operation would result in duplicate appointments");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package seedu.address.model.appointment.exceptions;

import seedu.address.logic.commands.exceptions.CommandException;

/**
* Signals that the operation is unable to find the specified person.
*/
public class InvalidAppointmentException extends RuntimeException {
public class InvalidAppointmentException extends CommandException {

public InvalidAppointmentException() {
super("This appointment is invalid due to invalid inputs.");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void getFilteredAppointmentList_modifyList_throwsUnsupportedOperationExce
}

@Test
public void getAppointmentList_getList_listIsNotNull() {
public void getAppointmentList_getList_listIsNotNull() throws ParseException {
model.addPerson(ALICE);
model.addPerson(BROWN);
model.addAppointment(new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-11-11")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ModelManager;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.appointment.AppointmentDate;
Expand All @@ -20,7 +21,7 @@ class AddAppointmentCommandTest {
private ModelManager modelManager = new ModelManager();

@Test
void execute_validCommand_executesCommand() throws CommandException {
void execute_validCommand_executesCommand() throws CommandException, ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -30,7 +31,7 @@ void execute_validCommand_executesCommand() throws CommandException {
}

@Test
void execute_invalidCommand_throwsInvalidAppointmentException() {
void execute_invalidCommand_throwsInvalidAppointmentException() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(ALICE.getNric(), BROWN.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -39,7 +40,7 @@ void execute_invalidCommand_throwsInvalidAppointmentException() {
}

@Test
void execute_invalidCommandAppointmentExists_throwsCommandException() {
void execute_invalidCommandAppointmentExists_throwsCommandException() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -49,7 +50,7 @@ void execute_invalidCommandAppointmentExists_throwsCommandException() {
}

@Test
void equals_sameCommandButDifferentObject_returnsTrue() {
void equals_sameCommandButDifferentObject_returnsTrue() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -59,7 +60,7 @@ void equals_sameCommandButDifferentObject_returnsTrue() {
}

@Test
void equals_sameCommandSameObject_returnsTrue() {
void equals_sameCommandSameObject_returnsTrue() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -68,7 +69,7 @@ void equals_sameCommandSameObject_returnsTrue() {
}

@Test
void equals_differentClass_returnsFalse() {
void equals_differentClass_returnsFalse() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand All @@ -77,7 +78,7 @@ void equals_differentClass_returnsFalse() {
}

@Test
void toString_returnsValidString() {
void toString_returnsValidString() throws ParseException {
modelManager.addPerson(ALICE);
modelManager.addPerson(BROWN);
Appointment appt = new Appointment(BROWN.getNric(), ALICE.getNric(), new AppointmentDate("2024-09-01"));
Expand Down
Loading

0 comments on commit 107a1a5

Please sign in to comment.