java 学生选课系统实现: 1.系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。2.基本功能需求: (1)使用命令行完成系统的功能设计; (2)启动程序,输出"学生选课管理——登录"。(2)提示客户输入学号、密码。3.其他功能需求: (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件登录.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”;(7)学生选课成功后,选课人数相应发生变化;代码
时间: 2024-02-12 20:06:59 浏览: 90
以下是Java学生选课系统的代码实现,包含了上述基本功能需求和其他功能需求:
Course.java:
```java
public class Course {
private String courseId;
private String courseName;
private int selectedNum;
private int availableNum;
private String teacher;
public Course(String courseId, String courseName, int selectedNum, int availableNum, String teacher) {
this.courseId = courseId;
this.courseName = courseName;
this.selectedNum = selectedNum;
this.availableNum = availableNum;
this.teacher = teacher;
}
public String getCourseId() {
return courseId;
}
public String getCourseName() {
return courseName;
}
public int getSelectedNum() {
return selectedNum;
}
public int getAvailableNum() {
return availableNum;
}
public String getTeacher() {
return teacher;
}
public void setSelectedNum(int selectedNum) {
this.selectedNum = selectedNum;
}
public void setAvailableNum(int availableNum) {
this.availableNum = availableNum;
}
}
```
CourseDB.java:
```java
import java.util.ArrayList;
import java.util.List;
public class CourseDB {
private static CourseDB instance = new CourseDB();
private List<Course> courseList;
private CourseDB() {
courseList = new ArrayList<>();
courseList.add(new Course("001", "Java入门", 0, 2, "张三"));
courseList.add(new Course("002", "Java进阶", 0, 1, "李四"));
courseList.add(new Course("003", "Java高级", 0, 3, "王五"));
courseList.add(new Course("004", "Python入门", 0, 2, "赵六"));
courseList.add(new Course("005", "Python进阶", 0, 1, "钱七"));
courseList.add(new Course("006", "Python高级", 0, 3, "孙八"));
courseList.add(new Course("007", "C++入门", 0, 2, "周九"));
courseList.add(new Course("008", "C++进阶", 0, 1, "吴十"));
courseList.add(new Course("009", "C++高级", 0, 3, "郑一"));
courseList.add(new Course("010", "数据结构", 0, 2, "孔二"));
}
public static CourseDB getInstance() {
return instance;
}
public List<Course> getCourseList() {
return courseList;
}
public Course getCourseById(String courseId) {
for (Course course : courseList) {
if (course.getCourseId().equals(courseId)) {
return course;
}
}
return null;
}
}
```
Student.java:
```java
public class Student {
private String studentId;
private String name;
private String gender;
private String grade;
private String password;
public Student(String studentId, String name, String gender, String grade, String password) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getGrade() {
return grade;
}
public String getPassword() {
return password;
}
}
```
StudentDB.java:
```java
import java.util.ArrayList;
import java.util.List;
public class StudentDB {
private static StudentDB instance = new StudentDB();
private List<Student> studentList;
private StudentDB() {
studentList = new ArrayList<>();
studentList.add(new Student("1001", "张三", "男", "大一", "123456"));
studentList.add(new Student("1002", "李四", "女", "大二", "123456"));
studentList.add(new Student("1003", "王五", "男", "大三", "123456"));
studentList.add(new Student("1004", "赵六", "女", "大四", "123456"));
studentList.add(new Student("1005", "钱七", "男", "大一", "123456"));
}
public static StudentDB getInstance() {
return instance;
}
public List<Student> getStudentList() {
return studentList;
}
public Student getStudentById(String studentId) {
for (Student student : studentList) {
if (student.getStudentId().equals(studentId)) {
return student;
}
}
return null;
}
}
```
Login.java:
```java
import java.util.Scanner;
public class Login {
public static Student login() {
Scanner input = new Scanner(System.in);
System.out.println("学生选课管理——登录");
System.out.print("请输入学号:");
String studentId = input.nextLine();
System.out.print("请输入密码:");
String password = input.nextLine();
Student student = StudentDB.getInstance().getStudentById(studentId);
if (student == null) {
System.out.println("该学号不存在,请重新输入。");
return null;
}
if (!student.getPassword().equals(password)) {
System.out.println("密码错误,请重新输入。");
return null;
}
return student;
}
}
```
SelectCourse.java:
```java
import java.util.List;
import java.util.Scanner;
public class SelectCourse {
public static void select(Student student) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("请输入要选的课程编号(输入0退出):");
List<Course> courseList = CourseDB.getInstance().getCourseList();
for (Course course : courseList) {
System.out.printf("%s\t%s\t%d/%d\t%s\n", course.getCourseId(), course.getCourseName(),
course.getSelectedNum(), course.getAvailableNum(), course.getTeacher());
}
String courseId = input.nextLine();
if (courseId.equals("0")) {
break;
}
Course course = CourseDB.getInstance().getCourseById(courseId);
if (course == null) {
System.out.println("该课程不存在,请重新输入。");
continue;
}
if (course.getSelectedNum() >= course.getAvailableNum()) {
System.out.println("该课程已满,请选择其他课程。");
continue;
}
if (studentHasSelectedCourse(student, courseId)) {
System.out.println("您已经选过该课程,请选择其他课程。");
continue;
}
course.setSelectedNum(course.getSelectedNum() + 1);
studentSelectCourse(student, course);
System.out.println("选课成功!");
}
}
private static boolean studentHasSelectedCourse(Student student, String courseId) {
List<Course> selectedCourseList = student.getSelectedCourseList();
for (Course course : selectedCourseList) {
if (course.getCourseId().equals(courseId)) {
return true;
}
}
return false;
}
private static void studentSelectCourse(Student student, Course course) {
List<Course> selectedCourseList = student.getSelectedCourseList();
selectedCourseList.add(course);
student.setSelectedCourseList(selectedCourseList);
course.setSelectedNum(course.getSelectedNum() + 1);
course.setAvailableNum(course.getAvailableNum() - 1);
}
}
```
MainProcess.java:
```java
public class MainProcess {
public static void main(String[] args) {
Student student = null;
while (student == null) {
student = Login.login();
}
SelectCourse.select(student);
}
}
```
注意,以上代码只是一个基本的实现,实际情况可能需要更多的逻辑判断和错误处理。
阅读全文