-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppointmentManager.java
More file actions
60 lines (49 loc) · 1.87 KB
/
Copy pathAppointmentManager.java
File metadata and controls
60 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.time.LocalDate;
public class AppointmentManager {
private HashSet<Appointment> appointments;
// Constructor
public AppointmentManager() {
this.appointments = new HashSet<>();
}
// Add an appointment
public void add(Appointment appointment) {
if (appointments.contains(appointment)) {
throw new IllegalArgumentException("Appointment exists!");
}
appointments.add(appointment);
}
// Delete an appointment
public void delete(Appointment appointment) {
if (!appointments.contains(appointment)) {
throw new IllegalArgumentException("Appointment not found!");
}
appointments.remove(appointment);
}
// Update an appointment
public void update(Appointment current, Appointment modified) {
delete(current); // Remove the current appointment
add(modified); // Add the modified appointment
}
// Get sorted appointments
public List<Appointment> getSortedAppointments() {
List<Appointment> sortedAppointments = new ArrayList<>(appointments);
sortedAppointments.sort(null); // Uses the compareTo method for sorting
return sortedAppointments;
}
// Get appointments on a specific date with optional comparator
public Appointment[] getAppointmentsOn(LocalDate date, Comparator<Appointment> comparator) {
List<Appointment> filteredAppointments = new ArrayList<>();
for (Appointment a : appointments) {
if (date == null || a.occursOn(date)) {
filteredAppointments.add(a);
}
}
if (comparator != null) {
filteredAppointments.sort(comparator);
} else {
filteredAppointments.sort(null); // Use default sorting
}
return filteredAppointments.toArray(new Appointment[0]);
}
}