-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.java
More file actions
110 lines (96 loc) · 3.98 KB
/
Copy pathDataManager.java
File metadata and controls
110 lines (96 loc) · 3.98 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
// DataManager.java - Handles file operations
package vhs;
import java.io.*;
import java.util.*;
public class DataManager {
private static final String USERS_FILE = "users.dat";
private static final String APPOINTMENTS_FILE = "appointments.dat";
private static final String EHR_FILE = "ehr.dat";
// Save all users to file
public static void saveUsers(List<User> users) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(USERS_FILE))) {
oos.writeObject(users);
System.out.println("Users saved successfully: " + users.size() + " records");
}
}
// Load users from file
public static List<User> loadUsers() throws IOException, ClassNotFoundException {
File file = new File(USERS_FILE);
if (!file.exists()) {
return new ArrayList<>();
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(USERS_FILE))) {
return (List<User>) ois.readObject();
}
}
// Save appointments
public static void saveAppointments(List<Appointment> appointments) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(APPOINTMENTS_FILE))) {
oos.writeObject(appointments);
System.out.println("Appointments saved: " + appointments.size());
}
}
// Updated: Adding new feature commits
// Load appointments
public static List<Appointment> loadAppointments() throws IOException, ClassNotFoundException {
File file = new File(APPOINTMENTS_FILE);
if (!file.exists()) {
return new ArrayList<>();
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(APPOINTMENTS_FILE))) {
return (List<Appointment>) ois.readObject();
}
}
// Search users by ID
public static User findUserById(String id) {
try {
List<User> users = loadUsers();
for (User user : users) {
if (user.getId().equals(id)) {
return user;
}
}
} catch (Exception e) {
System.err.println("Error searching user: " + e.getMessage());
}
return null;
}
// Search appointments by patient ID
public static List<Appointment> findAppointmentsByPatientId(String patientId) {
List<Appointment> result = new ArrayList<>();
try {
List<Appointment> appointments = loadAppointments();
for (Appointment app : appointments) {
if (app.getPatient().getId().equals(patientId)) {
result.add(app);
}
}
} catch (Exception e) {
System.err.println("Error searching appointments: " + e.getMessage());
}
return result;
}
// Export data to text file (for backup)
public static void exportDataToText(String filename) throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println("=== VIRTUAL HEALTHCARE SYSTEM DATA EXPORT ===");
writer.println("Export Date: " + new Date());
writer.println();
try {
List<User> users = loadUsers();
writer.println("USERS (" + users.size() + "):");
for (User user : users) {
writer.println(" " + user);
}
writer.println();
List<Appointment> appointments = loadAppointments();
writer.println("APPOINTMENTS (" + appointments.size() + "):");
for (Appointment app : appointments) {
writer.println(" " + app);
}
} catch (Exception e) {
writer.println("Error loading data: " + e.getMessage());
}
}
}
}