在IDEA用Java设计一个简单的学生选课系统,并可以完成添加、删除、修改、查询相关信息 。 说明: 可以设计以下类: 描述课程基本信息类(课程编号,课程名,学分 ); 描述学生基本信息类(学号,姓名,专业方向 ); 记录选课结果的类(课程,选课的学生,课程最多人数,当前人数 ); 测试类(设定或输入最多人数,创建课程对象,创建选课学生对象,创建选课对象,显示选课情况 )
时间: 2024-03-03 08:54:01 浏览: 61
好的,这是一个非常好的问题,我将尽力回答你。首先,我们需要创建三个类:Course、Student、选课结果,并且需要创建一个测试类 TestClass。
下面是每个类的详细信息:
1.描述课程基本信息类(课程编号,课程名,学分 );
```
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;
}
}
```
2.描述学生基本信息类(学号,姓名,专业方向 );
```
public class Student {
private String studentId;
private String studentName;
private String major;
public Student(String studentId, String studentName, String major) {
this.studentId = studentId;
this.studentName = studentName;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
3.记录选课结果的类(课程,选课的学生,课程最多人数,当前人数 );
```
public class CourseSelection {
private Course course;
private List<Student> selectedStudents;
private int maxStudents;
private int currentStudents;
public CourseSelection(Course course, int maxStudents) {
this.course = course;
this.maxStudents = maxStudents;
this.currentStudents = 0;
this.selectedStudents = new ArrayList<Student>();
}
public boolean addStudent(Student student) {
if (currentStudents >= maxStudents) {
return false;
}
selectedStudents.add(student);
currentStudents++;
return true;
}
public boolean removeStudent(Student student) {
boolean result = selectedStudents.remove(student);
if (result) {
currentStudents--;
}
return result;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List<Student> getSelectedStudents() {
return selectedStudents;
}
public void setSelectedStudents(List<Student> selectedStudents) {
this.selectedStudents = selectedStudents;
}
public int getMaxStudents() {
return maxStudents;
}
public void setMaxStudents(int maxStudents) {
this.maxStudents = maxStudents;
}
public int getCurrentStudents() {
return currentStudents;
}
public void setCurrentStudents(int currentStudents) {
this.currentStudents = currentStudents;
}
}
```
4.测试类(设定或输入最多人数,创建课程对象,创建选课学生对象,创建选课对象,显示选课情况 )
```
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程编号:");
String courseId = scanner.nextLine();
System.out.println("请输入课程名称:");
String courseName = scanner.nextLine();
System.out.println("请输入课程学分:");
int credit = scanner.nextInt();
Course course = new Course(courseId, courseName, credit);
System.out.println("请输入最大学生人数:");
int maxStudents = scanner.nextInt();
CourseSelection courseSelection = new CourseSelection(course, maxStudents);
System.out.println("请输入学生人数:");
int studentCount = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < studentCount; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的学号:");
String studentId = scanner.nextLine();
System.out.println("请输入第" + (i + 1) + "个学生的姓名:");
String studentName = scanner.nextLine();
System.out.println("请输入第" + (i + 1) + "个学生的专业:");
String major = scanner.nextLine();
Student student = new Student(studentId, studentName, major);
boolean result = courseSelection.addStudent(student);
if (result) {
System.out.println("学生" + studentName + "选课成功!");
} else {
System.out.println("学生" + studentName + "选课失败!");
}
}
System.out.println("课程信息:");
System.out.println("课程编号:" + course.getCourseId());
System.out.println("课程名称:" + course.getCourseName());
System.out.println("课程学分:" + course.getCredit());
System.out.println("选课情况:");
List<Student> selectedStudents = courseSelection.getSelectedStudents();
for (int i = 0; i < selectedStudents.size(); i++) {
Student student = selectedStudents.get(i);
System.out.println("学生" + student.getStudentName() + "选课成功!");
}
}
}
```
这样就完成了一个简单的学生选课系统,可以完成添加、删除、修改、查询相关信息。请注意,这只是一个简单的示例,实际应用中需要更多的功能和更复杂的逻辑来实现。
阅读全文