学生选课管理系统java数据结构代码
时间: 2024-03-10 11:41:57 浏览: 79
学生选课管理系统是一个常见的项目,它可以使用Java语言和数据结构来实现。下面是一个简单的示例代码,用于演示学生选课管理系统的基本功能:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Course {
private String courseId;
private String courseName;
public Course(String courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
}
public String getCourseId() {
return courseId;
}
public String getCourseName() {
return courseName;
}
}
class Student {
private String studentId;
private String studentName;
private List<Course> courses;
public Student(String studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
this.courses = new ArrayList<>();
}
public String getStudentId() {
return studentId;
}
public String getStudentName() {
return studentName;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
}
class CourseManagementSystem {
private Map<String, Course> courseMap;
private Map<String, Student> studentMap;
public CourseManagementSystem() {
this.courseMap = new HashMap<>();
this.studentMap = new HashMap<>();
}
public void addCourse(String courseId, String courseName) {
Course course = new Course(courseId, courseName);
courseMap.put(courseId, course);
}
public void addStudent(String studentId, String studentName) {
Student student = new Student(studentId, studentName);
studentMap.put(studentId, student);
}
public void enrollCourse(String studentId, String courseId) {
if (studentMap.containsKey(studentId) && courseMap.containsKey(courseId)) {
Student student = studentMap.get(studentId);
Course course = courseMap.get(courseId);
student.addCourse(course);
}
}
public void dropCourse(String studentId, String courseId) {
if (studentMap.containsKey(studentId) && courseMap.containsKey(courseId)) {
Student student = studentMap.get(studentId);
Course course = courseMap.get(courseId);
student.removeCourse(course);
}
}
public List<Course> getStudentCourses(String studentId) {
if (studentMap.containsKey(studentId)) {
Student student = studentMap.get(studentId);
return student.getCourses();
}
return new ArrayList<>();
}
}
public class Main {
public static void main(String[] args) {
CourseManagementSystem cms = new CourseManagementSystem();
// 添加课程
cms.addCourse("C001", "Java Programming");
cms.addCourse("C002", "Data Structures");
cms.addCourse("C003", "Database Management");
// 添加学生
cms.addStudent("S001", "Alice");
cms.addStudent("S002", "Bob");
// 学生选课
cms.enrollCourse("S001", "C001");
cms.enrollCourse("S001", "C002");
cms.enrollCourse("S002", "C002");
cms.enrollCourse("S002", "C003");
// 获取学生选课信息
List<Course> aliceCourses = cms.getStudentCourses("S001");
List<Course> bobCourses = cms.getStudentCourses("S002");
System.out.println("Alice's courses:");
for (Course course : aliceCourses) {
System.out.println(course.getCourseId() + " - " + course.getCourseName());
}
System.out.println("Bob's courses:");
for (Course course : bobCourses) {
System.out.println(course.getCourseId() + " - " + course.getCourseName());
}
}
}
```
这段代码实现了一个简单的学生选课管理系统,包括添加课程、添加学生、学生选课、退课以及获取学生选课信息等功能。你可以根据自己的需求进行扩展和修改。
阅读全文