簡化的場景設定
- 有限的教室數量。
- 每個教師可以教授多個課程。
- 每個課程在一個特定的時間段內只能安排一次。
- 考慮教室容量和課程需求。
Java代碼實現
首先,我們定義幾個基本的類:Course
、Teacher
、Room
?和?TimeSlot
。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;class Course {String name;Teacher teacher;int studentCount;public Course(String name, Teacher teacher, int studentCount) {this.name = name;this.teacher = teacher;this.studentCount = studentCount;}
}class Teacher {String name;List<Course> courses;public Teacher(String name) {this.name = name;this.courses = new ArrayList<>();}void addCourse(Course course) {this.courses.add(course);}
}class Room {String roomNumber;int capacity;public Room(String roomNumber, int capacity) {this.roomNumber = roomNumber;this.capacity = capacity;}
}class TimeSlot {String day;String time;public TimeSlot(String day, String time) {this.day = day;this.time = time;}
}class ScheduleEntry {Course course;Room room;TimeSlot timeSlot;public ScheduleEntry(Course course, Room room, TimeSlot timeSlot) {this.course = course;this.room = room;this.timeSlot = timeSlot;}
}public class Scheduler {List<Course> courses;List<Teacher> teachers;List<Room> rooms;List<TimeSlot> timeSlots;List<ScheduleEntry> schedule;public Scheduler(List<Course> courses, List<Teacher> teachers, List<Room> rooms, List<TimeSlot> timeSlots) {this.courses = courses;this.teachers = teachers;this.rooms = rooms;this.timeSlots = timeSlots;this.schedule = new ArrayList<>();}public void createSchedule() {Map<Teacher, List<TimeSlot>> teacherAvailability = new HashMap<>();for (Teacher teacher : teachers) {teacherAvailability.put(teacher, new ArrayList<>(timeSlots));}for (Course course : courses) {for (Room room : rooms) {if (room.capacity >= course.studentCount) {for (TimeSlot timeSlot : teacherAvailability.get(course.teacher)) {schedule.add(new ScheduleEntry(course, room, timeSlot));teacherAvailability.get(course.teacher).remove(timeSlot);break;}break;}}}}public void printSchedule() {for (ScheduleEntry entry : schedule) {System.out.println("Course: " + entry.course.name + " in Room: " + entry.room.roomNumber +" at " + entry.timeSlot.day + " " + entry.timeSlot.time + " by Teacher: " + entry.course.teacher.name);}}public static void main(String[] args) {List<Course> courses = new ArrayList<>();List<Teacher> teachers = new ArrayList<>();List<Room> rooms = new ArrayList<>();List<TimeSlot> timeSlots = new ArrayList<>();Teacher teacher1 = new Teacher("Dr. Smith");Teacher teacher2 = new Teacher("Dr. Jones");teachers.add(teacher1);teachers.add(teacher2);Course course1 = new Course("Math 101", teacher1, 30);Course course2 = new Course("Physics 101", teacher2, 25);courses.add(course1);courses.add(course2);teacher1.addCourse(course1);teacher2.addCourse(course2);Room room1 = new Room("101A", 50);Room room2 = new Room("102B", 30);rooms.add(room1);rooms.add(room2);TimeSlot slot1 = new TimeSlot("Monday", "9AM-11AM");TimeSlot slot2 = new TimeSlot("Tuesday", "10AM-12PM");timeSlots.add(slot1);timeSlots.add(slot2);Scheduler scheduler = new Scheduler(courses, teachers, rooms, timeSlots);scheduler.createSchedule();scheduler.printSchedule();}
}
說明
- 類定義:定義了
Course
,?Teacher
,?Room
,?TimeSlot
, 和?ScheduleEntry
類來表示課程、教師、教室、時間段和排課條目。 - 排課邏輯:在
createSchedule()
方法中,我們嘗試為每個課程找到一個合適的教室和時間段。我們假設每個教師在所有時間段都可用,這顯然是非常簡化的。 - 輸出:
printSchedule()
方法打印出排課結果。
這個例子非常基礎,實際應用中排課算法會更復雜,需要處理更多的約束和優化問題。