-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeTable.java
279 lines (233 loc) · 12.2 KB
/
TimeTable.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.*;
import java.io.Serializable;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class TimeTable implements Serializable {
private Map<String, List<Class>> classes;
private Map<String, List<TimeSlot>> teacherSchedule;
private static final String[] DAYS = {"MON", "TUE", "WED", "THU", "FRI"};
private static final String[] ALL_TIME_SLOTS = {"08:00-09:00", "09:00-10:30", "10:45-12:15", "14:30-16:00", "16:00-17:30"};
private static final String[] REGULAR_TIME_SLOTS = {"09:00-10:30", "10:45-12:15", "14:30-16:00", "16:00-17:30"};
private static final String[] LAB_SLOTS = {"14:30-16:30"};
public TimeTable() {
classes = new HashMap<>();
teacherSchedule = new HashMap<>();
}
public boolean isEmpty() {
return classes.isEmpty();
}
public void generateTimetableImage(String batchName, String outputPath) {
TimetableImageGenerator.generateTimetableImage(classes.get(batchName), batchName, outputPath);
}
public boolean isSlotFree(TimeSlot newSlot, String batchName, String facultyId) {
if (classes.containsKey(batchName)) {
for (Class cls : classes.get(batchName)) {
if (cls.getTimeSlot().getDay().equals(newSlot.getDay()) &&
isTimeOverlap(cls.getTimeSlot(), newSlot)) {
return false;
}
}
}
if (teacherSchedule.containsKey(facultyId)) {
for (TimeSlot existingSlot : teacherSchedule.get(facultyId)) {
if (existingSlot.getDay().equals(newSlot.getDay()) &&
isTimeOverlap(existingSlot, newSlot)) {
return false;
}
}
}
return true;
}
private boolean isTimeOverlap(TimeSlot slot1, TimeSlot slot2) {
int start1 = timeToMinutes(slot1.getStartTime());
int end1 = timeToMinutes(slot1.getEndTime());
int start2 = timeToMinutes(slot2.getStartTime());
int end2 = timeToMinutes(slot2.getEndTime());
return (start1 < end2 && start2 < end1);
}
private int timeToMinutes(String time) {
String[] parts = time.split(":");
return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
}
public boolean addClass(Class newClass, String branch) {
String batchName = newClass.getBatchName();
String facultyId = newClass.getCourse().getEligibleFacultyIds().get(0);
if (isSubjectScheduledForDay(batchName, newClass.getCourse().getCourseCode(), newClass.getTimeSlot().getDay())) {
return false;
}
if (isSlotFree(newClass.getTimeSlot(), batchName, facultyId)) {
classes.computeIfAbsent(batchName, k -> new ArrayList<>()).add(newClass);
teacherSchedule.computeIfAbsent(facultyId, k -> new ArrayList<>()).add(newClass.getTimeSlot());
return true;
}
return false;
}
private boolean isSubjectScheduledForDay(String batchName, String courseCode, String day) {
if (classes.containsKey(batchName)) {
for (Class cls : classes.get(batchName)) {
if (cls.getTimeSlot().getDay().equals(day) && cls.getCourse().getCourseCode().equals(courseCode)) {
return true;
}
}
}
return false;
}
public void displayTimetable(String batchName) {
// Calculate padding for centering batch name
String titleText = "Timetable for " + batchName;
int totalWidth = 97; // Total width of the table
int padding = (totalWidth - titleText.length()) / 2;
String centeredTitle = String.format("%" + padding + "s%s%" + padding + "s", "", titleText, "");
System.out.println("\n╔═════════════════════════════════════════════════════════════════════════════════════════════════╗");
System.out.println("║" + centeredTitle + "║");
System.out.println("╠═════════════════════════════════════════════════════════════════════════════════════════════════╣");
System.out.println("║ Time │ Monday │ Tuesday │ Wednesday │ Thursday │ Friday ║");
System.out.println("╠══════════════╪════════════╪════════════╪════════════╪════════════╪════════════╣");
boolean hasMinorCourses = classes.get(batchName).stream().anyMatch(cls -> cls.getCourse().getCourseType().equalsIgnoreCase("minor"));
String[] timeSlots = hasMinorCourses ? ALL_TIME_SLOTS : REGULAR_TIME_SLOTS;
for (String timeSlot : timeSlots) {
System.out.printf("║ %-12s │", timeSlot);
for (String day : DAYS) {
boolean slotFilled = false;
for (Class cls : classes.getOrDefault(batchName, Collections.emptyList())) {
if (cls.getTimeSlot().getDay().equals(day) &&
isTimeOverlap(cls.getTimeSlot(), new TimeSlot(day, timeSlot.split("-")[0], timeSlot.split("-")[1]))) {
System.out.printf(" %-10s │", cls.getCourse().getCourseCode());
slotFilled = true;
break;
}
}
if (!slotFilled) {
System.out.print(" │");
}
}
System.out.println();
}
System.out.println("╚══════════════╧════════════╧════════════╧════════════╧════════════╧════════════╝");
// Print course details with improved formatting
System.out.println("\nCourse Details:");
System.out.println("═══════════════");
String format = "%-10s │ %-35s │ %-10s │ %-10s │ %-15s │ %-15s │ %-15s\n";
System.out.printf(format, "Code", "Name", "Type", "Credits", "Branch/Section", "Hours", "Faculty IDs");
System.out.println("─".repeat(120));
Set<String> displayedCourses = new HashSet<>();
for (Class cls : classes.getOrDefault(batchName, Collections.emptyList())) {
Course course = cls.getCourse();
if (!displayedCourses.contains(course.getCourseCode()) && !course.getCourseCode().equals("LUNCH")) {
displayedCourses.add(course.getCourseCode());
System.out.printf(format,
course.getCourseCode(),
course.getName(),
course.getCourseType(),
course.getCredits(),
course.getBranch() + "/" + course.getSection(),
String.format("L-%d T-%d P-%d", course.getLecture(), course.getTheory(), course.getPractical()),
String.join(", ", course.getEligibleFacultyIds())
);
}
}
System.out.println("\nMinor Courses:");
System.out.println("═══════════════");
boolean minorCoursesFound = false;
Set<String> displayedMinorCourses = new HashSet<>();
for (Class cls : classes.getOrDefault(batchName, Collections.emptyList())) {
Course course = cls.getCourse();
if (course.getCourseType().equalsIgnoreCase("minor") &&
!displayedMinorCourses.contains(course.getCourseCode())) {
displayedMinorCourses.add(course.getCourseCode());
System.out.printf("%-10s │ %-30s │ %-20s\n",
course.getCourseCode(),
course.getName(),
cls.getTimeSlot()
);
minorCoursesFound = true;
}
}
if (!minorCoursesFound) {
System.out.println("No minor courses found for this batch.");
}
}
public List<TimeSlot> findFreeSlots(String batchName, String day) {
List<TimeSlot> freeSlots = new ArrayList<>();
List<TimeSlot> occupiedSlots = new ArrayList<>();
for (Class cls : classes.getOrDefault(batchName, Collections.emptyList())) {
if (cls.getTimeSlot().getDay().equals(day)) {
occupiedSlots.add(cls.getTimeSlot());
}
}
occupiedSlots.sort(Comparator.comparing(TimeSlot::getStartTime));
String[] standardTimes = {"09:00", "10:45", "12:15", "14:30", "16:00", "17:30"};
for (int i = 0; i < standardTimes.length - 1; i++) {
TimeSlot potentialSlot = new TimeSlot(day, standardTimes[i], standardTimes[i+1]);
boolean isFree = true;
for (TimeSlot occupiedSlot : occupiedSlots) {
if (isTimeOverlap(potentialSlot, occupiedSlot)) {
isFree = false;
break;
}
}
if (isFree) {
freeSlots.add(potentialSlot);
}
}
return freeSlots;
}
public void generateCSV(String batchName, String outputPath) {
try (FileWriter csvWriter = new FileWriter(outputPath)) {
csvWriter.append("Day,Time,Course Code,Course Name,Room,Faculty\n");
Map<String, Map<String, Class>> sortedClasses = new TreeMap<>();
for (Class cls : classes.getOrDefault(batchName, Collections.emptyList())) {
sortedClasses
.computeIfAbsent(cls.getTimeSlot().getDay(), k -> new TreeMap<>())
.put(cls.getTimeSlot().getStartTime(), cls);
}
boolean hasMinorCourses = classes.get(batchName).stream().anyMatch(cls -> cls.getCourse().getCourseType().equalsIgnoreCase("minor"));
String[] timeSlots = hasMinorCourses ? ALL_TIME_SLOTS : REGULAR_TIME_SLOTS;
for (String day : DAYS) {
Map<String, Class> dayClasses = sortedClasses.getOrDefault(day, Collections.emptyMap());
for (String timeSlot : timeSlots) {
String startTime = timeSlot.split("-")[0];
Class cls = dayClasses.get(startTime);
if (cls != null) {
csvWriter.append(String.format("%s,%s,%s,%s,%s,%s\n",
day,
timeSlot,
cls.getCourse().getCourseCode(),
cls.getCourse().getName(),
cls.getClassroom(),
String.join(";", cls.getCourse().getEligibleFacultyIds())
));
} else {
csvWriter.append(String.format("%s,%s,,,,\n", day, timeSlot));
}
}
}
System.out.println("CSV file has been generated successfully: " + outputPath);
} catch (IOException e) {
System.out.println("Error generating CSV file: " + e.getMessage());
}
}
public void scheduleLunch() {
Random random = new Random();
for (String batchName : classes.keySet()) {
String day = DAYS[random.nextInt(DAYS.length)];
int lunchHour = 12 + random.nextInt(2);
int lunchMinute = 30 + random.nextInt(2) * 30;
String lunchStart = "12:30";
String lunchEnd = String.format("%02d:%02d", lunchHour, lunchMinute);
TimeSlot lunchSlot = new TimeSlot(day, lunchStart, lunchEnd);
Course lunchCourse = new Course("LUNCH", "LUNCH", "Lunch Break", "BREAK", "", "", 0, 0, 0, "", 0, new ArrayList<>());
Class lunchClass = new Class(batchName, "CANTEEN", lunchCourse, lunchSlot, false);
classes.get(batchName).add(lunchClass);
}
}
}