用Java编写设计一个简单的学生选课系统,并可以完成添加、删除、修改、查询相关信息 。 说明: 可以设计以下类: 描述课程基本信息类(课程编号,课程名,学分 ); 描述学生基本信息类(学号,姓名,专业方向 ); 记录选课结果的类(课程,选课的学生,课程最多人数,当前人数 ); 测试类(设定或输入最多人数,创建课程对象,创建选课学生对象,创建选课对象,显示选课情况 )
时间: 2024-03-11 16:44:20 浏览: 30
好的,以下是一个简单的学生选课系统的实现(Java语言):
Course类:
```
public class Course {
private String courseId;
private String courseName;
private int credit;
private int maxNum;
private int currentNum;
public Course(String courseId, String courseName, int credit, int maxNum) {
this.courseId = courseId;
this.courseName = courseName;
this.credit = credit;
this.maxNum = maxNum;
this.currentNum = 0;
}
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;
}
public int getMaxNum() {
return maxNum;
}
public void setMaxNum(int maxNum) {
this.maxNum = maxNum;
}
public int getCurrentNum() {
return currentNum;
}
public void setCurrentNum(int currentNum) {
this.currentNum = currentNum;
}
public boolean isFull() {
return currentNum >= maxNum;
}
@Override
public String toString() {
return "Course{" +
"courseId='" + courseId + '\'' +
", courseName='" + courseName + '\'' +
", credit=" + credit +
", maxNum=" + maxNum +
", currentNum=" + currentNum +
'}';
}
}
```
Student类:
```
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;
}
@Override
public String toString() {
return "Student{" +
"studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", major='" + major + '\'' +
'}';
}
}
```
选课结果类:
```
public class Selection {
private Course course;
private Student student;
public Selection(Course course, Student student) {
this.course = course;
this.student = student;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Selection{" +
"course=" + course +
", student=" + student +
'}';
}
}
```
测试类:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
List<Student> students = new ArrayList<>();
List<Selection> selections = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入课程最多人数:");
int maxNum = scanner.nextInt();
Course course1 = new Course("001", "Java程序设计", 4, maxNum);
Course course2 = new Course("002", "数据结构与算法", 3, maxNum);
courses.add(course1);
courses.add(course2);
Student student1 = new Student("001", "张三", "计算机科学与技术");
Student student2 = new Student("002", "李四", "软件工程");
students.add(student1);
students.add(student2);
while (true) {
System.out.println("请选择操作:1.添加课程 2.删除课程 3.修改课程 4.查询课程 5.添加学生 6.删除学生 7.修改学生 8.查询学生 9.选课 10.退选 11.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入课程编号:");
String courseId = scanner.next();
System.out.print("请输入课程名称:");
String courseName = scanner.next();
System.out.print("请输入课程学分:");
int credit = scanner.nextInt();
Course course = new Course(courseId, courseName, credit, maxNum);
courses.add(course);
break;
case 2:
System.out.print("请输入要删除的课程编号:");
String courseIdToDelete = scanner.next();
Course courseToDelete = null;
for (Course c : courses) {
if (c.getCourseId().equals(courseIdToDelete)) {
courseToDelete = c;
break;
}
}
if (courseToDelete != null) {
courses.remove(courseToDelete);
System.out.println("删除成功!");
} else {
System.out.println("未找到要删除的课程!");
}
break;
case 3:
System.out.print("请输入要修改的课程编号:");
String courseIdToModify = scanner.next();
Course courseToModify = null;
for (Course c : courses) {
if (c.getCourseId().equals(courseIdToModify)) {
courseToModify = c;
break;
}
}
if (courseToModify != null) {
System.out.println("请输入修改后的课程名称(不修改请直接回车):");
String newCourseName = scanner.next();
if (!newCourseName.equals("")) {
courseToModify.setCourseName(newCourseName);
}
System.out.println("请输入修改后的课程学分(不修改请直接回车):");
String newCreditString = scanner.next();
if (!newCreditString.equals("")) {
int newCredit = Integer.parseInt(newCreditString);
courseToModify.setCredit(newCredit);
}
System.out.println("课程修改成功!");
} else {
System.out.println("未找到要修改的课程!");
}
break;
case 4:
System.out.println("课程列表:");
for (Course c : courses) {
System.out.println(c);
}
break;
case 5:
System.out.print("请输入学生学号:");
String studentId = scanner.next();
System.out.print("请输入学生姓名:");
String studentName = scanner.next();
System.out.print("请输入学生专业:");
String major = scanner.next();
Student student = new Student(studentId, studentName, major);
students.add(student);
break;
case 6:
System.out.print("请输入要删除的学生学号:");
String studentIdToDelete = scanner.next();
Student studentToDelete = null;
for (Student s : students) {
if (s.getStudentId().equals(studentIdToDelete)) {
studentToDelete = s;
break;
}
}
if (studentToDelete != null) {
students.remove(studentToDelete);
System.out.println("删除成功!");
} else {
System.out.println("未找到要删除的学生!");
}
break;
case 7:
System.out.print("请输入要修改的学生学号:");
String studentIdToModify = scanner.next();
Student studentToModify = null;
for (Student s : students) {
if (s.getStudentId().equals(studentIdToModify)) {
studentToModify = s;
break;
}
}
if (studentToModify != null) {
System.out.println("请输入修改后的学生姓名(不修改请直接回车):");
String newStudentName = scanner.next();
if (!newStudentName.equals("")) {
studentToModify.setStudentName(newStudentName);
}
System.out.println("请输入修改后的学生专业(不修改请直接回车):");
String newMajor = scanner.next();
if (!newMajor.equals("")) {
studentToModify.setMajor(newMajor);
}
System.out.println("学生修改成功!");
} else {
System.out.println("未找到要修改的学生!");
}
break;
case 8:
System.out.println("学生列表:");
for (Student s : students) {
System.out.println(s);
}
break;
case 9:
System.out.print("请输入要选课的学生学号:");
String studentIdToSelect = scanner.next();
Student studentToSelect = null;
for (Student s : students) {
if (s.getStudentId().equals(studentIdToSelect)) {
studentToSelect = s;
break;
}
}
if (studentToSelect == null) {
System.out.println("未找到要选课的学生!");
break;
}
System.out.println("可选课程列表:");
List<Course> availableCourses = new ArrayList<>();
for (Course c : courses) {
if (!c.isFull()) {
System.out.println(c);
availableCourses.add(c);
}
}
if (availableCourses.isEmpty()) {
System.out.println("当前没有可选课程!");
break;
}
System.out.print("请输入要选的课程编号:");
String courseIdToSelect = scanner.next();
Course courseToSelect = null;
for (Course c : availableCourses) {
if (c.getCourseId().equals(courseIdToSelect)) {
courseToSelect = c;
break;
}
}
if (courseToSelect == null) {
System.out.println("未找到要选的课程!");
break;
}
Selection selection = new Selection(courseToSelect, studentToSelect);
selections.add(selection);
courseToSelect.setCurrentNum(courseToSelect.getCurrentNum() + 1);
System.out.println("选课成功!");
break;
case 10:
System.out.print("请输入要退选的学生学号:");
String studentIdToDrop = scanner.next();
System.out.print("请输入要退选的课程编号:");
String courseIdToDrop = scanner.next();
Selection selectionToDrop = null;
for (Selection s : selections) {
if (s.getStudent().getStudentId().equals(studentIdToDrop) && s.getCourse().getCourseId().equals(courseIdToDrop)) {
selectionToDrop = s;
break;
}
}
if (selectionToDrop == null) {
System.out.println("未找到要退选的选课记录!");
break;
}
selections.remove(selectionToDrop);
selectionToDrop.getCourse().setCurrentNum(selectionToDrop.getCourse().getCurrentNum() - 1);
System.out.println("退选成功!");
break;
case 11:
System.out.println("谢谢使用!");
return;
default:
System.out.println("输入不合法,请重新输入!");
break;
}
}
}
}
```
以上是一个简单的学生选课系统的实现,可以完成添加、删除、修改、查询相关信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)