Java学生选课系统实现: 1. 系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。 2.基本功能需求: (1)使用命令行完成系统的功能设计; (2)启动程序,输出“学生选课管理——登录”。 (2)提示客户输入学号、密码。 3.其他功能需求: (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (7)学生选课成功后,选课人数相应发生变化;代码
时间: 2024-02-12 11:06:44 浏览: 85
JAVA学生选课系统
4星 · 用户满意度95%
以下是一个基本的Java学生选课系统的代码实现,包含了上述所述的所有信息和功能需求:
Course.java
```java
public class Course {
private int id;
private String name;
private int selectedNum;
private int maxNum;
private String teacher;
public Course(int id, String name, int maxNum, String teacher) {
this.id = id;
this.name = name;
this.maxNum = maxNum;
this.teacher = teacher;
this.selectedNum = 0;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getSelectedNum() {
return selectedNum;
}
public int getMaxNum() {
return maxNum;
}
public String getTeacher() {
return teacher;
}
public boolean isSelected() {
return selectedNum >= maxNum;
}
public void select() {
selectedNum++;
}
public void unselect() {
selectedNum--;
}
}
```
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<Course>();
courses.add(new Course(1, "Java Programming", 30, "Tom"));
courses.add(new Course(2, "Data Structures", 30, "Jerry"));
courses.add(new Course(3, "Algorithm Design", 30, "Lucy"));
courses.add(new Course(4, "Database Systems", 30, "Lily"));
courses.add(new Course(5, "Operating Systems", 30, "Bob"));
courses.add(new Course(6, "Computer Networks", 30, "Alice"));
courses.add(new Course(7, "Software Engineering", 30, "John"));
courses.add(new Course(8, "Web Development", 30, "Mary"));
courses.add(new Course(9, "Artificial Intelligence", 30, "David"));
courses.add(new Course(10, "Mobile App Development", 30, "Amy"));
}
public static CourseDB getInstance() {
if (instance == null) {
instance = new CourseDB();
}
return instance;
}
public List<Course> getCourses() {
return courses;
}
public Course getCourseById(int id) {
for (Course course : courses) {
if (course.getId() == id) {
return course;
}
}
return null;
}
}
```
Student.java
```java
public class Student {
private int id;
private String name;
private String gender;
private String grade;
private String password;
public Student(int 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 int getId() {
return id;
}
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 = null;
private List<Student> students;
private StudentDB() {
students = new ArrayList<Student>();
students.add(new Student(1, "Tom", "Male", "Sophomore", "123456"));
students.add(new Student(2, "Jerry", "Male", "Freshman", "123456"));
students.add(new Student(3, "Lucy", "Female", "Senior", "123456"));
students.add(new Student(4, "Lily", "Female", "Junior", "123456"));
students.add(new Student(5, "Bob", "Male", "Senior", "123456"));
}
public static StudentDB getInstance() {
if (instance == null) {
instance = new StudentDB();
}
return instance;
}
public List<Student> getStudents() {
return students;
}
public Student getStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
}
```
Login.java
```java
import java.util.Scanner;
public class Login {
public static Student login() {
Scanner scanner = new Scanner(System.in);
System.out.println("学生选课管理——登录");
System.out.print("请输入学号:");
int id = scanner.nextInt();
System.out.print("请输入密码:");
String password = scanner.next();
StudentDB studentDB = StudentDB.getInstance();
Student student = studentDB.getStudentById(id);
if (student == null || !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 {
private static void printCourses(List<Course> courses) {
System.out.println("所有课程列表:");
System.out.println("编号\t名称\t已选人数\t可选人数\t授课教师");
for (Course course : courses) {
System.out.printf("%d\t%s\t%d\t
阅读全文