java代码:系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。 (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (7)学生选课成功后,选课人数相应发生变化;
时间: 2024-02-17 12:05:18 浏览: 115
学生信息管理系统, 实现学生信息包括:学号、姓名、性别、年龄、班级等信息
5星 · 资源好评率100%
以下是一个简单的 Java 代码示例,实现了上述要求:
CourseDB.java
```java
import java.util.ArrayList;
import java.util.List;
public class CourseDB {
private static CourseDB instance = null;
private List<Course> courses;
private CourseDB() {
courses = new ArrayList<>();
// 初始化课程信息
courses.add(new Course(1, "Java程序设计", 0, 50, "张三"));
courses.add(new Course(2, "数据结构", 0, 60, "李四"));
courses.add(new Course(3, "计算机网络", 0, 40, "王五"));
courses.add(new Course(4, "数据库原理", 0, 45, "赵六"));
courses.add(new Course(5, "操作系统", 0, 55, "钱七"));
courses.add(new Course(6, "软件工程", 0, 30, "刘八"));
courses.add(new Course(7, "人工智能", 0, 35, "孙九"));
courses.add(new Course(8, "计算机组成原理", 0, 40, "周十"));
courses.add(new Course(9, "编译原理", 0, 25, "吴十一"));
courses.add(new Course(10, "离散数学", 0, 30, "郑十二"));
}
public static CourseDB getInstance() {
if (instance == null) {
instance = new CourseDB();
}
return instance;
}
public List<Course> getCourses() {
return courses;
}
}
```
StudentDB.java
```java
import java.util.ArrayList;
import java.util.List;
public class StudentDB {
private static StudentDB instance = null;
private List<Student> students;
private StudentDB() {
students = new ArrayList<>();
// 初始化学生信息
students.add(new Student(1001, "张三", "男", 1, "123456"));
students.add(new Student(1002, "李四", "女", 2, "123456"));
students.add(new Student(1003, "王五", "男", 3, "123456"));
students.add(new Student(1004, "赵六", "女", 2, "123456"));
students.add(new Student(1005, "钱七", "男", 1, "123456"));
}
public static StudentDB getInstance() {
if (instance == null) {
instance = new StudentDB();
}
return instance;
}
public List<Student> getStudents() {
return students;
}
}
```
Login.java
```java
import java.util.List;
import java.util.Scanner;
public class Login {
public static Student doLogin() {
Scanner in = new Scanner(System.in);
StudentDB studentDB = StudentDB.getInstance();
List<Student> students = studentDB.getStudents();
while (true) {
System.out.print("请输入学号:");
int studentId = in.nextInt();
in.nextLine();
System.out.print("请输入密码:");
String password = in.nextLine();
for (Student student : students) {
if (student.getStudentId() == studentId && student.getPassword().equals(password)) {
System.out.println("登陆成功,欢迎 " + student.getName() + " 同学!");
return student;
}
}
System.out.println("学号或密码错误,请重新输入!");
}
}
}
```
SelectCourse.java
```java
import java.util.List;
import java.util.Scanner;
public class SelectCourse {
public static void doSelectCourse(Student student) {
Scanner in = new Scanner(System.in);
CourseDB courseDB = CourseDB.getInstance();
List<Course> courses = courseDB.getCourses();
System.out.println("可选课程列表:");
for (Course course : courses) {
if (course.getTotalNum() > course.getSelectedNum()) { // 可选
System.out.println(course.getCourseId() + "\t" + course.getCourseName() + "\t" + course.getTeacher());
}
}
while (true) {
System.out.print("请选择要选的课程编号(输入0退出):");
int courseId = in.nextInt();
in.nextLine();
if (courseId == 0) {
return;
}
for (Course course : courses) {
if (course.getCourseId() == courseId) {
// 检查是否已选
if (student.getCourseIds().contains(courseId)) {
System.out.println("该课程已经选过了!");
break;
}
// 检查是否可选
if (course.getTotalNum() <= course.getSelectedNum()) {
System.out.println("该课程已满!");
break;
}
// 选课成功
student.getCourseIds().add(courseId);
course.setSelectedNum(course.getSelectedNum() + 1);
System.out.println("选课成功!");
return;
}
}
System.out.println("课程编号不存在,请重新输入!");
}
}
}
```
MainProcess.java
```java
public class MainProcess {
public static void main(String[] args) {
Student student = Login.doLogin();
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 选课");
System.out.println("2. 查看已选课程");
System.out.println("0. 退出");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
switch (choice) {
case 1:
SelectCourse.doSelectCourse(student);
break;
case 2:
System.out.println("已选课程列表:");
for (int courseId : student.getCourseIds()) {
Course course = CourseDB.getInstance().getCourses().get(courseId - 1);
System.out.println(course.getCourseId() + "\t" + course.getCourseName() + "\t" + course.getTeacher());
}
break;
case 0:
System.out.println("退出系统!");
return;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
}
```
注意:为了简化代码,这里假设每个学生只能选一门课程,如果需要支持多门课程选修,请调整代码。
阅读全文