用java设计一个教材类,一个课程类,及一个为某门课程指定参考教材的类。一门课程可以有多本参考教材,再设计一个测试类。 设计一个简单的学生选课系统,并可以完成添加、删除、修改、查询相关信息 。 说明:设计以下类: 描述课程基本信息类(课程编号,课程名,学分 ); 描述学生基本信息类(学号,姓名,专业方向 ); 记录选课结果的类(课程,选课的学生,课程最多人数,当前人数 ); 测试类(设定或输入最多人数,创建课程对象,创建选课学生对象,创建选课对象,显示选课情况 )
时间: 2024-03-06 17:46:16 浏览: 51
好的,以下是Java代码实现:
Course.java
```java
public class Course {
private String courseId;
private String courseName;
private int credit;
public Course(String courseId, String courseName, int credit) {
this.courseId = courseId;
this.courseName = courseName;
this.credit = credit;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
}
```
Textbook.java
```java
public class Textbook {
private Course course;
private String textbookName;
public Textbook(Course course, String textbookName) {
this.course = course;
this.textbookName = textbookName;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public String getTextbookName() {
return textbookName;
}
public void setTextbookName(String textbookName) {
this.textbookName = textbookName;
}
}
```
Student.java
```java
public class Student {
private String studentId;
private String name;
private String major;
public Student(String studentId, String name, String major) {
this.studentId = studentId;
this.name = name;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
Enrollment.java
```java
import java.util.ArrayList;
import java.util.List;
public class Enrollment {
private Course course;
private List<Student> students = new ArrayList<>();
private int maxNumOfStudents;
private int currentNumOfStudents;
public Enrollment(Course course, int maxNumOfStudents) {
this.course = course;
this.maxNumOfStudents = maxNumOfStudents;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getMaxNumOfStudents() {
return maxNumOfStudents;
}
public void setMaxNumOfStudents(int maxNumOfStudents) {
this.maxNumOfStudents = maxNumOfStudents;
}
public int getCurrentNumOfStudents() {
return currentNumOfStudents;
}
public void setCurrentNumOfStudents(int currentNumOfStudents) {
this.currentNumOfStudents = currentNumOfStudents;
}
public void addStudent(Student student) {
if (currentNumOfStudents < maxNumOfStudents) {
students.add(student);
currentNumOfStudents++;
System.out.println("Add student " + student.getName() + " to course " + course.getCourseName() + " successfully.");
} else {
System.out.println("Failed to add student " + student.getName() + " to course " + course.getCourseName()
+ ". The course is full.");
}
}
public void removeStudent(Student student) {
if (students.remove(student)) {
currentNumOfStudents--;
System.out.println("Remove student " + student.getName() + " from course " + course.getCourseName() + " successfully.");
} else {
System.out.println("Failed to remove student " + student.getName() + " from course " + course.getCourseName()
+ ". The student is not in the course.");
}
}
public void modifyCourse(Course course) {
this.course = course;
System.out.println("Modify course " + course.getCourseName() + " successfully.");
}
public void queryEnrollment() {
System.out.println("Course " + course.getCourseName() + " currently has " + currentNumOfStudents + " students:");
for (Student student : students) {
System.out.println(student.getName() + " (" + student.getStudentId() + ")");
}
}
}
```
Test.java
```java
public class Test {
public static void main(String[] args) {
int maxNumOfStudents = 3;
Course javaCourse = new Course("C001", "Java Programming", 3);
Student student1 = new Student("S001", "Tom", "Computer Science");
Student student2 = new Student("S002", "Jerry", "Computer Science");
Student student3 = new Student("S003", "Kate", "Computer Science");
Enrollment enrollment = new Enrollment(javaCourse, maxNumOfStudents);
enrollment.addStudent(student1);
enrollment.addStudent(student2);
enrollment.addStudent(student3);
enrollment.queryEnrollment();
enrollment.removeStudent(student2);
enrollment.queryEnrollment();
Course pythonCourse = new Course("C002", "Python Programming", 4);
enrollment.modifyCourse(pythonCourse);
enrollment.queryEnrollment();
Textbook javaTextbook1 = new Textbook(javaCourse, "Thinking in Java");
Textbook javaTextbook2 = new Textbook(javaCourse, "Java: A Beginner's Guide");
Textbook pythonTextbook = new Textbook(pythonCourse, "Learning Python");
System.out.println("Course " + javaCourse.getCourseName() + " has the following textbooks:");
System.out.println(javaTextbook1.getTextbookName());
System.out.println(javaTextbook2.getTextbookName());
}
}
```
阅读全文