java代码:系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师 (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (7)学生选课成功后,选课人数相应发生变化;
时间: 2024-02-24 13:57:34 浏览: 71
好的,这是一个选课系统的代码设计,下面是一些代码示例:
1. CourseDB.java
```
public class CourseDB {
private static CourseDB instance;
private List<Course> courseList;
private CourseDB() {
courseList = new ArrayList<>();
//初始化至少封装10门课程
courseList.add(new Course("001", "Java程序设计", 0, 50, "张三"));
courseList.add(new Course("002", "数据库原理", 0, 40, "李四"));
courseList.add(new Course("003", "网络编程", 0, 30, "王五"));
//...
}
public static synchronized CourseDB getInstance() {
if (instance == null) {
instance = new CourseDB();
}
return instance;
}
public List<Course> getCourseList() {
return courseList;
}
}
```
2. StudentDB.java
```
public class StudentDB {
private static StudentDB instance;
private List<Student> studentList;
private StudentDB() {
studentList = new ArrayList<>();
//初始化至少封装5名学生信息
studentList.add(new Student("001", "张三", "男", "大一", "123456"));
studentList.add(new Student("002", "李四", "女", "大二", "234567"));
//...
}
public static synchronized StudentDB getInstance() {
if (instance == null) {
instance = new StudentDB();
}
return instance;
}
public List<Student> getStudentList() {
return studentList;
}
}
```
3. Login.java
```
public class Login {
private StudentDB studentDB;
public Login(StudentDB studentDB) {
this.studentDB = studentDB;
}
public boolean login(String studentId, String password) {
List<Student> studentList = studentDB.getStudentList();
for (Student student : studentList) {
if (studentId.equals(student.getId()) && password.equals(student.getPassword())) {
return true;
}
}
return false;
}
}
```
4. SelectCourse.java
```
public class SelectCourse {
private CourseDB courseDB;
private StudentDB studentDB;
public SelectCourse(CourseDB courseDB, StudentDB studentDB) {
this.courseDB = courseDB;
this.studentDB = studentDB;
}
public boolean selectCourse(String studentId, String courseId) {
List<Student> studentList = studentDB.getStudentList();
List<Course> courseList = courseDB.getCourseList();
Student student = null;
Course course = null;
for (Student s : studentList) {
if (s.getId().equals(studentId)) {
student = s;
break;
}
}
if (student == null) {
return false;
}
for (Course c : courseList) {
if (c.getId().equals(courseId)) {
course = c;
break;
}
}
if (course == null) {
return false;
}
if (student.getSelectedCourseList().contains(course)) {
//已选课程,无法重复选择
return false;
}
if (course.getSelectedNum() >= course.getTotalNum()) {
//该课程已满
return false;
}
//选课成功
student.getSelectedCourseList().add(course);
course.setSelectedNum(course.getSelectedNum() + 1);
return true;
}
}
```
5. MainProcess.java
```
public class MainProcess {
private Login login;
private SelectCourse selectCourse;
public MainProcess(Login login, SelectCourse selectCourse) {
this.login = login;
this.selectCourse = selectCourse;
}
public void process() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String studentId = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
if (login.login(studentId, password)) {
System.out.println("登录成功");
System.out.println("请选择课程编号:");
String courseId = scanner.nextLine();
if (selectCourse.selectCourse(studentId, courseId)) {
System.out.println("选课成功");
} else {
System.out.println("选课失败");
}
} else {
System.out.println("登录失败");
}
}
}
```
这些代码实现了一个简单的选课系统,当然,还有很多细节和功能需要完善,但是这里提供的代码可以作为一个基础框架,供大家参考。
阅读全文