制作一个学生选课系统,系统包括学号,姓名,性别,年级,密码。课程要包括课程编号,课程名称,已选人数,可选人数,授课老师。文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程
时间: 2023-09-06 22:12:21 浏览: 106
基于C语言实现的学生选课管理系统源码(C语言课程大作业).zip
5星 · 资源好评率100%
是的,你说得完全正确。下面是一个简单的Java代码示例,可以实现你所需要的学生选课系统:
```java
import java.util.ArrayList;
import java.util.List;
public class Student {
private String id;
private String name;
private String gender;
private int grade;
private String password;
private List<Course> courses;
public Student(String id, String name, String gender, int grade, String password) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
this.courses = new ArrayList<>();
}
public void selectCourse(Course course) {
if (course.getAvailableSeats() > 0) {
courses.add(course);
course.addStudent();
} else {
System.out.println("This course is full.");
}
}
public void dropCourse(Course course) {
if (courses.contains(course)) {
courses.remove(course);
course.removeStudent();
} else {
System.out.println("You haven't selected this course.");
}
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getGrade() {
return grade;
}
public String getPassword() {
return password;
}
public List<Course> getCourses() {
return courses;
}
}
class Course {
private String id;
private String name;
private int selectedSeats;
private int availableSeats;
private String teacher;
public Course(String id, String name, int availableSeats, String teacher) {
this.id = id;
this.name = name;
this.selectedSeats = 0;
this.availableSeats = availableSeats;
this.teacher = teacher;
}
public void addStudent() {
selectedSeats++;
availableSeats--;
}
public void removeStudent() {
selectedSeats--;
availableSeats++;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getSelectedSeats() {
return selectedSeats;
}
public int getAvailableSeats() {
return availableSeats;
}
public String getTeacher() {
return teacher;
}
}
class CourseDB {
private static CourseDB instance;
private List<Course> courses;
private CourseDB() {
courses = new ArrayList<>();
courses.add(new Course("001", "Math", 30, "Tom"));
courses.add(new Course("002", "English", 50, "Jerry"));
courses.add(new Course("003", "Computer Science", 20, "John"));
courses.add(new Course("004", "Physics", 40, "Lucy"));
courses.add(new Course("005", "Chemistry", 35, "Bob"));
courses.add(new Course("006", "Biology", 25, "Lily"));
courses.add(new Course("007", "History", 30, "David"));
courses.add(new Course("008", "Geography", 30, "Frank"));
courses.add(new Course("009", "Music", 15, "Mary"));
courses.add(new Course("010", "Art", 20, "Kate"));
}
public static CourseDB getInstance() {
if (instance == null) {
instance = new CourseDB();
}
return instance;
}
public List<Course> getCourses() {
return courses;
}
}
public class Main {
public static void main(String[] args) {
CourseDB courseDB = CourseDB.getInstance();
List<Course> courses = courseDB.getCourses();
Student student1 = new Student("001", "Tom", "Male", 1, "123456");
Student student2 = new Student("002", "Lucy", "Female", 2, "123456");
System.out.println("Course List:");
for (Course course : courses) {
System.out.println(course.getId() + " " + course.getName() + " " + course.getTeacher() + " " + course.getAvailableSeats());
}
System.out.println("\nStudent 1 selects courses:");
student1.selectCourse(courses.get(0));
student1.selectCourse(courses.get(1));
student1.selectCourse(courses.get(2));
for (Course course : student1.getCourses()) {
System.out.println(course.getId() + " " + course.getName() + " " + course.getTeacher());
}
System.out.println("\nStudent 2 selects courses:");
student2.selectCourse(courses.get(2));
student2.selectCourse(courses.get(3));
for (Course course : student2.getCourses()) {
System.out.println(course.getId() + " " + course.getName() + " " + course.getTeacher());
}
System.out.println("\nStudent 1 drops a course:");
student1.dropCourse(courses.get(1));
for (Course course : student1.getCourses()) {
System.out.println(course.getId() + " " + course.getName() + " " + course.getTeacher());
}
}
}
```
上面的代码演示了如何创建一个名为CourseDB的单例类,模拟数据库中的课程。初始化后,它至少包含10门课程。此外,还定义了一个名为Student的类,表示学生,它拥有一些基本属性,如学号、姓名、性别、年级和密码,以及一个名为courses的列表来存储所选的课程。在Student类中定义了两个方法:selectCourse和dropCourse,用于选课和退课。最后,还定义了一个名为Course的类,表示课程,它拥有一些基本属性,如课程编号、课程名称、已选人数、可选人数和授课老师。
在主函数中,我们首先获取CourseDB中的所有课程,然后创建两个学生对象,分别选取一些课程,并演示了如何退课。
阅读全文