教务系统来说,选课是其所必备的功能,请实现一个选课模块,要求: 1、选择合适的集合 2、能够查询课程以及剩余的数量 3、能够查询学生所选的课,也可以通过课反向查询课下面的学生(显示学号+姓名) 4、选课需要记录具体学生。用java的hashmap完成
时间: 2023-07-16 10:13:47 浏览: 94
好的,下面是一个简单的选课模块的Java实现,使用HashMap来存储课程和学生的信息:
```java
import java.util.*;
public class CourseSelection {
// 用HashMap来存储课程和学生信息
private Map<String, Course> courses = new HashMap<>();
private Map<String, Student> students = new HashMap<>();
// 添加课程
public void addCourse(String courseName, int capacity) {
Course course = new Course(courseName, capacity);
courses.put(courseName, course);
}
// 添加学生
public void addStudent(String studentId, String name) {
Student student = new Student(studentId, name);
students.put(studentId, student);
}
// 选课
public boolean selectCourse(String studentId, String courseName) {
Student student = students.get(studentId);
Course course = courses.get(courseName);
if (student == null || course == null) {
return false;
}
if (student.getCourses().contains(course)) {
return true; // 已选过该课程
}
if (course.getRemaining() <= 0) {
return false; // 课程已满
}
student.addCourse(course);
course.addStudent(student);
return true;
}
// 查询课程信息
public void printCourseInfo(String courseName) {
Course course = courses.get(courseName);
if (course == null) {
System.out.println("该课程不存在!");
return;
}
System.out.println("课程名称:" + course.getName());
System.out.println("课程容量:" + course.getCapacity());
System.out.println("剩余容量:" + course.getRemaining());
}
// 查询学生选课信息
public void printStudentInfo(String studentId) {
Student student = students.get(studentId);
if (student == null) {
System.out.println("该学生不存在!");
return;
}
System.out.println("学生姓名:" + student.getName());
System.out.println("学生编号:" + student.getId());
System.out.println("所选课程:");
List<Course> courses = student.getCourses();
for (Course course : courses) {
System.out.println(course.getName());
}
}
// 查询某课程下的学生信息
public void printCourseStudents(String courseName) {
Course course = courses.get(courseName);
if (course == null) {
System.out.println("该课程不存在!");
return;
}
System.out.println("课程名称:" + course.getName());
System.out.println("选课学生:");
List<Student> students = course.getStudents();
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName());
}
}
public static void main(String[] args) {
CourseSelection selection = new CourseSelection();
selection.addCourse("Java程序设计", 50);
selection.addCourse("数据结构", 40);
selection.addCourse("计算机组成原理", 30);
selection.addStudent("10001", "张三");
selection.addStudent("10002", "李四");
selection.addStudent("10003", "王五");
selection.selectCourse("10001", "Java程序设计");
selection.selectCourse("10001", "数据结构");
selection.selectCourse("10002", "Java程序设计");
selection.selectCourse("10003", "Java程序设计");
selection.printCourseInfo("Java程序设计");
selection.printStudentInfo("10001");
selection.printCourseStudents("Java程序设计");
}
}
// 课程类
class Course {
private String name;
private int capacity;
private List<Student> students = new ArrayList<>();
public Course(String name, int capacity) {
this.name = name;
this.capacity = capacity;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public int getRemaining() {
return capacity - students.size();
}
public List<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
students.add(student);
}
}
// 学生类
class Student {
private String id;
private String name;
private List<Course> courses = new ArrayList<>();
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
}
```
以上代码演示了一个简单的选课系统,可以添加课程和学生信息,选课,并查询课程和学生的选课信息。其中,课程和学生信息以HashMap进行存储,选课时需要对课程和学生的存在性进行判断,同时需要记录学生选课信息和课程选课学生信息。
阅读全文