-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.java
More file actions
1324 lines (1130 loc) · 51.6 KB
/
Copy pathtempCodeRunnerFile.java
File metadata and controls
1324 lines (1130 loc) · 51.6 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TimetableApp.java
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TimetableApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new LoginFrame());
}
}
// LoginFrame.java
// LoginFrame.java
class LoginFrame extends JFrame {
public LoginFrame() {
setTitle("Login");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Clear student timetable file on startup
clearStudentTimetableFile();
JPanel panel = new JPanel(new GridLayout(4, 1));
JButton adminButton = new JButton("Admin Login");
JButton studentButton = new JButton("Student Login");
JButton facultyButton = new JButton("Faculty Login");
panel.add(adminButton);
panel.add(studentButton);
panel.add(facultyButton);
adminButton.addActionListener(e -> {
dispose();
new MainFrame();
});
studentButton.addActionListener(e -> {
dispose();
new StudentFrame();
});
facultyButton.addActionListener(e -> {
dispose();
new FacultyFrame();
});
add(panel);
setVisible(true);
}
// Clear student timetable file at startup
private void clearStudentTimetableFile() {
try {
new FileWriter("studentTT.csv", false).close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// MainFrame.java
class MainFrame extends JFrame {
public MainFrame() {
setTitle("Admin Panel");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JButton addButton = new JButton("Add Class");
JButton generateButton = new JButton("Generate Timetable");
addButton.addActionListener(e -> new AddClassFrame());
generateButton.addActionListener(e -> new TimetableViewFrame());
JPanel panel = new JPanel();
panel.add(addButton);
panel.add(generateButton);
add(panel);
setVisible(true);
}
}
// TimetableData.java
// Modified TimetableData.java addEntry method to handle tutorials
class TimetableData {
static final String FILE_PATH = "timetable.csv";
static final String FACULTY_PATH = "faculty.csv";
static final String COURSES_PATH = "courses.csv";
static final java.util.List<String> ROOMS = java.util.Arrays.asList(
"F102", "F103", "F104", "F105", "F106", // Large rooms (previously R1-R5)
"G101", "G102", "G103", "G104", "G105", // Small rooms (previously R6-R10)
"D311", "D313" // Lab rooms (previously L1, L2)
);
public static boolean addEntry(String room, String course, java.util.List<String> days,
java.util.List<Integer> times, boolean isLab, String faculty) throws IOException {
if (!ROOMS.contains(room)) return false;
// Validate room type - Labs can only be in lab rooms
if (isLab && !(room.equals("D311") || room.equals("D313"))) return false;
// Validate room type - Tutorials can only be in G101-G105
boolean isTutorial = !isLab && times.size() == 1 &&
(room.startsWith("G10") && Character.isDigit(room.charAt(3)));
java.util.List<String[]> entries = readEntries();
for (String[] entry : entries) {
for (int i = 0; i < days.size(); i++) {
String day = days.get(i);
int time = times.get(i);
// Check for room conflict
if (entry[0].equals(room) && entry[2].equals(day) && Integer.parseInt(entry[3]) == time) {
return false;
}
// Check for faculty conflict - faculty can't teach two classes at the same time
if (entry.length >= 6 && entry[5].equals(faculty) && entry[2].equals(day) &&
Integer.parseInt(entry[3]) == time) {
return false;
}
}
}
FileWriter fw = new FileWriter(FILE_PATH, true);
for (String day : days) {
for (int time : times) {
fw.write(String.join(",", room, course, day, String.valueOf(time),
String.valueOf(isLab), faculty) + "\n");
}
}
fw.close();
return true;
}
// Rest of the methods remain unchanged
public static java.util.List<String[]> readEntries() throws IOException {
java.util.List<String[]> data = new java.util.ArrayList<>();
File file = new File(FILE_PATH);
if (!file.exists()) return data;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
data.add(line.split(","));
}
br.close();
return data;
}
public static java.util.List<String> getFacultyList() throws IOException {
java.util.List<String> facultyList = new java.util.ArrayList<>();
File file = new File(FACULTY_PATH);
if (!file.exists()) return facultyList;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
facultyList.add(line.trim());
}
br.close();
return facultyList;
}
public static java.util.Map<String, String> getCourseMap() throws IOException {
java.util.Map<String, String> courseMap = new java.util.HashMap<>();
File file = new File(COURSES_PATH);
if (!file.exists()) return courseMap;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length == 2) {
courseMap.put(parts[0].trim(), parts[1].trim());
}
}
br.close();
return courseMap;
}
}
class AddClassFrame extends JFrame {
private final JComboBox<String> roomBox;
private final JCheckBox labBox;
private final JCheckBox tutorialBox; // New checkbox for tutorials
private final JTextField capacityField;
private final JTextField courseNameField;
public AddClassFrame() {
setTitle("Add Class");
setSize(400, 500); // Increased height for new field
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(10, 2)); // Increased grid size for new field
capacityField = new JTextField("0");
JButton updateRoomsButton = new JButton("Update Rooms");
roomBox = new JComboBox<>();
JComboBox<String> courseBox = new JComboBox<>();
courseNameField = new JTextField();
courseNameField.setEditable(false);
courseNameField.setBackground(new Color(240, 240, 240));
JComboBox<String> dayPatternBox = new JComboBox<>();
JComboBox<String> timeSlotBox = new JComboBox<>();
labBox = new JCheckBox("Lab Required");
tutorialBox = new JCheckBox("Tutorial Required"); // New checkbox
JComboBox<String> facultyBox = new JComboBox<>();
JButton submit = new JButton("Submit");
// Create a map to store course codes and names
final java.util.Map<String, String> coursesMap = new java.util.HashMap<>();
try {
for (String f : TimetableData.getFacultyList()) {
facultyBox.addItem(f);
}
java.util.Map<String, String> courses = TimetableData.getCourseMap();
for (String code : courses.keySet()) {
courseBox.addItem(code);
coursesMap.put(code, courses.get(code));
}
} catch (IOException ex) {
ex.printStackTrace();
}
// Add listener to update course name when course code is selected
courseBox.addActionListener(e -> {
String selectedCode = (String) courseBox.getSelectedItem();
if (selectedCode != null && coursesMap.containsKey(selectedCode)) {
courseNameField.setText(coursesMap.get(selectedCode));
} else {
courseNameField.setText("");
}
});
// Initialize with first course if available
if (courseBox.getItemCount() > 0) {
String firstCode = (String) courseBox.getItemAt(0);
if (coursesMap.containsKey(firstCode)) {
courseNameField.setText(coursesMap.get(firstCode));
}
}
// Create a panel for capacity field with update button
JPanel capacityPanel = new JPanel(new BorderLayout());
capacityPanel.add(capacityField, BorderLayout.CENTER);
capacityPanel.add(updateRoomsButton, BorderLayout.EAST);
panel.add(new JLabel("Number of Students:"));
panel.add(capacityPanel);
panel.add(new JLabel("Lab Required:"));
panel.add(labBox);
panel.add(new JLabel("Tutorial Required:")); // Add label for tutorial checkbox
panel.add(tutorialBox); // Add tutorial checkbox
panel.add(new JLabel("Room:"));
panel.add(roomBox);
panel.add(new JLabel("Course Code:"));
panel.add(courseBox);
panel.add(new JLabel("Course Name:"));
panel.add(courseNameField);
panel.add(new JLabel("Day Pattern:"));
panel.add(dayPatternBox);
panel.add(new JLabel("Time Slot:"));
panel.add(timeSlotBox);
panel.add(new JLabel("Faculty:"));
panel.add(facultyBox);
panel.add(new JLabel());
panel.add(submit);
// Update room options when the update button is clicked
updateRoomsButton.addActionListener(e -> updateRoomOptions());
// Update room options when lab or tutorial checkbox is clicked
labBox.addActionListener(e -> {
if (labBox.isSelected()) {
tutorialBox.setSelected(false); // Can't be both lab and tutorial
}
updateRoomOptions();
updateDayAndTimeOptions(dayPatternBox, timeSlotBox);
});
tutorialBox.addActionListener(e -> {
if (tutorialBox.isSelected()) {
labBox.setSelected(false); // Can't be both lab and tutorial
}
updateRoomOptions();
updateDayAndTimeOptions(dayPatternBox, timeSlotBox);
});
// Initial update of room options
updateRoomOptions();
// Initial update of day and time options
updateDayAndTimeOptions(dayPatternBox, timeSlotBox);
submit.addActionListener(e -> {
try {
if (roomBox.getSelectedItem() == null) {
JOptionPane.showMessageDialog(this, "Please select a room");
return;
}
String selectedDay = (String) dayPatternBox.getSelectedItem();
String selectedTimeSlot = (String) timeSlotBox.getSelectedItem();
java.util.List<String> days = new ArrayList<>();
java.util.List<Integer> times = new ArrayList<>();
// For labs and tutorials, we use single day patterns
if (labBox.isSelected() || tutorialBox.isSelected()) {
days.add(selectedDay);
// For labs, extract both time slots
if (labBox.isSelected()) {
if (selectedTimeSlot.equals("9-11 AM")) {
times.add(1);
times.add(2);
} else if (selectedTimeSlot.equals("11-1 PM")) {
times.add(3);
times.add(4);
} else if (selectedTimeSlot.equals("2-4 PM")) {
times.add(6);
times.add(7);
} else if (selectedTimeSlot.equals("4-6 PM")) {
times.add(8);
times.add(9);
}
}
// For tutorials, extract single time slot
else {
int timeSlot;
if (selectedTimeSlot.equals("8-9 AM")) {
timeSlot = 0;
} else if (selectedTimeSlot.equals("2-3 PM")) {
timeSlot = 6;
} else if (selectedTimeSlot.equals("3-4 PM")) {
timeSlot = 7;
} else if (selectedTimeSlot.equals("4-5 PM")) {
timeSlot = 8;
} else { // 5-6 PM
timeSlot = 9;
}
times.add(timeSlot);
}
}
// Regular classes with patterns
else {
String pattern = (String) dayPatternBox.getSelectedItem();
String timeString = (String) timeSlotBox.getSelectedItem();
String[] splitTimes = timeString.split("-");
switch (pattern) {
case "MON-WED-FRI":
days = Arrays.asList("MON", "WED", "FRI");
break;
case "TUE-THU-MON":
days = Arrays.asList("TUE", "THU", "MON");
break;
case "TUE-THU-WED":
days = Arrays.asList("TUE", "THU", "WED");
break;
case "TUE-THU-FRI":
days = Arrays.asList("TUE", "THU", "FRI");
break;
}
for (String t : splitTimes) {
times.add(Integer.parseInt(t));
}
}
boolean success = TimetableData.addEntry(
(String) roomBox.getSelectedItem(),
(String) courseBox.getSelectedItem(),
days,
times,
labBox.isSelected(),
(String) facultyBox.getSelectedItem()
);
if (success) {
JOptionPane.showMessageDialog(this, "Class Added Successfully");
dispose();
} else {
JOptionPane.showMessageDialog(this, "Clash Detected or Invalid Input");
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid number for capacity");
}
});
add(panel);
setVisible(true);
}
private void updateRoomOptions() {
roomBox.removeAllItems();
try {
int capacity = Integer.parseInt(capacityField.getText());
if (labBox.isSelected()) {
// Show only lab rooms
roomBox.addItem("D311");
roomBox.addItem("D313");
} else if (tutorialBox.isSelected()) {
// Show only tutorial rooms (G101-G105)
for (int i = 1; i <= 5; i++) {
roomBox.addItem("G10" + i);
}
} else {
// Show regular classrooms based on capacity
if (capacity <= 60) {
// Smaller rooms
for (int i = 1; i <= 5; i++) {
roomBox.addItem("G10" + i);
}
} else {
// Larger rooms
for (int i = 2; i <= 6; i++) {
roomBox.addItem("F10" + i);
}
}
}
} catch (NumberFormatException e) {
// Handle invalid capacity input
JOptionPane.showMessageDialog(this, "Please enter a valid number for capacity");
}
}
// New method to update day patterns and time slots based on selection
private void updateDayAndTimeOptions(JComboBox<String> dayPatternBox, JComboBox<String> timeSlotBox) {
dayPatternBox.removeAllItems();
timeSlotBox.removeAllItems();
// For labs: single day pattern, 2-hour time slots
if (labBox.isSelected()) {
dayPatternBox.addItem("MON");
dayPatternBox.addItem("TUE");
dayPatternBox.addItem("WED");
dayPatternBox.addItem("THU");
dayPatternBox.addItem("FRI");
// 2-hour lab slots
timeSlotBox.addItem("9-11 AM");
timeSlotBox.addItem("11-1 PM");
timeSlotBox.addItem("2-4 PM");
timeSlotBox.addItem("4-6 PM");
}
// For tutorials: single day pattern, 1-hour time slots
else if (tutorialBox.isSelected()) {
dayPatternBox.addItem("MON");
dayPatternBox.addItem("TUE");
dayPatternBox.addItem("WED");
dayPatternBox.addItem("THU");
dayPatternBox.addItem("FRI");
// 1-hour tutorial slots
timeSlotBox.addItem("8-9 AM");
timeSlotBox.addItem("2-3 PM");
timeSlotBox.addItem("3-4 PM");
timeSlotBox.addItem("4-5 PM");
timeSlotBox.addItem("5-6 PM");
}
// For regular classes: use day patterns
else {
dayPatternBox.addItem("MON-WED-FRI");
dayPatternBox.addItem("TUE-THU-MON");
dayPatternBox.addItem("TUE-THU-WED");
dayPatternBox.addItem("TUE-THU-FRI");
// Default time slots for regular classes
for (int i = 0; i <= 4; i++) {
String pattern = (String) dayPatternBox.getSelectedItem();
if (pattern != null && pattern.equals("MON-WED-FRI")) {
timeSlotBox.addItem(i + "-" + i + "-" + i);
} else if (pattern != null) {
if (i != 0) {
timeSlotBox.addItem(8 + i + "-" + (8 + i) + "-" + (1 + i));
}
}
}
}
}
}
class TimetableViewFrame extends JFrame {
public TimetableViewFrame() {
setTitle("Generated Timetable");
setSize(1000, 600);
setLocationRelativeTo(null);
add(createTimetableView());
setVisible(true);
}
private JScrollPane createTimetableView() {
String[] days = {"MON", "TUE", "WED", "THU", "FRI"};
String[] timeSlots = {
"8-9 AM", "9-10 AM", "10-11 AM", "11-12 PM",
"12-1 PM", /* Removed 1-2 PM slot */ "2-3 PM", "3-4 PM", "4-5 PM", "5-6 PM"
};
// Create a table model with time slots as first column
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
// Add column for time slots
model.addColumn("Time");
// Add columns for days
for (String day : days) {
model.addColumn(day);
}
// Initialize the grid with empty cells
String[][] grid = new String[9][5]; // Changed from 10 to 9 rows
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
grid[i][j] = "";
}
}
// Fill the grid with class data
try {
java.util.List<String[]> entries = TimetableData.readEntries();
for (String[] entry : entries) {
String room = entry[0];
String course = entry[1];
String day = entry[2];
int time = Integer.parseInt(entry[3]);
int col = Arrays.asList(days).indexOf(day);
// Adjust time index - skip the 1-2 PM slot (index 5)
int displayTimeIndex = time;
if (time > 5) {
displayTimeIndex = time - 1;
}
if (col >= 0 && time != 5 && displayTimeIndex < 9) { // Skip time=5 (1-2 PM)
// Add the class info to the grid cell
if (!grid[displayTimeIndex][col].isEmpty()) {
grid[displayTimeIndex][col] += "\n"; // Add a newline between entries
}
grid[displayTimeIndex][col] += room + "-" + course;
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Add rows to the model
for (int i = 0; i < timeSlots.length; i++) {
Object[] rowData = new Object[6]; // 1 for time + 5 for days
rowData[0] = timeSlots[i];
for (int j = 0; j < 5; j++) {
rowData[j + 1] = grid[i][j];
}
model.addRow(rowData);
}
// Create and configure the table
JTable table = new JTable(model);
table.setRowHeight(60);
table.getColumnModel().getColumn(0).setPreferredWidth(100); // Make time column a bit wider
// Add custom renderer for cells with multiple classes
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// For day columns with class information
if (column > 0 && value != null && !value.toString().isEmpty()) {
// Create a panel with scroll capability
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.WHITE);
// Create a text area for the class information
JTextArea textArea = new JTextArea(value.toString());
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
textArea.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
// Add the text area to a scroll pane
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(BorderFactory.createEmptyBorder());
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
// For time column or empty cells
else {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (c instanceof JLabel) {
((JLabel) c).setVerticalAlignment(JLabel.CENTER);
}
return c;
}
}
});
return new JScrollPane(table);
}
}
// StudentFrame.java
// StudentFrame.java
class StudentFrame extends JFrame {
public StudentFrame() {
setTitle("Student Course Selection");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Clear student timetable file on startup
clearStudentTimetableFile();
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addButton = new JButton("Add Course");
JButton viewButton = new JButton("View Timetable");
buttonPanel.add(addButton);
buttonPanel.add(viewButton);
// Display status area
JTextArea statusArea = new JTextArea();
statusArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(statusArea);
// Update status area with current courses
updateStatusArea(statusArea);
addButton.addActionListener(e -> {
new StudentAddClassFrame(this, statusArea);
});
viewButton.addActionListener(e -> {
new StudentTimetableFrame();
});
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// Update the status area with currently selected courses
public void updateStatusArea(JTextArea statusArea) {
statusArea.setText(""); // Clear existing text
try {
statusArea.append("Your Selected Courses:\n");
statusArea.append("------------------------\n");
// Read and display courses from studentTT.csv
java.util.List<String[]> entries = readStudentTimetable();
if (entries.isEmpty()) {
statusArea.append("No courses selected yet.\n");
} else {
for (String[] entry : entries) {
String room = entry[0];
String course = entry[1];
String day = entry[2];
String time = convertTimeToString(Integer.parseInt(entry[3]));
statusArea.append(course + " - " + day + " " + time + " in " + room + "\n");
}
}
} catch (IOException ex) {
statusArea.append("Error reading course selections.\n");
ex.printStackTrace();
}
}
// Convert numeric time slot to readable string
private String convertTimeToString(int timeSlot) {
String[] timeSlots = {
"8-9 AM", "9-10 AM", "10-11 AM", "11-12 PM",
"12-1 PM", "2-3 PM", "3-4 PM", "4-5 PM", "5-6 PM"
};
// Adjust for removed 1-2 PM slot
if (timeSlot < 5) {
return timeSlots[timeSlot];
} else if (timeSlot > 5) {
return timeSlots[timeSlot - 1];
}
return "Unknown Time";
}
// Clear student timetable file at startup
private void clearStudentTimetableFile() {
try {
new FileWriter("studentTT.csv", false).close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Read student timetable entries
public static java.util.List<String[]> readStudentTimetable() throws IOException {
java.util.List<String[]> data = new java.util.ArrayList<>();
File file = new File("studentTT.csv");
if (!file.exists()) return data;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
data.add(line.split(","));
}
br.close();
return data;
}
// Check if the course has a scheduling conflict
public static boolean hasConflict(String day, int time) throws IOException {
java.util.List<String[]> entries = readStudentTimetable();
for (String[] entry : entries) {
String entryDay = entry[2];
int entryTime = Integer.parseInt(entry[3]);
if (entryDay.equals(day) && entryTime == time) {
return true;
}
}
return false;
}
// Add a course to student timetable
public static boolean addCourseToTimetable(String room, String course, String day,
int time, boolean isLab, String faculty) throws IOException {
// Check for conflicts
if (hasConflict(day, time)) {
return false;
}
// No conflict, add the course
FileWriter fw = new FileWriter("studentTT.csv", true);
fw.write(String.join(",", room, course, day, String.valueOf(time),
String.valueOf(isLab), faculty) + "\n");
fw.close();
return true;
}
}
// StudentAddClassFrame.java - Similar to AddClassFrame but simplified for students
class StudentAddClassFrame extends JFrame {
private final JComboBox<String> courseBox;
private JComboBox<String> dayBox;
private JComboBox<String> timeBox;
private StudentFrame parentFrame;
private JTextArea statusArea;
public StudentAddClassFrame(StudentFrame parent, JTextArea statusArea) {
this.parentFrame = parent;
this.statusArea = statusArea;
setTitle("Add Course");
setSize(400, 300);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(5, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Course selection
courseBox = new JComboBox<>();
try {
java.util.Map<String, String> courses = TimetableData.getCourseMap();
for (String code : courses.keySet()) {
courseBox.addItem(code + " - " + courses.get(code));
}
} catch (IOException ex) {
ex.printStackTrace();
}
// Day selection
dayBox = new JComboBox<>(new String[]{"MON", "TUE", "WED", "THU", "FRI"});
// Time selection
timeBox = new JComboBox<>();
updateTimeSlots();
// Add listeners
courseBox.addActionListener(e -> {
updateAvailableSlots();
});
dayBox.addActionListener(e -> {
updateAvailableSlots();
});
// Submit button
JButton submitButton = new JButton("Add to My Schedule");
submitButton.addActionListener(e -> {
addCourseToSchedule();
});
panel.add(new JLabel("Select Course:"));
panel.add(courseBox);
panel.add(new JLabel("Select Day:"));
panel.add(dayBox);
panel.add(new JLabel("Select Time:"));
panel.add(timeBox);
panel.add(new JLabel(""));
panel.add(submitButton);
add(panel);
setVisible(true);
// Initial update of available slots
updateAvailableSlots();
}
private void updateTimeSlots() {
timeBox.removeAllItems();
String[] slots = {
"8-9 AM (0)", "9-10 AM (1)", "10-11 AM (2)", "11-12 PM (3)",
"12-1 PM (4)", "2-3 PM (6)", "3-4 PM (7)", "4-5 PM (8)", "5-6 PM (9)",
// Add new lab time slots
"9-11 AM (Lab)", "11-1 PM (Lab)", "2-4 PM (Lab)", "4-6 PM (Lab)"
};
for (String slot : slots) {
timeBox.addItem(slot);
}
}
private void updateAvailableSlots() {
if (courseBox.getSelectedItem() == null) return;
String selectedCourseCode = ((String)courseBox.getSelectedItem()).split(" - ")[0];
String selectedDay = (String)dayBox.getSelectedItem();
// Save current selection
Object currentSelection = timeBox.getSelectedItem();
// Clear and repopulate time slots
timeBox.removeAllItems();
try {
// Get all timetable entries
java.util.List<String[]> allEntries = TimetableData.readEntries();
// Map to track lab sessions and tutorial sessions
Map<String, Boolean> isLabSlot = new HashMap<>();
Map<String, String> roomForSlot = new HashMap<>();
Map<String, String> facultyForSlot = new HashMap<>();
// Find all available slots for this course and day
for (String[] entry : allEntries) {
if (entry[1].equals(selectedCourseCode) && entry[2].equals(selectedDay)) {
int timeValue = Integer.parseInt(entry[3]);
boolean isLab = Boolean.parseBoolean(entry[4]);
String room = entry[0];
String faculty = entry.length >= 6 ? entry[5] : "";
String slotKey = String.valueOf(timeValue);
isLabSlot.put(slotKey, isLab);
roomForSlot.put(slotKey, room);
facultyForSlot.put(slotKey, faculty);
}
}
// Get student's existing schedule to check for conflicts
Set<String> conflictSlots = new HashSet<>();
java.util.List<String[]> studentEntries = StudentFrame.readStudentTimetable();
for (String[] entry : studentEntries) {
if (entry[2].equals(selectedDay)) {
conflictSlots.add(entry[3]); // Mark this time as having a conflict
}
}
// Add available regular time slots
String[] regularSlots = {
"8-9 AM (0)", "9-10 AM (1)", "10-11 AM (2)", "11-12 PM (3)",
"12-1 PM (4)", "2-3 PM (6)", "3-4 PM (7)", "4-5 PM (8)", "5-6 PM (9)"
};
int[] timeValues = {0, 1, 2, 3, 4, 6, 7, 8, 9};
for (int i = 0; i < regularSlots.length; i++) {
String slotKey = String.valueOf(timeValues[i]);
if (roomForSlot.containsKey(slotKey) && !conflictSlots.contains(slotKey)) {
boolean isLab = isLabSlot.getOrDefault(slotKey, false);
String roomInfo = roomForSlot.get(slotKey);
// Only add if it's not part of a lab session
if (!isLab) {
timeBox.addItem(regularSlots[i] + " - Room: " + roomInfo);
}
}
}
// Add available lab time slots (which span 2 hours)
addLabTimeSlots(selectedDay, timeValues, isLabSlot, roomForSlot, conflictSlots);
// Try to restore previous selection
if (currentSelection != null) {
for (int i = 0; i < timeBox.getItemCount(); i++) {
if (((String)timeBox.getItemAt(i)).startsWith(((String)currentSelection).split(" - ")[0])) {
timeBox.setSelectedIndex(i);
break;
}
}
}
// If no options available, add a message
if (timeBox.getItemCount() == 0) {
timeBox.addItem("No available slots for this course/day");
}
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading course data");
}
}
// Helper method to add lab time slots (which span multiple hours)
private void addLabTimeSlots(String selectedDay, int[] timeValues,
Map<String, Boolean> isLabSlot,
Map<String, String> roomForSlot,
Set<String> conflictSlots) {
// Check for 9-11 AM lab (timeValues 1 and 2)
if (isLabSlot.getOrDefault("1", false) && isLabSlot.getOrDefault("2", false) &&
!conflictSlots.contains("1") && !conflictSlots.contains("2")) {
timeBox.addItem("9-11 AM (Lab) - Room: " + roomForSlot.get("1"));
}
// Check for 11-1 PM lab (timeValues 3 and 4)
if (isLabSlot.getOrDefault("3", false) && isLabSlot.getOrDefault("4", false) &&
!conflictSlots.contains("3") && !conflictSlots.contains("4")) {
timeBox.addItem("11-1 PM (Lab) - Room: " + roomForSlot.get("3"));
}
// Check for 2-4 PM lab (timeValues 6 and 7)
if (isLabSlot.getOrDefault("6", false) && isLabSlot.getOrDefault("7", false) &&
!conflictSlots.contains("6") && !conflictSlots.contains("7")) {
timeBox.addItem("2-4 PM (Lab) - Room: " + roomForSlot.get("6"));
}
// Check for 4-6 PM lab (timeValues 8 and 9)
if (isLabSlot.getOrDefault("8", false) && isLabSlot.getOrDefault("9", false) &&
!conflictSlots.contains("8") && !conflictSlots.contains("9")) {
timeBox.addItem("4-6 PM (Lab) - Room: " + roomForSlot.get("8"));
}
}
private void addCourseToSchedule() {
if (courseBox.getSelectedItem() == null || timeBox.getSelectedItem() == null) {
JOptionPane.showMessageDialog(this, "Please select course and time");
return;
}
String selectedTimeText = (String)timeBox.getSelectedItem();
if (selectedTimeText.startsWith("No available")) {
JOptionPane.showMessageDialog(this, "No available time slots for this selection");
return;
}
try {
// Parse course code
String courseCode = ((String)courseBox.getSelectedItem()).split(" - ")[0];
// Parse day
String day = (String)dayBox.getSelectedItem();
// Check if it's a lab session
boolean isLab = selectedTimeText.contains("(Lab)");
// Get room info
String roomInfo = selectedTimeText.split("Room: ")[1];
String room = roomInfo;
// Find appropriate time values and add to schedule
if (isLab) {
// Handle lab sessions (2 hours)
java.util.List<Integer> labTimes = new ArrayList<>();
if (selectedTimeText.contains("9-11 AM")) {
labTimes.add(1);
labTimes.add(2);
} else if (selectedTimeText.contains("11-1 PM")) {
labTimes.add(3);