-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.java
More file actions
174 lines (154 loc) · 6.57 KB
/
DataManager.java
File metadata and controls
174 lines (154 loc) · 6.57 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package s25.cs151.application.model;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class DataManager {
private static final String DATA_FILE = "office_hours.dat"; // File to store office hours data
private static final String TIMESLOTS_FILE = "timeslots.dat";
private static final String COURSES_FILE = "courses.dat";
private static final String SCHEDULE_FILE = "schedule_entries.dat";
// Save office hours data to file
public static void saveOfficeHours(SemesterOfficeHours data) {
List<SemesterOfficeHours> allData = loadAllOfficeHours(); // Retrieve existing data
allData.add(data); // Add new data to the list
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(DATA_FILE))) {
oos.writeObject(allData); //Serialize and write data to file
} catch (IOException e) {
e.printStackTrace(); //Print error details if writing fails
}
}
// Load all saved office hours data from the file, if file is not found--empty list returned.
@SuppressWarnings("unchecked")
public static List<SemesterOfficeHours> loadAllOfficeHours() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(DATA_FILE))) {
return (List<SemesterOfficeHours>) ois.readObject(); // Deserialize and return data
} catch (FileNotFoundException e) {
return new ArrayList<>(); // Return an empty list if no file exists
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace(); // Print error details if reading fails
return new ArrayList<>();
}
}
public static void saveTimeslot(Timeslot timeslot) {
List<Timeslot> allTimeslots = loadAllTimeslots();
allTimeslots.add(timeslot);
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(TIMESLOTS_FILE))) {
oos.writeObject(allTimeslots);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static List<Timeslot> loadAllTimeslots() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(TIMESLOTS_FILE))) {
return (List<Timeslot>) ois.readObject();
} catch (FileNotFoundException e) {
return new ArrayList<>();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return new ArrayList<>();
}
}
/**
* C@onverts a boolean array representing selected days into a formatted string.
* Example: {true, false, true, false, true} -> "Monday, Wednesday, Friday"
*
* @param daysSelected A boolean array where each index represents a day (Monday-Friday).
* @return A formatted string listing the selected days.
*/
private String convertDaysToString(boolean[] daysSelected) {
String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
StringBuilder days = new StringBuilder();
for (int i = 0; i < daysSelected.length; i++) {
if (daysSelected[i]) {
if (days.length() > 0) {
days.append(", "); // Add a comma seperator if multiple days are selected
}
days.append(dayNames[i]); //Append the selected day
}
}
return days.toString(); // Return the formatted String
}
public static void saveCourse(Course course) {
List<Course> allCourses = loadAllCourses();
allCourses.add(course);
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(COURSES_FILE))) {
oos.writeObject(allCourses);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static List<Course> loadAllCourses() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(COURSES_FILE))) {
return (List<Course>) ois.readObject();
} catch (FileNotFoundException e) {
return new ArrayList<>();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return new ArrayList<>();
}
}
public static void clearAllOfficeHours() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(DATA_FILE))) {
oos.writeObject(new ArrayList<>()); // Write empty list to file
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveScheduleEntries(List<ScheduleEntry> entries) {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(SCHEDULE_FILE))) {
oos.writeObject(entries);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Failed to save schedule entries", e);
}
}
@SuppressWarnings("unchecked")
public static List<ScheduleEntry> loadScheduleEntries() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(SCHEDULE_FILE))) {
return (List<ScheduleEntry>) ois.readObject();
} catch (FileNotFoundException e) {
return new ArrayList<>();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Failed to load schedule entries", e);
}
}
// Add clear method for timeslots
public static void clearAllTimeslots() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(TIMESLOTS_FILE))) {
oos.writeObject(new ArrayList<>()); // Write empty list
} catch (IOException e) {
e.printStackTrace();
}
}
// Add clear method for courses
public static void clearAllCourses() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(COURSES_FILE))) {
oos.writeObject(new ArrayList<>()); // Write empty list
} catch (IOException e) {
e.printStackTrace();
}
}
// Add this new method for clearing schedule entries
public static void clearAllScheduleEntries() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(SCHEDULE_FILE))) {
oos.writeObject(new ArrayList<>());
} catch (IOException e) {
e.printStackTrace();
}
}
}