学生选课系统通过Student、Course、Teacher和CourseSystem类实现,使用List和Map管理数据,支持选课、退课、查询等功能,体现面向对象设计与集合应用。

开发一个学生选课系统是Java学习中非常典型的综合练习,它能帮助你深入理解面向对象设计、多类协作以及集合框架的使用。这个系统涉及多个实体类之间的交互,比如学生、课程、教师等,同时需要利用List、Map等集合来管理数据和关系。
在动手编码前,先明确系统的基本功能:
基于这些需求,我们可以设计以下几个核心类:
主要保存学生基本信息,并用ArrayList存储已选课程。
立即学习“Java免费学习笔记(深入)”;
public class Student {
private String id;
private String name;
private List<Course> selectedCourses;
public Student(String id, String name) {
this.id = id;
this.name = name;
this.selectedCourses = new ArrayList<>();
}
public void addCourse(Course course) {
if (!selectedCourses.contains(course)) {
selectedCourses.add(course);
}
}
public void dropCourse(Course course) {
selectedCourses.remove(course);
}
// getter方法
public String getId() { return id; }
public String getName() { return name; }
public List<Course> getSelectedCourses() { return selectedCourses; }
}
管理课程信息和选课人数,使用List记录选课学生。
public class Course {
private String courseId;
private String name;
private Teacher teacher;
private int maxStudents;
private List<Student> enrolledStudents;
public Course(String courseId, String name, Teacher teacher, int maxStudents) {
this.courseId = courseId;
this.name = name;
this.teacher = teacher;
this.maxStudents = maxStudents;
this.enrolledStudents = new ArrayList<>();
}
public boolean isFull() {
return enrolledStudents.size() >= maxStudents;
}
public boolean enroll(Student student) {
if (!isFull()) {
enrolledStudents.add(student);
return true;
}
return false;
}
public boolean drop(Student student) {
return enrolledStudents.remove(student);
}
// getter方法
public String getCourseId() { return courseId; }
public String getName() { return name; }
public List<Student> getEnrolledStudents() { return enrolledStudents; }
public int getCurrentCount() { return enrolledStudents.size(); }
}
CourseSystem类作为协调者,使用Map来高效查找学生和课程,避免遍历List。
public class CourseSystem {
private Map<String, Student> students;
private Map<String, Course> courses;
public CourseSystem() {
this.students = new HashMap<>();
this.courses = new HashMap<>();
}
public void addStudent(Student student) {
students.put(student.getId(), student);
}
public void addCourse(Course course) {
courses.put(course.getCourseId(), course);
}
public boolean selectCourse(String studentId, String courseId) {
Student student = students.get(studentId);
Course course = courses.get(courseId);
if (student == null || course == null) {
System.out.println("学生或课程不存在");
return false;
}
if (course.isFull()) {
System.out.println("课程已满");
return false;
}
if (course.enroll(student)) {
student.addCourse(course);
System.out.println(student.getName() + " 成功选修 " + course.getName());
return true;
}
return false;
}
public void showCourseStudents(String courseId) {
Course course = courses.get(courseId);
if (course != null) {
System.out.println("课程《" + course.getName() + "》的学生列表:");
for (Student s : course.getEnrolledStudents()) {
System.out.println(" " + s.getName() + " (" + s.getId() + ")");
}
}
}
}
编写main方法测试系统功能:
public class Main {
public static void main(String[] args) {
CourseSystem system = new CourseSystem();
Teacher t1 = new Teacher("T001", "张教授");
Course c1 = new Course("C001", "Java编程", t1, 2);
Course c2 = new Course("C002", "数据结构", t1, 2);
Student s1 = new Student("S001", "小明");
Student s2 = new Student("S002", "小红");
system.addCourse(c1);
system.addCourse(c2);
system.addStudent(s1);
system.addStudent(s2);
system.selectCourse("S001", "C001");
system.selectCourse("S002", "C001");
system.selectCourse("S001", "C002"); // 小明再选一门
system.showCourseStudents("C001");
}
}
输出结果会显示两名学生成功选入Java编程课,并列出具体名单。
基本上就这些。通过这个练习,你能掌握类之间如何协作、集合如何高效管理对象引用,以及如何设计清晰的责任分工。不复杂但容易忽略细节,比如重复选课判断、集合初始化等。以上就是Java开发学生选课系统_多类协作与集合框架综合练习的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号