Java实现学生选课系统
时间: 2023-08-30 14:07:22 浏览: 161
学生选课系统可以使用 Java 实现,以下是一个简单的实现示例:
1. 定义学生类
```java
public class Student {
private int id;
private String name;
private List<Course> courses;
public Student(int id, String name) {
this.id = id;
this.name = name;
this.courses = new ArrayList<>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
}
```
2. 定义课程类
```java
public class Course {
private int id;
private String name;
private int capacity;
private List<Student> students;
public Course(int id, String name, int capacity) {
this.id = id;
this.name = name;
this.capacity = capacity;
this.students = new ArrayList<>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public List<Student> getStudents() {
return students;
}
public boolean addStudent(Student student) {
if (students.size() < capacity) {
students.add(student);
student.addCourse(this);
return true;
}
return false;
}
public void removeStudent(Student student) {
students.remove(student);
student.removeCourse(this);
}
}
```
3. 定义选课系统类
```java
public class CourseSelectionSystem {
private List<Student> students;
private List<Course> courses;
public CourseSelectionSystem() {
this.students = new ArrayList<>();
this.courses = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void addCourse(Course course) {
courses.add(course);
}
public boolean enroll(int studentId, int courseId) {
Student student = getStudent(studentId);
Course course = getCourse(courseId);
if (student != null && course != null) {
return course.addStudent(student);
}
return false;
}
public boolean drop(int studentId, int courseId) {
Student student = getStudent(studentId);
Course course = getCourse(courseId);
if (student != null && course != null) {
course.removeStudent(student);
return true;
}
return false;
}
private Student getStudent(int studentId) {
for (Student student : students) {
if (student.getId() == studentId) {
return student;
}
}
return null;
}
private Course getCourse(int courseId) {
for (Course course : courses) {
if (course.getId() == courseId) {
return course;
}
}
return null;
}
}
```
使用示例:
```java
// 创建学生和课程
Student s1 = new Student(1, "Alice");
Student s2 = new Student(2, "Bob");
Course c1 = new Course(1, "Java Programming", 3);
Course c2 = new Course(2, "Data Structures", 2);
// 添加学生和课程到选课系统
CourseSelectionSystem system = new CourseSelectionSystem();
system.addStudent(s1);
system.addStudent(s2);
system.addCourse(c1);
system.addCourse(c2);
// 学生选课
system.enroll(1, 1); // Alice选Java Programming课,成功
system.enroll(1, 2); // Alice选Data Structures课,成功
system.enroll(2, 1); // Bob选Java Programming课,成功
system.enroll(2, 2); // Bob选Data Structures课,失败,因为课程已满
// 学生退课
system.drop(1, 2); // Alice退掉Data Structures课
```
这只是一个简单的实现示例,实际的选课系统可能需要更多的功能和复杂性。
阅读全文