用java设计一个教材类,一个课程类,及一个为某门课程指定参考教材的类。一门课程可以有多本参考教材,再设计一个测试类。 设计一个简单的学生选课系统,并可以完成添加、删除、修改、查询相关信息 。 说明:设计以下类: 描述课程基本信息类(课程编号,课程名,学分 ); 描述学生基本信息类(学号,姓名,专业方向 ); 记录选课结果的类(课程,选课的学生,课程最多人数,当前人数 ); 测试类(设定或输入最多人数,创建课程对象,创建选课学生对象,创建选课对象,显示选课情况 )可以在idea中运行。
时间: 2024-03-06 21:46:33 浏览: 78
好的,我会根据您的要求设计这些类。首先是教材类:
```java
public class Textbook {
private String name;
private String author;
private double price;
public Textbook(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
接下来是课程类:
```java
import java.util.ArrayList;
public class Course {
private String code;
private String name;
private int credit;
private ArrayList<Textbook> textbooks;
public Course(String code, String name, int credit) {
this.code = code;
this.name = name;
this.credit = credit;
this.textbooks = new ArrayList<>();
}
public void addTextbook(Textbook textbook) {
textbooks.add(textbook);
}
// Getters and setters
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
public ArrayList<Textbook> getTextbooks() {
return textbooks;
}
public void setTextbooks(ArrayList<Textbook> textbooks) {
this.textbooks = textbooks;
}
}
```
然后是为某门课程指定参考教材的类:
```java
public class CourseTextbook {
private Course course;
private Textbook textbook;
public CourseTextbook(Course course, Textbook textbook) {
this.course = course;
this.textbook = textbook;
course.addTextbook(textbook);
}
// Getters and setters
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Textbook getTextbook() {
return textbook;
}
public void setTextbook(Textbook textbook) {
this.textbook = textbook;
}
}
```
下面是描述学生基本信息类:
```java
public class Student {
private String id;
private String name;
private String major;
public Student(String id, String name, String major) {
this.id = id;
this.name = name;
this.major = major;
}
// Getters and setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
```
然后是记录选课结果的类:
```java
import java.util.ArrayList;
public class Enrollment {
private Course course;
private int maxStudents;
private ArrayList<Student> students;
public Enrollment(Course course, int maxStudents) {
this.course = course;
this.maxStudents = maxStudents;
this.students = new ArrayList<>();
}
public boolean addStudent(Student student) {
if (students.size() < maxStudents) {
students.add(student);
return true;
}
return false;
}
public boolean removeStudent(Student student) {
return students.remove(student);
}
// Getters and setters
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public int getMaxStudents() {
return maxStudents;
}
public void setMaxStudents(int maxStudents) {
this.maxStudents = maxStudents;
}
public ArrayList<Student> getStudents() {
return students;
}
public void setStudents(ArrayList<Student> students) {
this.students = students;
}
}
```
最后是测试类:
```java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// 设定最多人数
int maxStudents = 50;
// 创建课程对象
Course math = new Course("MATH101", "Calculus", 3);
Course physics = new Course("PHYS101", "Mechanics", 4);
// 创建教材对象
Textbook mathTextbook1 = new Textbook("Calculus, 10th edition", "Ron Larson, Bruce H. Edwards", 100.0);
Textbook mathTextbook2 = new Textbook("Calculus, 11th edition", "Ron Larson, Bruce H. Edwards", 120.0);
Textbook physicsTextbook1 = new Textbook("Classical Mechanics, 3rd edition", "Herbert Goldstein", 150.0);
// 为课程指定参考教材
CourseTextbook ct1 = new CourseTextbook(math, mathTextbook1);
CourseTextbook ct2 = new CourseTextbook(math, mathTextbook2);
CourseTextbook ct3 = new CourseTextbook(physics, physicsTextbook1);
// 创建学生对象
Student s1 = new Student("101", "Tom", "Mathematics");
Student s2 = new Student("102", "Jerry", "Physics");
Student s3 = new Student("103", "Lucy", "Mathematics");
// 创建选课对象
Enrollment mathEnrollment = new Enrollment(math, maxStudents);
Enrollment physicsEnrollment = new Enrollment(physics, maxStudents);
// 添加学生到选课对象中
mathEnrollment.addStudent(s1);
mathEnrollment.addStudent(s3);
physicsEnrollment.addStudent(s2);
// 显示选课情况
System.out.println("Math enrollment: " + mathEnrollment.getStudents().size() + "/" + mathEnrollment.getMaxStudents());
for (Student student : mathEnrollment.getStudents()) {
System.out.println(student.getName());
}
System.out.println("Physics enrollment: " + physicsEnrollment.getStudents().size() + "/" + physicsEnrollment.getMaxStudents());
for (Student student : physicsEnrollment.getStudents()) {
System.out.println(student.getName());
}
}
}
```
这个简单的学生选课系统可以实现添加、删除、修改、查询相关信息。您可以根据自己的需求进行扩展。
阅读全文