学生选课管理系统通过Student、Course和CourseManager类实现,支持学生与课程的增删查及选课退课功能,利用集合存储数据并进行关联操作,最后通过测试类验证核心逻辑正确性。

学生选课管理系统是Java初学者常见的实战项目,能综合运用面向对象编程、集合框架、文件读写等核心知识。下面从需求分析到代码实现,带你一步步完成一个基础但完整的系统。
一个基本的学生选课管理系统应包含以下功能:
根据功能划分,系统主要需要三个类:
Student类
立即学习“Java免费学习笔记(深入)”;
public class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
// getter方法
public String getId() { return id; }
public String getName() { return name; }
@Override
public String toString() {
return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}';
}
}
Course类
public class Course {
private String courseId;
private String courseName;
private int maxStudents;
private int currentCount;
public Course(String courseId, String courseName, int maxStudents) {
this.courseId = courseId;
this.courseName = courseName;
this.maxStudents = maxStudents;
this.currentCount = 0;
}
public boolean isFull() {
return currentCount >= maxStudents;
}
public void enroll() {
if (!isFull()) {
currentCount++;
}
}
public void drop() {
if (currentCount > 0) {
currentCount--;
}
}
// getter方法
public String getCourseId() { return courseId; }
public String getCourseName() { return courseName; }
@Override
public String toString() {
return "Course{" + "courseId='" + courseId + '\'' +
", courseName='" + courseName + '\'' +
", capacity=" + currentCount + "/" + maxStudents + '}';
}
}
CourseManager类(核心逻辑)
import java.util.*;
public class CourseManager {
private Map<String, Student> students = new HashMap<>();
private Map<String, Course> courses = new HashMap<>();
private Map<String, List<String>> studentEnrollments = new HashMap<>(); // 学生ID -> 课程ID列表
public void addStudent(Student student) {
students.put(student.getId(), student);
studentEnrollments.put(student.getId(), new ArrayList<>());
}
public void addCourse(Course course) {
courses.put(course.getCourseId(), course);
}
public boolean selectCourse(String studentId, String courseId) {
if (!students.containsKey(studentId)) {
System.out.println("学生不存在");
return false;
}
if (!courses.containsKey(courseId)) {
System.out.println("课程不存在");
return false;
}
Course course = courses.get(courseId);
if (course.isFull()) {
System.out.println("课程已满");
return false;
}
List<String> studentCourses = studentEnrollments.get(studentId);
if (studentCourses.contains(courseId)) {
System.out.println("该学生已选此课程");
return false;
}
studentCourses.add(courseId);
course.enroll();
System.out.println("选课成功");
return true;
}
public void dropCourse(String studentId, String courseId) {
List<String> studentCourses = studentEnrollments.get(studentId);
if (studentCourses != null && studentCourses.remove(courseId)) {
Course course = courses.get(courseId);
if (course != null) {
course.drop();
}
System.out.println("退课成功");
} else {
System.out.println("未找到该选课记录");
}
}
public void printStudentCourses(String studentId) {
Student student = students.get(studentId);
if (student == null) {
System.out.println("学生不存在");
return;
}
List<String> courseIds = studentEnrollments.get(studentId);
System.out.println(student.getName() + " 的选课列表:");
for (String cid : courseIds) {
Course c = courses.get(cid);
if (c != null) {
System.out.println(" " + c);
}
}
}
public void printCourseStudents(String courseId) {
Course course = courses.get(courseId);
if (course == null) {
System.out.println("课程不存在");
return;
}
System.out.println(course.getCourseName() + " 的学生列表:");
for (Map.Entry<String, List<String>> entry : studentEnrollments.entrySet()) {
if (entry.getValue().contains(courseId)) {
Student s = students.get(entry.getKey());
System.out.println(" " + s);
}
}
}
}
编写main方法进行测试:
public class Main {
public static void main(String[] args) {
CourseManager cm = new CourseManager();
// 添加学生
cm.addStudent(new Student("S001", "张三"));
cm.addStudent(new Student("S002", "李四"));
// 添加课程
cm.addCourse(new Course("C001", "Java编程", 2));
cm.addCourse(new Course("C002", "数据结构", 1));
// 选课
cm.selectCourse("S001", "C001");
cm.selectCourse("S002", "C001");
cm.selectCourse("S001", "C002"); // 张三再选一门
// 查看结果
cm.printStudentCourses("S001");
cm.printCourseStudents("C001");
}
}
这个系统虽然简单,但涵盖了Java开发中常见的设计思路和编码技巧。你可以在此基础上扩展功能,比如加入文件持久化、图形界面、异常处理等。基本上就这些,动手试试吧。
以上就是Java中学生选课管理系统实战的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号