Java学生选课管理系统开发实战
作者:吹吹风会发光
时间:2026-01-14
来源:互联网
浏览:0
学生选课管理系统通过Student、Course和CourseManager类实现,支持学生与课程的增删查及选课退课功能,利用集合存储数据并进行关联操作,最后通过测试类验证核心逻辑正确性。
学生选课管理系统通过Student、Course和CourseManager类实现,支持学生与课程的增删查及选课退课功能,利用集合存储数据并进行关联操作,最后通过测试类验证核心逻辑正确性。

学生选课管理系统是Java初学者常见的实战项目,能综合运用面向对象编程、集合框架、文件读写等核心知识。下面从需求分析到代码实现,带你一步步完成一个基础但完整的系统。
系统功能设计
一个基本的学生选课管理系统应包含以下功能:
- 学生管理:添加、查询学生信息
- 课程管理:添加、查看课程
- 选课操作:学生选择课程,支持退课
- 查询选课情况:查看某学生所选课程或某课程的选课学生
类结构设计
根据功能划分,系统主要需要三个类:
- Student:包含学号、姓名等属性
- Course:包含课程编号、名称、最大容量等
- CourseManager:负责管理学生、课程和选课关系
Student类
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 students = new HashMap<>();
private Map courses = new HashMap<>();
private Map> 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 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 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 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> 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开发中常见的设计思路和编码技巧。你可以在此基础上扩展功能,比如加入文件持久化、图形界面、异常处理等。基本上就这些,动手试试吧。
作者最新文章
iphone蓝牙连接ipad有什么用及连接方法教程
2026-09-21 17:28
PDF怎么取消密码保护?4种解锁方法整理
2026-09-08 18:23
三星 Galaxy Z Fold8 内屏边角支撑偏软?实测与官方回应
2026-09-08 16:39
手机Excel表格制作教程:小屏幕高效录入与格式调整指南
2026-09-04 09:27
PDF文档按页转换成图片怎么做?在线转换步骤整理
2026-09-03 11:06
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















