Skip to content

Commit b8cc855

Browse files
authored
Merge pull request #85 from Kappaccinoh/delete-appointment-recursively
Delete appointment recursively
2 parents 2fba314 + 165bab7 commit b8cc855

File tree

6 files changed

+138
-14
lines changed

6 files changed

+138
-14
lines changed

src/main/java/seedu/address/logic/commands/DeleteCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class DeleteCommand extends Command {
2121

2222
public static final String MESSAGE_USAGE = COMMAND_WORD
2323
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
24+
+ "*Note that this also deletes all appointments associated with the person in the appointments list"
2425
+ "Parameters: INDEX (must be a positive integer)\n"
2526
+ "Example: " + COMMAND_WORD + " 1";
2627

src/main/java/seedu/address/model/AddressBook.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,20 @@ public void setAppointment(Appointment target, Appointment editedAppointment) {
126126
}
127127

128128
/**
129-
* Removes {@code key} from this {@code AddressBook}.
129+
* Removes {@code key} from this {@code AddressBook} and removes all appointments
130+
* which involve Person {@code key}, both patient and doctor.
130131
* {@code key} must exist in the address book.
131132
*/
132133
public void removePerson(Person key) {
134+
List<Appointment> appointmentsToDelete = getAppointmentByPerson(key);
135+
appointmentsToDelete.forEach(appointments::remove);
133136
persons.remove(key);
134137
}
135138

139+
public List<Appointment> getAppointmentByPerson(Person person) {
140+
return appointments.contains(person);
141+
}
142+
136143
public void addAppointment(Appointment appointment) {
137144
appointments.add(appointment);
138145
}

src/main/java/seedu/address/model/appointment/Appointment.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import seedu.address.commons.util.ToStringBuilder;
99
import seedu.address.model.person.Nric;
10+
import seedu.address.model.person.Person;
1011

1112
/**
1213
* Appointment class that describes an appointment
@@ -68,7 +69,7 @@ public Appointment(Nric doctorNric, Nric patientNric, AppointmentDate appointmen
6869
* @param appointmentDate Date to check validity of
6970
* @return boolean if appointment is valid or not
7071
*/
71-
private boolean isValidAppointment(AppointmentDate appointmentDate) {
72+
public boolean isValidAppointment(AppointmentDate appointmentDate) {
7273
AppointmentDate currentDate = new AppointmentDate(LocalDate.now());
7374
return appointmentDate.compareTo(currentDate) > -1;
7475
}
@@ -117,6 +118,18 @@ public boolean isSameAppointment(Appointment appt) {
117118
&& appt.getAppointmentDate().equals(this.getAppointmentDate());
118119
}
119120

121+
/**
122+
* Checks if the given {@code Person} is associated with this appointment either as a doctor or a patient.
123+
*
124+
* @param person The {@code Person} to check if associated with this appointment.
125+
* @return {@code true} if the person's NRIC matches either the doctor's NRIC or the patient's NRIC,
126+
* {@code false} otherwise.
127+
*/
128+
public boolean appointmentContainsPerson(Person person) {
129+
return person.getNric().equals(this.doctorNric)
130+
|| person.getNric().equals(this.patientNric);
131+
}
132+
120133
@Override
121134
public boolean equals(Object appt) {
122135
if (appt == this) {

src/main/java/seedu/address/model/appointment/UniqueAppointmentList.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import java.util.Iterator;
77
import java.util.List;
8+
import java.util.stream.Collectors;
89

910
import javafx.collections.FXCollections;
1011
import javafx.collections.ObservableList;
1112
import seedu.address.model.appointment.exceptions.AppointmentNotFoundException;
1213
import seedu.address.model.appointment.exceptions.DuplicateAppointmentException;
14+
import seedu.address.model.person.Person;
1315

1416
/**
1517
* A list of appointments that enforces uniqueness between its elements and does not allow nulls.
@@ -29,16 +31,28 @@ public class UniqueAppointmentList implements Iterable<Appointment> {
2931
FXCollections.unmodifiableObservableList(internalList);
3032

3133
/**
32-
* Returns true if the list contains an equivalent person as the given argument.
34+
* Returns true if the list contains an equivalent appointment as the given argument.
3335
*/
3436
public boolean contains(Appointment toCheck) {
3537
requireNonNull(toCheck);
3638
return internalList.stream().anyMatch(toCheck::isSameAppointment);
3739
}
3840

3941
/**
40-
* Adds a person to the list.
41-
* The person must not already exist in the list.
42+
* Returns a list of appointments if the appointment contains the person. Checked via person's NRIC
43+
* @param person target person
44+
* @return list of appointments
45+
*/
46+
public List<Appointment> contains(Person person) {
47+
requireNonNull(person);
48+
return internalList.stream()
49+
.filter(appointment -> appointment.appointmentContainsPerson(person))
50+
.collect(Collectors.toList());
51+
}
52+
53+
/**
54+
* Adds an appointment to the list.
55+
* The appointment must not already exist in the list.
4256
*/
4357
public void add(Appointment toAdd) {
4458
requireNonNull(toAdd);
@@ -49,9 +63,10 @@ public void add(Appointment toAdd) {
4963
}
5064

5165
/**
52-
* Replaces the person {@code target} in the list with {@code editedPerson}.
66+
* Replaces the appointment {@code target} in the list with {@code editedAppointment}.
5367
* {@code target} must exist in the list.
54-
* The person identity of {@code editedPerson} must not be the same as another existing person in the list.
68+
* The appointment details of {@code editedAppointment} must not be the same as another
69+
* existing appointment in the list.
5570
*/
5671
public void setAppointment(Appointment target, Appointment editedAppointment) {
5772
requireAllNonNull(target, editedAppointment);
@@ -69,8 +84,8 @@ public void setAppointment(Appointment target, Appointment editedAppointment) {
6984
}
7085

7186
/**
72-
* Removes the equivalent person from the list.
73-
* The person must exist in the list.
87+
* Removes the equivalent appointment from the list.
88+
* The appointment must exist in the list.
7489
*/
7590
public void remove(Appointment toRemove) {
7691
requireNonNull(toRemove);
@@ -85,8 +100,8 @@ public void setPersons(UniqueAppointmentList replacement) {
85100
}
86101

87102
/**
88-
* Replaces the contents of this list with {@code persons}.
89-
* {@code persons} must not contain duplicate persons.
103+
* Replaces the contents of this list with {@code appointments}.
104+
* {@code appointments} must not contain duplicate appointments.
90105
*/
91106
public void setAppointments(List<Appointment> appointments) {
92107
requireAllNonNull(appointments);
@@ -135,7 +150,7 @@ public String toString() {
135150
}
136151

137152
/**
138-
* Returns true if {@code persons} contains only unique persons.
153+
* Returns true if {@code appointments} contains only unique appointments.
139154
*/
140155
private boolean appointmentsAreUnique(List<Appointment> appointments) {
141156
for (int i = 0; i < appointments.size() - 1; i++) {
Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,118 @@
11
package seedu.address.model.appointment;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.time.LocalDate;
9+
310
import org.junit.jupiter.api.Test;
411

12+
import seedu.address.model.person.Nric;
13+
import seedu.address.model.person.Person;
14+
import seedu.address.testutil.DoctorBuilder;
15+
import seedu.address.testutil.PatientBuilder;
16+
517
class AppointmentTest {
618

719
@Test
8-
void getDoctoNric() {
20+
void getDoctorNric() {
21+
Nric doctorNric = new Nric("S1234567A");
22+
Appointment appointment = new Appointment(doctorNric, new Nric("T1234567A"),
23+
new AppointmentDate(LocalDate.now()));
24+
assertEquals(doctorNric, appointment.getDoctorNric());
925
}
1026

1127
@Test
1228
void getPatientNric() {
29+
Nric patientNric = new Nric("T1234567A");
30+
Appointment appointment = new Appointment(new Nric("S1234567A"), patientNric,
31+
new AppointmentDate(LocalDate.now()));
32+
assertEquals(patientNric, appointment.getPatientNric());
1333
}
1434

1535
@Test
1636
void getAppointmentId() {
37+
AppointmentId appointmentId = new AppointmentId();
38+
Appointment appointment = new Appointment(new Nric("S1234567A"), new Nric("T1234567A"),
39+
new AppointmentDate(LocalDate.now()), appointmentId);
40+
assertEquals(appointmentId, appointment.getAppointmentId());
1741
}
1842

1943
@Test
2044
void getAppointmentDate() {
45+
AppointmentDate appointmentDate = new AppointmentDate(LocalDate.now());
46+
Appointment appointment = new Appointment(new Nric("S1234567A"), new Nric("T1234567A"), appointmentDate);
47+
assertEquals(appointmentDate, appointment.getAppointmentDate());
48+
}
49+
50+
@Test
51+
void isValidAppointment_validDate_returnsTrue() {
52+
AppointmentDate futureDate = new AppointmentDate(LocalDate.now().plusDays(1));
53+
Appointment appointment = new Appointment(new Nric("S1234567A"), new Nric("T1234567A"), futureDate);
54+
55+
assertTrue(appointment.isValidAppointment(futureDate));
56+
}
57+
58+
@Test
59+
void isValidAppointment_pastDate_returnsFalse() {
60+
AppointmentDate pastDate = new AppointmentDate(LocalDate.now().minusDays(1));
61+
// Use assertThrows to check if IllegalArgumentException is thrown
62+
assertThrows(IllegalArgumentException.class, () -> {
63+
new Appointment(new Nric("S1234567A"), new Nric("T1234567A"), pastDate);
64+
});
2165
}
2266

2367
@Test
2468
void isSameAppointment() {
69+
Nric doctorNric = new Nric("S1234567A");
70+
Nric patientNric = new Nric("T1234567A");
71+
AppointmentDate appointmentDate = new AppointmentDate(LocalDate.now());
72+
Appointment appointment1 = new Appointment(doctorNric, patientNric, appointmentDate);
73+
Appointment appointment2 = new Appointment(doctorNric, patientNric, appointmentDate);
74+
assertTrue(appointment1.isSameAppointment(appointment2));
75+
}
76+
77+
@Test
78+
void appointmentContainsPerson() {
79+
Nric doctorNric = new Nric("S1234567A");
80+
Nric patientNric = new Nric("T1234567A");
81+
AppointmentDate appointmentDate = new AppointmentDate(LocalDate.now());
82+
Appointment appointment = new Appointment(doctorNric, patientNric, appointmentDate);
83+
84+
// False Doctor and Patient
85+
Person doctor = new DoctorBuilder().withNric("S7654321A").build();
86+
Person patient = new PatientBuilder().withNric("T7654321A").build();
87+
assertFalse(appointment.appointmentContainsPerson(doctor));
88+
assertFalse(appointment.appointmentContainsPerson(patient));
89+
90+
// True Doctor and Patient
91+
doctor = new DoctorBuilder().withNric("S1234567A").build();
92+
patient = new PatientBuilder().withNric("T1234567A").build();
93+
assertTrue(appointment.appointmentContainsPerson(doctor));
94+
assertTrue(appointment.appointmentContainsPerson(patient));
95+
}
96+
97+
@Test
98+
void testEquals() {
99+
Nric doctorNric = new Nric("S1234567A");
100+
Nric patientNric = new Nric("T1234567A");
101+
AppointmentDate appointmentDate = new AppointmentDate(LocalDate.now());
102+
Appointment appointment1 = new Appointment(doctorNric, patientNric, appointmentDate);
103+
Appointment appointment2 = new Appointment(doctorNric, patientNric, appointmentDate);
104+
assertEquals(appointment1, appointment2);
25105
}
26106

27107
@Test
28108
void testToString() {
109+
Nric doctorNric = new Nric("S1234567A");
110+
Nric patientNric = new Nric("T1234567A");
111+
AppointmentDate appointmentDate = new AppointmentDate(LocalDate.now());
112+
AppointmentId appointmentId = new AppointmentId();
113+
Appointment appointment = new Appointment(doctorNric, patientNric, appointmentDate, appointmentId);
114+
String expectedString = "seedu.address.model.appointment.Appointment{Date=" + appointmentDate + ", Doctor="
115+
+ doctorNric + ", Patient=" + patientNric + ", Id=" + appointmentId + "}";
116+
assertEquals(expectedString, appointment.toString());
29117
}
30118
}

src/test/java/seedu/address/model/appointment/UniqueAppointmentListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class UniqueAppointmentListTest {
2424

2525
@Test
2626
public void contains_nullAppointment_throwsNullPointerException() {
27-
assertThrows(NullPointerException.class, () -> uniqueAppointmentList.contains(null));
27+
assertThrows(NullPointerException.class, () -> uniqueAppointmentList.contains((Appointment) null));
2828
}
2929

3030
@Test

0 commit comments

Comments
 (0)