-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationService.java
More file actions
113 lines (91 loc) · 4.43 KB
/
Copy pathNotificationService.java
File metadata and controls
113 lines (91 loc) · 4.43 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package vhs;
import javax.swing.JOptionPane;
import java.util.*;
import java.text.SimpleDateFormat;
public class NotificationService {
private List<Appointment> appointments;
private User currentUser; // Added: current user reference
// Constructor with appointments only
public NotificationService(List<Appointment> appointments) {
this.appointments = appointments;
this.currentUser = null; // Initially null, should be set based on context
}
// Alternative constructor with current user
public NotificationService(List<Appointment> appointments, User currentUser) {
this.appointments = appointments;
this.currentUser = currentUser;
}
// Method to set current user if not in constructor
public void setCurrentUser(User currentUser) {
this.currentUser = currentUser;
}
public void checkAndSendReminders() {
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
// Check appointments in next 24 hours
calendar.add(Calendar.HOUR, 24);
Date tomorrow = calendar.getTime();
for (Appointment appointment : appointments) {
if (appointment.getStatus().equals("Scheduled")) {
Date appTime = appointment.getDateTime();
// Send reminder 24 hours before
if (appTime.after(now) && appTime.before(tomorrow)) {
sendReminder(appointment);
}
// Send reminder 1 hour before
calendar.setTime(now);
calendar.add(Calendar.HOUR, 1);
Date oneHourFromNow = calendar.getTime();
if (appTime.after(now) && appTime.before(oneHourFromNow)) {
sendUrgentReminder(appointment);
}
}
}
}
private void sendReminder(Appointment appointment) {
String message = String.format(
"REMINDER: You have an appointment with %s on %s at %s",
appointment.getProvider().getName(),
new SimpleDateFormat("yyyy-MM-dd").format(appointment.getDateTime()),
new SimpleDateFormat("HH:mm").format(appointment.getDateTime())
);
// In real system: Send email/SMS
System.out.println("Notification sent to " + appointment.getPatient().getName() + ": " + message);
// For GUI: Show dialog (only for current user)
if (currentUser != null && currentUser.getId().equals(appointment.getPatient().getId())) {
JOptionPane.showMessageDialog(null, message, "Appointment Reminder", JOptionPane.INFORMATION_MESSAGE);
}
}
private void sendUrgentReminder(Appointment appointment) {
String message = String.format(
"URGENT: Your appointment with %s is in 1 hour (%s)",
appointment.getProvider().getName(),
new SimpleDateFormat("HH:mm").format(appointment.getDateTime())
);
System.out.println("URGENT notification: " + message);
if (currentUser != null && currentUser.getId().equals(appointment.getPatient().getId())) {
JOptionPane.showMessageDialog(null, message, "Urgent Reminder", JOptionPane.WARNING_MESSAGE);
}
}
public void sendPrescriptionRenewalReminder(Prescription prescription) {
Date expiry = prescription.getExpiryDate();
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(expiry);
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date weekBeforeExpiry = calendar.getTime();
if (now.after(weekBeforeExpiry) && now.before(expiry)) {
String message = String.format(
"PRESCRIPTION RENEWAL: Your prescription for %s expires on %s",
prescription.getMedication(),
new SimpleDateFormat("yyyy-MM-dd").format(expiry)
);
System.out.println("Prescription renewal reminder: " + message);
// Optional: Show GUI notification for current user if they're the patient
if (currentUser != null && currentUser.getId().equals(prescription.getPatient().getId())) {
JOptionPane.showMessageDialog(null, message, "Prescription Renewal", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}