首先设计Course和Student类,分别包含课程与学生的基本属性,并通过CourseRegistrationService管理报名逻辑;利用Map存储课程和学生信息,实现报名、退课与查询功能;在报名时检查课程是否已满、学生是否重复报名,确保数据一致性;最后通过测试用例验证系统正确性。该方案适用于小型应用,具备清晰的结构与完整的边界处理。

实现课程报名管理功能,核心是设计好数据模型和业务逻辑。Java中可以通过面向对象的方式构建课程、学生和报名关系,并结合集合类来管理数据。以下是具体实现思路和代码示例。
先创建两个基础类:Course(课程)和Student(学生),用于表示基本实体。
示例代码:
class Course {
private String courseId;
private String name;
private int maxStudents;
private List<Student> enrolledStudents;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">public Course(String courseId, String name, int maxStudents) {
this.courseId = courseId;
this.name = name;
this.maxStudents = maxStudents;
this.enrolledStudents = new ArrayList<>();
}
// getter 方法
public String getCourseId() { return courseId; }
public String getName() { return name; }
public int getMaxStudents() { return maxStudents; }
public List<Student> getEnrolledStudents() { return enrolledStudents; }
public int getCurrentEnrollment() {
return enrolledStudents.size();
}
public boolean isFull() {
return getCurrentEnrollment() >= maxStudents;
}}
立即学习“Java免费学习笔记(深入)”;
class Student { private String studentId; private String name;
public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
}
// getter 方法
public String getStudentId() { return studentId; }
public String getName() { return name; }
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student s = (Student) o;
return studentId.equals(s.studentId);
}
public int hashCode() {
return studentId.hashCode();
}}
创建一个 CourseRegistrationService 类来处理报名逻辑,包括添加学生、取消报名、查询状态等功能。
示例代码:
class CourseRegistrationService {
private Map<String, Course> courses;
private Map<String, Student> students;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">public CourseRegistrationService() {
this.courses = new HashMap<>();
this.students = new HashMap<>();
}
public void addCourse(Course course) {
courses.put(course.getCourseId(), course);
}
public void addStudent(Student student) {
students.put(student.getStudentId(), student);
}
public boolean registerStudent(String courseId, String studentId) {
Course course = courses.get(courseId);
Student student = students.get(studentId);
if (course == null || student == null) {
System.out.println("课程或学生不存在");
return false;
}
if (course.isFull()) {
System.out.println("课程 " + course.getName() + " 已满");
return false;
}
if (course.getEnrolledStudents().contains(student)) {
System.out.println("该学生已报名此课程");
return false;
}
course.getEnrolledStudents().add(student);
System.out.println(student.getName() + " 成功报名 " + course.getName());
return true;
}
public boolean unregisterStudent(String courseId, String studentId) {
Course course = courses.get(courseId);
Student student = students.get(studentId);
if (course != null && student != null) {
if (course.getEnrolledStudents().remove(student)) {
System.out.println(student.getName() + " 已退出 " + course.getName());
return true;
} else {
System.out.println("该学生未报名此课程");
}
} else {
System.out.println("课程或学生不存在");
}
return false;
}
public void printCourseInfo(String courseId) {
Course course = courses.get(courseId);
if (course != null) {
System.out.println("课程: " + course.getName());
System.out.println("已报名人数: " + course.getCurrentEnrollment() +
"/" + course.getMaxStudents());
System.out.println("学员列表:");
for (Student s : course.getEnrolledStudents()) {
System.out.println(" - " + s.getName() + " (" + s.getStudentId() + ")");
}
} else {
System.out.println("课程不存在");
}
}}
编写主程序验证功能是否正常运行。
public class RegistrationDemo {
public static void main(String[] args) {
CourseRegistrationService service = new CourseRegistrationService();
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;"> // 创建课程
Course javaCourse = new Course("C001", "Java编程", 2);
Course webCourse = new Course("C002", "Web开发", 3);
// 创建学生
Student s1 = new Student("S001", "张三");
Student s2 = new Student("S002", "李四");
Student s3 = new Student("S003", "王五");
// 注册课程和学生
service.addCourse(javaCourse);
service.addCourse(webCourse);
service.addStudent(s1);
service.addStudent(s2);
service.addStudent(s3);
// 报名操作
service.registerStudent("C001", "S001");
service.registerStudent("C001", "S002");
service.registerStudent("C001", "S003"); // 应提示已满
service.registerStudent("C002", "S001");
service.registerStudent("C002", "S002");
// 打印信息
service.printCourseInfo("C001");
service.printCourseInfo("C002");
}}
基本上就这些。这个实现适合小型系统,如果需要持久化,可以引入数据库或文件存储。关键点在于控制报名边界条件,比如重复报名、超员等情况的处理。结构清晰、逻辑完整就能稳定运行。
以上就是在Java中如何实现课程报名管理功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号