java代码:学生选课系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。 (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)文件Course.java: 创建课程类; (7)文件Student.java:创建学生类; (8)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (9)学生选课成功后,选课人数相应发生变化;
时间: 2024-03-10 07:49:02 浏览: 57
java代码-使用java解决学生选课系统的源代码
好的,下面是一个简单的示例代码,实现了学生选课系统的基本功能:
Course.java:
```java
public class Course {
private String id;
private String name;
private int selectedNum;
private int totalNum;
private String teacher;
public Course(String id, String name, int totalNum, String teacher) {
this.id = id;
this.name = name;
this.totalNum = totalNum;
this.teacher = teacher;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getSelectedNum() {
return selectedNum;
}
public int getTotalNum() {
return totalNum;
}
public String getTeacher() {
return teacher;
}
public boolean select() {
if (selectedNum < totalNum) {
selectedNum++;
return true;
} else {
return false;
}
}
public boolean cancel() {
if (selectedNum > 0) {
selectedNum--;
return true;
} else {
return false;
}
}
public String getStatus() {
if (selectedNum == totalNum) {
return "已满";
} else {
return "可选";
}
}
}
```
Student.java:
```java
public class Student {
private String id;
private String name;
private String gender;
private String grade;
private String password;
public Student(String id, String name, String gender, String grade, String password) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getGrade() {
return grade;
}
public boolean checkPassword(String password) {
return this.password.equals(password);
}
}
```
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基础", 50, "张三"));
courseList.add(new Course("002", "Web开发", 50, "李四"));
courseList.add(new Course("003", "Python基础", 50, "王五"));
courseList.add(new Course("004", "算法设计与分析", 50, "赵六"));
courseList.add(new Course("005", "数据库系统原理", 50, "钱七"));
courseList.add(new Course("006", "计算机组成原理", 50, "孙八"));
courseList.add(new Course("007", "计算机网络", 50, "周九"));
courseList.add(new Course("008", "操作系统原理", 50, "吴十"));
courseList.add(new Course("009", "机器学习", 50, "张三"));
courseList.add(new Course("010", "深度学习", 50, "李四"));
}
public static CourseDB getInstance() {
return instance;
}
public List<Course> getCourseList() {
return courseList;
}
public Course getCourseById(String courseId) {
for (Course course : courseList) {
if (course.getId().equals(courseId)) {
return course;
}
}
return null;
}
}
```
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("001", "张三", "男", "大一", "123456"));
studentList.add(new Student("002", "李四", "女", "大一", "123456"));
studentList.add(new Student("003", "王五", "男", "大二", "123456"));
studentList.add(new Student("004", "赵六", "女", "大二", "123456"));
studentList.add(new Student("005", "钱七", "男", "大三", "123456"));
}
public static StudentDB getInstance() {
return instance;
}
public List<Student> getStudentList() {
return studentList;
}
public Student getStudentById(String studentId) {
for (Student student : studentList) {
if (student.getId().equals(studentId)) {
return student;
}
}
return null;
}
}
```
Login.java:
```java
import java.util.Scanner;
public class Login {
private static Scanner scanner = new Scanner(System.in);
public static Student login() {
System.out.println("请输入学号:");
String studentId = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
StudentDB studentDB = StudentDB.getInstance();
Student student = studentDB.getStudentById(studentId);
if (student != null && student.checkPassword(password)) {
System.out.println("欢迎您," + student.getName() + "!");
return student;
} else {
System.out.println("学号或密码错误!");
return null;
}
}
}
```
SelectCourse.java:
```java
import java.util.List;
import java.util.Scanner;
public class SelectCourse {
private static Scanner scanner = new Scanner(System.in);
public static void select(Student student) {
CourseDB courseDB = CourseDB.getInstance();
List<Course> courseList = courseDB.getCourseList();
System.out.println("可选课程列表:");
for (Course course : courseList) {
System.out.println(course.getId() + "\t" + course.getName() + "\t" + course.getTeacher() + "\t" + course.getStatus());
}
System.out.println("请输入课程编号:");
String courseId = scanner.nextLine();
Course course = courseDB.getCourseById(courseId);
if (course == null) {
System.out.println("课程编号不存在!");
} else {
if (course.select()) {
System.out.println("选课成功!");
studentSelected(student, course);
} else {
System.out.println("选课失败,该课程已满!");
}
}
}
public static void cancel(Student student) {
CourseDB courseDB = CourseDB.getInstance();
List<Course> courseList = courseDB.getCourseList();
System.out.println("已选课程列表:");
for (Course course : courseList) {
if (course.getSelectedNum() > 0) {
System.out.println(course.getId() + "\t" + course.getName() + "\t" + course.getTeacher());
}
}
System.out.println("请输入课程编号:");
String courseId = scanner.nextLine();
Course course = courseDB.getCourseById(courseId);
if (course == null) {
System.out.println("课程编号不存在!");
} else {
if (course.cancel()) {
System.out.println("退课成功!");
studentCanceled(student, course);
} else {
System.out.println("退课失败,您未选择该课程!");
}
}
}
private static void studentSelected(Student student, Course course) {
student.setSelected(course.getId());
System.out.println("选课状态已更新!");
}
private static void studentCanceled(Student student, Course course) {
student.setCanceled(course.getId());
System.out.println("退课状态已更新!");
}
}
```
MainProcess.java:
```java
public class MainProcess {
private static Student currentStudent = null;
public static void main(String[] args) {
while (true) {
System.out.println("1.登录 2.选课 3.退课 4.退出");
String choice = Login.scanner.nextLine();
switch (choice) {
case "1":
currentStudent = Login.login();
break;
case "2":
if (currentStudent == null) {
System.out.println("请先登录!");
} else {
SelectCourse.select(currentStudent);
}
break;
case "3":
if (currentStudent == null) {
System.out.println("请先登录!");
} else {
SelectCourse.cancel(currentStudent);
}
break;
case "4":
System.exit(0);
default:
System.out.println("无效的选项!");
}
}
}
}
```
注意:以上代码仅作为示例代码,可能存在不完善或错误之处,仅供学习参考。
阅读全文