课程信息管理系统 Java 实验小结
引言
课程信息管理系统是一种用于管理学生课程信息的应用程序。通过该系统,学生可以查询自己的课程信息,包括课程名称、授课教师、上课时间和地点等。教师可以添加、修改和删除课程信息,并查询学生的选课情况。该系统使用 Java 编程语言开发,并采用面向对象的设计思想。本文将对该系统的设计与实现进行总结,并附带代码示例进行讲解。
设计与实现
功能要求
课程信息管理系统需要实现以下功能:
- 学生功能:学生可以查询自己的课程信息,包括课程名称、授课教师、上课时间和地点。
- 教师功能:教师可以添加、修改和删除课程信息,并查询学生的选课情况。
数据结构
系统中最重要的数据结构是课程(Course
)和学生(Student
)。课程包含名称(name
)、授课教师(teacher
)、上课时间(time
)和地点(location
)等属性。学生包含姓名(name
)、学号(id
)和已选课程列表(courses
)等属性。
public class Course {
private String name;
private String teacher;
private String time;
private String location;
// 构造函数、Getter 和 Setter 方法省略
}
public class Student {
private String name;
private String id;
private List<Course> courses;
// 构造函数、Getter 和 Setter 方法省略
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
}
系统设计
系统采用了面向对象的设计思想,使用了 MVC(Model-View-Controller)架构模式。其中,Course
和 Student
是模型(Model),用于表示数据。视图(View)负责显示数据给用户,控制器(Controller)负责处理用户的输入。
在系统的入口处,我们创建了一个主控制器(Controller
)对象,并通过它来管理课程和学生的数据。主控制器提供了各种方法供用户调用,实现了上述的功能要求。
public class Controller {
private List<Course> courses;
private List<Student> students;
public void addCourse(String name, String teacher, String time, String location) {
Course course = new Course(name, teacher, time, location);
courses.add(course);
}
public void removeCourse(String name) {
Course course = findCourseByName(name);
courses.remove(course);
}
public void addStudent(String name, String id) {
Student student = new Student(name, id);
students.add(student);
}
public void removeStudent(String id) {
Student student = findStudentById(id);
students.remove(student);
}
// 其他方法省略
}
代码示例
下面是一个简单的代码示例,展示了如何使用课程信息管理系统的功能。
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
// 添加课程
controller.addCourse("数学", "张三", "周一 8:00-9:40", "教室A");
controller.addCourse("英语", "李四", "周二 10:00-11:40", "教室B");
// 添加学生
controller.addStudent("小明", "1001");
controller.addStudent("小红", "1002");
// 学生选课
controller.addCourseForStudent("1001", "数学");
controller.addCourseForStudent("1001", "英语");
controller.addCourseForStudent("1002", "数学");
// 查询学生选课情况
List<Course> courses = controller.getCoursesByStudent("1001");
for (Course course : courses) {
System.out.println(course.getName());
}
}
}
实验总结
通过本次实验,我学习了如何使用 Java 编程语言开发一个简单的课程信息管理系统。通过面向对象的设计思想,我们将系统