Refactored controller classes and the Course class for better maintainability

This commit is contained in:
haxala1r
2025-12-18 16:19:32 +03:00
parent 39ad08d836
commit 463d8b39ec
5 changed files with 133 additions and 202 deletions

View File

@@ -15,20 +15,33 @@ import org.example.se302.service.DataManager;
*/ */
public class CoursesController { public class CoursesController {
@FXML private TextField searchField; @FXML
@FXML private Label resultCountLabel; private TextField searchField;
@FXML private TableView<Course> coursesTable; @FXML
@FXML private TableColumn<Course, String> courseCodeColumn; private Label resultCountLabel;
@FXML private TableColumn<Course, Number> studentCountColumn; @FXML
@FXML private TableColumn<Course, String> classroomColumn; private TableView<Course> coursesTable;
@FXML private TableColumn<Course, String> examDateColumn; @FXML
@FXML private TableColumn<Course, Void> actionColumn; private TableColumn<Course, String> courseCodeColumn;
@FXML
private TableColumn<Course, Number> studentCountColumn;
@FXML
private TableColumn<Course, String> classroomColumn;
@FXML
private TableColumn<Course, String> examDateColumn;
@FXML
private TableColumn<Course, Void> actionColumn;
@FXML private VBox studentListPanel; @FXML
@FXML private Label studentListTitleLabel; private VBox studentListPanel;
@FXML private TableView<String> enrolledStudentsTable; @FXML
@FXML private TableColumn<String, String> enrolledStudentIdColumn; private Label studentListTitleLabel;
@FXML private Label enrolledCountLabel; @FXML
private TableView<String> enrolledStudentsTable;
@FXML
private TableColumn<String, String> enrolledStudentIdColumn;
@FXML
private Label enrolledCountLabel;
private DataManager dataManager; private DataManager dataManager;
private FilteredList<Course> filteredCourses; private FilteredList<Course> filteredCourses;
@@ -38,11 +51,10 @@ public class CoursesController {
dataManager = DataManager.getInstance(); dataManager = DataManager.getInstance();
// Set up table columns // Set up table columns
courseCodeColumn.setCellValueFactory(cellData -> courseCodeColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCourseCode()));
new SimpleStringProperty(cellData.getValue().getCourseCode()));
studentCountColumn.setCellValueFactory(cellData -> studentCountColumn.setCellValueFactory(
new SimpleIntegerProperty(cellData.getValue().getEnrolledStudentsCount())); cellData -> new SimpleIntegerProperty(cellData.getValue().getEnrolledStudentsCount()));
classroomColumn.setCellValueFactory(cellData -> { classroomColumn.setCellValueFactory(cellData -> {
String classroom = cellData.getValue().getAssignedClassroom(); String classroom = cellData.getValue().getAssignedClassroom();
@@ -50,8 +62,12 @@ public class CoursesController {
}); });
examDateColumn.setCellValueFactory(cellData -> { examDateColumn.setCellValueFactory(cellData -> {
String examDate = cellData.getValue().getExamDateTime(); Course course = cellData.getValue();
return new SimpleStringProperty(examDate != null ? examDate : "Not Scheduled"); if (course.isScheduled()) {
return new SimpleStringProperty("Day " + (course.getExamDay() + 1) +
", Slot " + (course.getExamTimeSlot() + 1));
}
return new SimpleStringProperty("Not Scheduled");
}); });
// Add "View Students" button to action column // Add "View Students" button to action column
@@ -73,8 +89,7 @@ public class CoursesController {
}); });
// Set up enrolled students table column // Set up enrolled students table column
enrolledStudentIdColumn.setCellValueFactory(cellData -> enrolledStudentIdColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue()));
new SimpleStringProperty(cellData.getValue()));
// Set up filtered list // Set up filtered list
filteredCourses = new FilteredList<>(dataManager.getCourses(), p -> true); filteredCourses = new FilteredList<>(dataManager.getCourses(), p -> true);
@@ -106,14 +121,13 @@ public class CoursesController {
filteredCourses.setPredicate(course -> true); filteredCourses.setPredicate(course -> true);
} else { } else {
String lowerCaseFilter = searchText.toLowerCase().trim(); String lowerCaseFilter = searchText.toLowerCase().trim();
filteredCourses.setPredicate(course -> filteredCourses.setPredicate(course -> course.getCourseCode().toLowerCase().contains(lowerCaseFilter));
course.getCourseCode().toLowerCase().contains(lowerCaseFilter)
);
} }
} }
private void showEnrolledStudents(Course course) { private void showEnrolledStudents(Course course) {
if (course == null) return; if (course == null)
return;
studentListTitleLabel.setText("Students Enrolled in " + course.getCourseCode()); studentListTitleLabel.setText("Students Enrolled in " + course.getCourseCode());
enrolledStudentsTable.setItems(FXCollections.observableArrayList(course.getEnrolledStudents())); enrolledStudentsTable.setItems(FXCollections.observableArrayList(course.getEnrolledStudents()));

View File

@@ -150,78 +150,62 @@ public class ScheduleClassroomController {
ScheduleConfiguration config = dataManager.getActiveConfiguration(); ScheduleConfiguration config = dataManager.getActiveConfiguration();
ObservableList<ClassroomSlotEntry> entries = FXCollections.observableArrayList(); ObservableList<ClassroomSlotEntry> entries = FXCollections.observableArrayList();
int totalSlots = 0;
int usedSlots = 0; int usedSlots = 0;
int totalStudents = 0; int totalStudents = 0;
// Find all courses scheduled in this classroom
for (Course course : dataManager.getCourses()) { for (Course course : dataManager.getCourses()) {
if (course.isScheduled() && if (!course.isScheduled() || !selected.getClassroomId().equals(course.getAssignedClassroom()))
selected.getClassroomId().equals(course.getAssignedClassroom())) { continue;
int dayIndex = course.getExamDay(); int dayIndex = course.getExamDay();
int slotIndex = course.getExamTimeSlot(); int slotIndex = course.getExamTimeSlot();
int studentCount = course.getEnrolledStudentsCount();
int utilization = selected.getCapacity() > 0
? (studentCount * 100) / selected.getCapacity()
: 0;
// Format date entries.add(new ClassroomSlotEntry(
String dateStr; formatDate(config, dayIndex),
if (config != null && config.getStartDate() != null) { formatTime(config, dayIndex, slotIndex),
LocalDate examDate = config.getStartDate().plusDays(dayIndex); course.getCourseCode(), studentCount, utilization + "%",
dateStr = examDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); utilization, dayIndex, slotIndex));
} else {
dateStr = "Day " + (dayIndex + 1);
}
// Format time usedSlots++;
String timeStr; totalStudents += studentCount;
if (config != null) {
TimeSlot timeSlot = config.getTimeSlot(dayIndex, slotIndex);
if (timeSlot != null) {
timeStr = timeSlot.getStartTime() + " - " + timeSlot.getEndTime();
} else {
timeStr = "Slot " + (slotIndex + 1);
}
} else {
timeStr = "Slot " + (slotIndex + 1);
}
// Calculate utilization
int studentCount = course.getEnrolledStudentsCount();
int capacity = selected.getCapacity();
int utilizationPercent = capacity > 0 ? (studentCount * 100) / capacity : 0;
String utilizationStr = utilizationPercent + "%";
entries.add(new ClassroomSlotEntry(
dateStr, timeStr, course.getCourseCode(),
studentCount, utilizationStr, utilizationPercent,
dayIndex, slotIndex));
usedSlots++;
totalStudents += studentCount;
}
} }
// Calculate total possible slots
if (config != null) {
totalSlots = config.getNumDays() * config.getSlotsPerDay();
}
// Sort by day then slot
entries.sort(Comparator.comparingInt(ClassroomSlotEntry::getDayIndex) entries.sort(Comparator.comparingInt(ClassroomSlotEntry::getDayIndex)
.thenComparingInt(ClassroomSlotEntry::getSlotIndex)); .thenComparingInt(ClassroomSlotEntry::getSlotIndex));
scheduleTable.setItems(entries); scheduleTable.setItems(entries);
// Update overall utilization label int totalSlots = config != null ? config.getNumDays() * config.getSlotsPerDay() : 0;
if (totalSlots > 0) { if (totalSlots > 0) {
int overallUtilization = (usedSlots * 100) / totalSlots; int overallUtil = (usedSlots * 100) / totalSlots;
utilizationLabel.setText(String.format( utilizationLabel.setText(String.format(
"Overall Utilization: %d%% (%d/%d slots used, %d total students)", "Overall Utilization: %d%% (%d/%d slots used, %d total students)",
overallUtilization, usedSlots, totalSlots, totalStudents)); overallUtil, usedSlots, totalSlots, totalStudents));
} else { } else {
utilizationLabel.setText("Overall Utilization: 0% (No schedule data available)"); utilizationLabel.setText("Overall Utilization: 0% (No schedule data available)");
} }
} }
private String formatDate(ScheduleConfiguration config, int dayIndex) {
if (config != null && config.getStartDate() != null) {
LocalDate examDate = config.getStartDate().plusDays(dayIndex);
return examDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
}
return "Day " + (dayIndex + 1);
}
private String formatTime(ScheduleConfiguration config, int dayIndex, int slotIndex) {
if (config != null) {
TimeSlot slot = config.getTimeSlot(dayIndex, slotIndex);
if (slot != null)
return slot.getStartTime() + " - " + slot.getEndTime();
}
return "Slot " + (slotIndex + 1);
}
// Helper class for table entries // Helper class for table entries
public static class ClassroomSlotEntry { public static class ClassroomSlotEntry {
private final String date; private final String date;

View File

@@ -151,53 +151,48 @@ public class ScheduleCourseController {
ObservableList<CourseScheduleEntry> entries = FXCollections.observableArrayList(); ObservableList<CourseScheduleEntry> entries = FXCollections.observableArrayList();
for (Course course : dataManager.getCourses()) { for (Course course : dataManager.getCourses()) {
String courseCode = course.getCourseCode();
int enrolled = course.getEnrolledStudentsCount();
String dateStr = "Not Scheduled"; String dateStr = "Not Scheduled";
String timeStr = "-"; String timeStr = "-";
String classroomStr = "-"; String classroomStr = "-";
int dayIndex = Integer.MAX_VALUE; int dayIndex = Integer.MAX_VALUE;
int slotIndex = Integer.MAX_VALUE; int slotIndex = Integer.MAX_VALUE;
// Check if this course has been scheduled
if (course.isScheduled()) { if (course.isScheduled()) {
dayIndex = course.getExamDay(); dayIndex = course.getExamDay();
slotIndex = course.getExamTimeSlot(); slotIndex = course.getExamTimeSlot();
classroomStr = course.getAssignedClassroom(); classroomStr = course.getAssignedClassroom();
dateStr = formatDate(config, dayIndex);
// Format date using configuration's start date timeStr = formatTime(config, dayIndex, slotIndex);
if (config != null && config.getStartDate() != null) {
LocalDate examDate = config.getStartDate().plusDays(dayIndex);
dateStr = examDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy (EEEE)"));
} else {
dateStr = "Day " + (dayIndex + 1);
}
// Format time using configuration's time slots
if (config != null) {
TimeSlot timeSlot = config.getTimeSlot(dayIndex, slotIndex);
if (timeSlot != null) {
timeStr = timeSlot.getStartTime() + " - " + timeSlot.getEndTime();
} else {
timeStr = "Slot " + (slotIndex + 1);
}
} else {
timeStr = "Slot " + (slotIndex + 1);
}
} }
entries.add(new CourseScheduleEntry(courseCode, enrolled, dateStr, timeStr, classroomStr, entries.add(new CourseScheduleEntry(course.getCourseCode(),
course.getEnrolledStudentsCount(), dateStr, timeStr, classroomStr,
dayIndex, slotIndex)); dayIndex, slotIndex));
} }
// Sort by day first, then by slot
entries.sort(Comparator.comparingInt(CourseScheduleEntry::getDayIndex) entries.sort(Comparator.comparingInt(CourseScheduleEntry::getDayIndex)
.thenComparingInt(CourseScheduleEntry::getSlotIndex)); .thenComparingInt(CourseScheduleEntry::getSlotIndex));
courseScheduleTable.setItems(entries); courseScheduleTable.setItems(entries);
} }
private String formatDate(ScheduleConfiguration config, int dayIndex) {
if (config != null && config.getStartDate() != null) {
LocalDate examDate = config.getStartDate().plusDays(dayIndex);
return examDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy (EEEE)"));
}
return "Day " + (dayIndex + 1);
}
private String formatTime(ScheduleConfiguration config, int dayIndex, int slotIndex) {
if (config != null) {
TimeSlot slot = config.getTimeSlot(dayIndex, slotIndex);
if (slot != null) {
return slot.getStartTime() + " - " + slot.getEndTime();
}
}
return "Slot " + (slotIndex + 1);
}
// Helper class for table entries // Helper class for table entries
public static class CourseScheduleEntry { public static class CourseScheduleEntry {
private final String courseCode; private final String courseCode;

View File

@@ -105,77 +105,58 @@ public class ScheduleStudentController {
for (String courseCode : student.getEnrolledCourses()) { for (String courseCode : student.getEnrolledCourses()) {
Course course = dataManager.getCourse(courseCode); Course course = dataManager.getCourse(courseCode);
if (course != null) { if (course == null)
String dateStr = "Not Scheduled"; continue;
String timeStr = "-";
String classroom = "-";
int dayIndex = -1;
int slotIndex = -1;
if (course.isScheduled()) { String dateStr = "Not Scheduled";
dayIndex = course.getExamDay(); String timeStr = "-";
slotIndex = course.getExamTimeSlot(); String classroom = "-";
classroom = course.getAssignedClassroom(); int dayIndex = -1, slotIndex = -1;
if (config != null) { if (course.isScheduled()) {
TimeSlot slot = config.getTimeSlot(dayIndex, slotIndex); dayIndex = course.getExamDay();
if (slot != null) { slotIndex = course.getExamTimeSlot();
dateStr = slot.getDate().toString(); // YYYY-MM-DD classroom = course.getAssignedClassroom();
timeStr = slot.getStartTime().toString() + " - " + slot.getEndTime().toString();
} else { TimeSlot slot = config != null ? config.getTimeSlot(dayIndex, slotIndex) : null;
dateStr = "Day " + (dayIndex + 1); if (slot != null) {
timeStr = "Slot " + (slotIndex + 1); dateStr = slot.getDate().toString();
} timeStr = slot.getStartTime() + " - " + slot.getEndTime();
} else { } else {
// Fallback if no config saved dateStr = "Day " + (dayIndex + 1);
dateStr = "Day " + (dayIndex + 1); timeStr = "Slot " + (slotIndex + 1);
timeStr = "Slot " + (slotIndex + 1);
}
} }
entries.add(new CourseScheduleEntry(courseCode, dateStr, timeStr, classroom, dayIndex, slotIndex));
} }
entries.add(new CourseScheduleEntry(courseCode, dateStr, timeStr, classroom, dayIndex, slotIndex));
} }
// Sort by day and time
entries.sort(Comparator.comparingInt(CourseScheduleEntry::getDayIndex) entries.sort(Comparator.comparingInt(CourseScheduleEntry::getDayIndex)
.thenComparingInt(CourseScheduleEntry::getSlotIndex)); .thenComparingInt(CourseScheduleEntry::getSlotIndex));
// Analyze for highlights
analyzeSchedule(entries); analyzeSchedule(entries);
scheduleTable.setItems(FXCollections.observableArrayList(entries)); scheduleTable.setItems(FXCollections.observableArrayList(entries));
} }
private void analyzeSchedule(List<CourseScheduleEntry> entries) { private void analyzeSchedule(List<CourseScheduleEntry> entries) {
if (entries.isEmpty())
return;
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
CourseScheduleEntry current = entries.get(i); CourseScheduleEntry current = entries.get(i);
if (current.getDayIndex() == -1) if (current.getDayIndex() == -1)
continue; // Skip unscheduled continue;
// Check for multiple exams on same day // Check for multiple exams and conflicts on same day
int examsOnDay = 0;
for (CourseScheduleEntry other : entries) { for (CourseScheduleEntry other : entries) {
if (other.getDayIndex() == current.getDayIndex() && other.getDayIndex() != -1) { if (other.getDayIndex() == current.getDayIndex() && other.getDayIndex() != -1) {
examsOnDay++; current.isMultipleExamsOnDay = true;
if (other.getSlotIndex() == current.getSlotIndex() && other != current) { if (other.getSlotIndex() == current.getSlotIndex() && other != current) {
current.hasConflictWarning = true; current.hasConflictWarning = true;
} }
} }
} }
if (examsOnDay > 1) {
current.isMultipleExamsOnDay = true;
}
// Check for consecutive days (look at previous scheduled exam) // Check for consecutive days with previous exam
// Since list is sorted, we can look at previous entry if it exists
if (i > 0) { if (i > 0) {
CourseScheduleEntry prev = entries.get(i - 1); CourseScheduleEntry prev = entries.get(i - 1);
if (prev.getDayIndex() != -1 && if (prev.getDayIndex() != -1 && current.getDayIndex() == prev.getDayIndex() + 1) {
current.getDayIndex() == prev.getDayIndex() + 1) {
current.isConsecutiveDay = true; current.isConsecutiveDay = true;
} }
} }

View File

@@ -4,72 +4,40 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Represents a course in the exam scheduling system. * Represents a course with exam scheduling details.
* Contains course information and exam scheduling details.
*/ */
public class Course { public class Course {
private String courseCode; private String courseCode;
private List<String> enrolledStudents; private List<String> enrolledStudents;
private int examDay = -1;
// Exam schedule fields (index-based) private int examTimeSlot = -1;
private int examDay; // -1 if not scheduled, 0-based day index private String assignedClassroom;
private int examTimeSlot; // -1 if not scheduled, 0-based slot index
private String assignedClassroom; // null if not scheduled
// Legacy field for backward compatibility
private String examDateTime; // null if not scheduled (string format)
public Course(String courseCode) { public Course(String courseCode) {
this.courseCode = courseCode; this.courseCode = courseCode;
this.enrolledStudents = new ArrayList<>(); this.enrolledStudents = new ArrayList<>();
this.examDay = -1;
this.examTimeSlot = -1;
this.assignedClassroom = null;
this.examDateTime = null;
} }
/**
* Checks if this course has been scheduled for an exam.
*/
public boolean isScheduled() { public boolean isScheduled() {
return examDay >= 0 && examTimeSlot >= 0 && assignedClassroom != null; return examDay >= 0 && examTimeSlot >= 0 && assignedClassroom != null;
} }
/**
* Clears the exam schedule for this course.
*/
public void clearSchedule() { public void clearSchedule() {
this.examDay = -1; this.examDay = -1;
this.examTimeSlot = -1; this.examTimeSlot = -1;
this.assignedClassroom = null; this.assignedClassroom = null;
this.examDateTime = null;
} }
/**
* Sets the complete exam schedule.
*
* @param day Day index (0-based)
* @param timeSlot Time slot index (0-based)
* @param classroomId Classroom ID
*/
public void setExamSchedule(int day, int timeSlot, String classroomId) { public void setExamSchedule(int day, int timeSlot, String classroomId) {
this.examDay = day; this.examDay = day;
this.examTimeSlot = timeSlot; this.examTimeSlot = timeSlot;
this.assignedClassroom = classroomId; this.assignedClassroom = classroomId;
} }
/**
* Gets a unique key for this course's time slot.
*/
public String getTimeSlotKey() { public String getTimeSlotKey() {
if (!isScheduled()) { return isScheduled() ? "D" + examDay + "_S" + examTimeSlot : null;
return null;
}
return "D" + examDay + "_S" + examTimeSlot;
} }
// Basic getters and setters
public String getCourseCode() { public String getCourseCode() {
return courseCode; return courseCode;
} }
@@ -82,14 +50,13 @@ public class Course {
return enrolledStudents; return enrolledStudents;
} }
public void setEnrolledStudents(List<String> enrolledStudents) { public void setEnrolledStudents(List<String> students) {
this.enrolledStudents = enrolledStudents; this.enrolledStudents = students;
} }
public void addStudent(String studentId) { public void addStudent(String studentId) {
if (!enrolledStudents.contains(studentId)) { if (!enrolledStudents.contains(studentId))
enrolledStudents.add(studentId); enrolledStudents.add(studentId);
}
} }
public void removeStudent(String studentId) { public void removeStudent(String studentId) {
@@ -100,8 +67,6 @@ public class Course {
return enrolledStudents.size(); return enrolledStudents.size();
} }
// Schedule field getters and setters
public int getExamDay() { public int getExamDay() {
return examDay; return examDay;
} }
@@ -114,24 +79,16 @@ public class Course {
return examTimeSlot; return examTimeSlot;
} }
public void setExamTimeSlot(int examTimeSlot) { public void setExamTimeSlot(int slot) {
this.examTimeSlot = examTimeSlot; this.examTimeSlot = slot;
} }
public String getAssignedClassroom() { public String getAssignedClassroom() {
return assignedClassroom; return assignedClassroom;
} }
public void setAssignedClassroom(String assignedClassroom) { public void setAssignedClassroom(String classroom) {
this.assignedClassroom = assignedClassroom; this.assignedClassroom = classroom;
}
public String getExamDateTime() {
return examDateTime;
}
public void setExamDateTime(String examDateTime) {
this.examDateTime = examDateTime;
} }
@Override @Override