利用java程序输出实现学生选课系统实现: 1. 系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。 2.基本功能需求: (1)使用命令行完成系统的功能设计; (2)启动程序,输出“学生选课管理——登录”。 (2)提示客户输入学号、密码。 (3)登录后显示操作菜单,效果如下图所示: (4)根据用户选择的功能,进行相关的展示: 查看全部课程: 选课功能: 查看已选课程: 3.其他功能需求: (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (7)学生选课成功后,选课人数相应发生变化;
时间: 2024-02-13 15:05:04 浏览: 135
好的,以下是一个简单的Java程序实现学生选课系统的示例代码:
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 String getPassword() {
return password;
}
}
```
Course.java:
```java
public class Course {
private String id;
private String name;
private int selectedNumber;
private int totalNumber;
private String teacher;
public Course(String id, String name, int selectedNumber, int totalNumber, String teacher) {
this.id = id;
this.name = name;
this.selectedNumber = selectedNumber;
this.totalNumber = totalNumber;
this.teacher = teacher;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getSelectedNumber() {
return selectedNumber;
}
public void setSelectedNumber(int selectedNumber) {
this.selectedNumber = selectedNumber;
}
public int getTotalNumber() {
return totalNumber;
}
public String getTeacher() {
return teacher;
}
}
```
CourseDB.java:
```java
import java.util.ArrayList;
import java.util.List;
public class CourseDB {
private static CourseDB instance;
private List<Course> courses;
private CourseDB() {
courses = new ArrayList<>();
courses.add(new Course("C001", "Java Programming", 0, 50, "Tom"));
courses.add(new Course("C002", "Data Structures and Algorithms", 0, 40, "John"));
courses.add(new Course("C003", "Database Systems", 0, 60, "Mary"));
courses.add(new Course("C004", "Computer Networks", 0, 30, "Bob"));
courses.add(new Course("C005", "Operating Systems", 0, 50, "Alice"));
courses.add(new Course("C006", "Web Development", 0, 40, "David"));
courses.add(new Course("C007", "Artificial Intelligence", 0, 30, "Peter"));
courses.add(new Course("C008", "Computer Graphics", 0, 50, "Maggie"));
courses.add(new Course("C009", "Mobile Application Development", 0, 40, "Jason"));
courses.add(new Course("C010", "Software Engineering", 0, 60, "Lucy"));
}
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;
private List<Student> students;
private StudentDB() {
students = new ArrayList<>();
students.add(new Student("S001", "Alice", "Female", "Sophomore", "123456"));
students.add(new Student("S002", "Bob", "Male", "Junior", "123456"));
students.add(new Student("S003", "Mary", "Female", "Senior", "123456"));
students.add(new Student("S004", "John", "Male", "Freshman", "123456"));
students.add(new Student("S005", "Lucy", "Female", "Sophomore", "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.Scanner;
public class Login {
private Student student;
public void login() {
Scanner scanner = new Scanner(System.in);
System.out.println("学生选课管理——登录");
System.out.print("请输入学号:");
String id = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
StudentDB studentDB = StudentDB.getInstance();
for (Student s : studentDB.getStudents()) {
if (s.getId().equals(id) && s.getPassword().equals(password)) {
student = s;
System.out.println("登录成功");
break;
}
}
if (student == null) {
System.out.println("学号或密码错误");
}
}
public Student getStudent() {
return student;
}
}
```
SelectCourse.java:
```java
import java.util.Scanner;
public class SelectCourse {
private Student student;
public SelectCourse(Student student) {
this.student = student;
}
public void select() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.查看全部课程 2.选课 3.查看已选课程 4.退出");
int choice = scanner.nextInt();
if (choice == 1) {
viewAllCourses();
} else if (choice == 2) {
selectCourse();
} else if (choice == 3) {
viewSelectedCourses();
} else if (choice == 4) {
break;
} else {
System.out.println("请选择正确的操作");
}
}
}
private void viewAllCourses() {
System.out.println("全部课程信息如下:");
CourseDB courseDB = CourseDB.getInstance();
for (Course c : courseDB.getCourses()) {
System.out.println("课程编号:" + c.getId());
System.out.println("课程名称:" + c.getName());
System.out.println("授课教师:" + c.getTeacher());
System.out.println("已选人数:" + c.getSelectedNumber());
System.out.println("可选人数:" + (c.getTotalNumber() - c.getSelectedNumber()));
System.out.println("--------------");
}
}
private void selectCourse() {
System.out.print("请输入要选的课程编号:");
Scanner scanner = new Scanner(System.in);
String id = scanner.nextLine();
CourseDB courseDB = CourseDB.getInstance();
Course selectedCourse = null;
for (Course c : courseDB.getCourses()) {
if (c.getId().equals(id)) {
selectedCourse = c;
break;
}
}
if (selectedCourse == null) {
System.out.println("没有该课程");
return;
}
if (selectedCourse.getSelectedNumber() >= selectedCourse.getTotalNumber()) {
System.out.println("该课程已选满");
return;
}
for (Course c : student.getCourses()) {
if (c != null && c.getId().equals(id)) {
System.out.println("该课程无法重复选择");
return;
}
}
selectedCourse.setSelectedNumber(selectedCourse.getSelectedNumber() + 1);
for (int i = 0; i < student.getCourses().length; i++) {
if (student.getCourses()[i] == null) {
student.getCourses()[i] = selectedCourse;
System.out.println("选课成功");
return;
}
}
System.out.println("您已选了最多的课程");
}
private void viewSelectedCourses() {
System.out.println("已选课程信息如下:");
for (Course c : student.getCourses()) {
if (c != null) {
System.out.println("课程编号:" + c.getId());
System.out.println("课程名称:" + c.getName());
System.out.println("授课教师:" + c.getTeacher());
System.out.println("已选人数:" + c.getSelectedNumber());
System.out.println("可选人数:" + (c.getTotalNumber() - c.getSelectedNumber()));
System.out.println("--------------");
}
}
}
}
```
MainProcess.java:
```java
public class MainProcess {
public static void main(String[] args) {
Login login = new Login();
login.login();
Student student = login.getStudent();
if (student == null) {
return;
}
SelectCourse selectCourse = new SelectCourse(student);
selectCourse.select();
}
}
```
这样就实现了一个简单的学生选课系统,其中包括登录、查看全部课程、选课、查看已选课程等功能,同时也考虑了学生选择“已选”课程、选课人数等重要细节。
阅读全文