Java排课示例:用代码实现课程安排
以下是一个简单的排课例子,使用Java语言实现:
import java.util.ArrayList;
import java.util.List;
public class Schedule {
private List<Course> courses;
public Schedule() {
this.courses = new ArrayList<>();
}
public void addCourse(Course course) {
this.courses.add(course);
}
public void removeCourse(Course course) {
this.courses.remove(course);
}
public List<Course> getCourses() {
return this.courses;
}
public void printSchedule() {
System.out.println('Schedule:');
for (Course course : this.courses) {
System.out.println(course);
}
}
public static void main(String[] args) {
Schedule schedule = new Schedule();
Course c1 = new Course('Math', 'MWF', '9:00am-10:00am');
Course c2 = new Course('English', 'TTh', '10:00am-11:00am');
Course c3 = new Course('History', 'MWF', '11:00am-12:00pm');
schedule.addCourse(c1);
schedule.addCourse(c2);
schedule.addCourse(c3);
schedule.printSchedule();
}
}
class Course {
private String name;
private String days;
private String time;
public Course(String name, String days, String time) {
this.name = name;
this.days = days;
this.time = time;
}
public String getName() {
return this.name;
}
public String getDays() {
return this.days;
}
public String getTime() {
return this.time;
}
public String toString() {
return this.name + ' (' + this.days + ' ' + this.time + ')';
}
}
在上面的代码中,有两个类:Schedule和Course。Schedule类表示整个排课表,包含多个课程,可以添加、删除课程,并可以打印整个排课表。Course类表示单个课程,包含课程名称、上课时间和上课日期,可以用于创建新的课程对象。
在main方法中,首先创建一个新的排课表对象schedule,然后创建三个课程对象c1、c2、c3,并将它们添加到排课表中。最后调用printSchedule方法打印整个排课表。
输出结果如下:
Schedule:
Math (MWF 9:00am-10:00am)
English (TTh 10:00am-11:00am)
History (MWF 11:00am-12:00pm)
当然,这只是一个简单的排课例子,实际的排课系统可能需要更复杂的功能和数据结构来实现。
原文地址: https://www.cveoy.top/t/topic/lFrZ 著作权归作者所有。请勿转载和采集!