实现java学生选课管理系统的源代码及解释说明
时间: 2023-12-18 10:04:30 浏览: 92
java代码-使用java解决学生选课系统的源代码
以下是一个简单的Java学生选课管理系统的源代码及解释说明:
1. 学生类(Student.java)
```java
public class Student {
private String name;
private int id;
private List<Course> courses; // 存储已选课程
public Student(String name, int id) {
this.name = name;
this.id = id;
this.courses = new ArrayList<>();
}
// 选课
public void selectCourse(Course course) {
if (!courses.contains(course)) {
courses.add(course);
course.addStudent(this);
}
}
// 退课
public void dropCourse(Course course) {
if (courses.contains(course)) {
courses.remove(course);
course.removeStudent(this);
}
}
// 查看已选课程
public List<Course> getCourses() {
return courses;
}
@Override
public String toString() {
return name + " (ID:" + id + ")";
}
}
```
该类表示一个学生,包含姓名、学号、已选课程等信息。其中,`selectCourse`方法用于选课,`dropCourse`方法用于退课,`getCourses`方法用于查看已选课程。
2. 课程类(Course.java)
```java
public class Course {
private String name;
private int id;
private int maxStudents; // 最大选课人数
private List<Student> students; // 存储已选学生
public Course(String name, int id, int maxStudents) {
this.name = name;
this.id = id;
this.maxStudents = maxStudents;
this.students = new ArrayList<>();
}
// 选课
public void addStudent(Student student) {
if (students.size() < maxStudents && !students.contains(student)) {
students.add(student);
student.selectCourse(this);
}
}
// 退课
public void removeStudent(Student student) {
if (students.contains(student)) {
students.remove(student);
student.dropCourse(this);
}
}
// 查看已选学生
public List<Student> getStudents() {
return students;
}
@Override
public String toString() {
return name + " (ID:" + id + ")";
}
}
```
该类表示一个课程,包含课程名称、课程编号、最大选课人数、已选学生等信息。其中,`addStudent`方法用于添加已选学生,`removeStudent`方法用于移除已选学生,`getStudents`方法用于查看已选学生。
3. 学生选课系统(Main.java)
```java
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
private static Student[] students = new Student[10];
private static Course[] courses = new Course[5];
public static void main(String[] args) {
initStudents();
initCourses();
showMenu();
}
// 初始化学生信息
private static void initStudents() {
students[0] = new Student("张三", 101);
students[1] = new Student("李四", 102);
students[2] = new Student("王五", 103);
students[3] = new Student("赵六", 104);
students[4] = new Student("钱七", 105);
}
// 初始化课程信息
private static void initCourses() {
courses[0] = new Course("Java程序设计", 201, 3);
courses[1] = new Course("Python程序设计", 202, 2);
courses[2] = new Course("Web开发技术", 203, 4);
courses[3] = new Course("数据库技术", 204, 3);
courses[4] = new Course("计算机网络", 205, 2);
}
// 显示菜单
private static void showMenu() {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 学生选课");
System.out.println("2. 学生退课");
System.out.println("3. 查看课程信息");
System.out.println("4. 查看学生信息");
System.out.println("5. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
selectCourse();
break;
case 2:
dropCourse();
break;
case 3:
showCourseInfo();
break;
case 4:
showStudentInfo();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("无效的操作,请重新选择!");
}
}
}
// 学生选课
private static void selectCourse() {
System.out.print("请输入学生ID:");
int studentId = scanner.nextInt();
System.out.print("请输入课程ID:");
int courseId = scanner.nextInt();
Student student = findStudent(studentId);
if (student == null) {
System.out.println("未找到该学生!");
return;
}
Course course = findCourse(courseId);
if (course == null) {
System.out.println("未找到该课程!");
return;
}
student.selectCourse(course);
System.out.println(student.getName() + "已选课程:" + course.getName());
}
// 学生退课
private static void dropCourse() {
System.out.print("请输入学生ID:");
int studentId = scanner.nextInt();
System.out.print("请输入课程ID:");
int courseId = scanner.nextInt();
Student student = findStudent(studentId);
if (student == null) {
System.out.println("未找到该学生!");
return;
}
Course course = findCourse(courseId);
if (course == null) {
System.out.println("未找到该课程!");
return;
}
student.dropCourse(course);
System.out.println(student.getName() + "已退课程:" + course.getName());
}
// 查看课程信息
private static void showCourseInfo() {
for (Course course : courses) {
System.out.println(course.getName() + " (ID:" + course.getId() +
", 选课人数:" + course.getStudents().size() +
"/" + course.getMaxStudents() + ")");
System.out.print("已选学生:");
for (Student student : course.getStudents()) {
System.out.print(student.getName() + " ");
}
System.out.println();
}
}
// 查看学生信息
private static void showStudentInfo() {
for (Student student : students) {
System.out.println(student.getName() + " (ID:" + student.getId() + ")");
System.out.print("已选课程:");
for (Course course : student.getCourses()) {
System.out.print(course.getName() + " ");
}
System.out.println();
}
}
// 根据ID查找学生
private static Student findStudent(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
// 根据ID查找课程
private static Course findCourse(int id) {
for (Course course : courses) {
if (course.getId() == id) {
return course;
}
}
return null;
}
}
```
该类实现了一个简单的学生选课系统,包含学生选课、学生退课、查看课程信息、查看学生信息等功能。其中,`initStudents`方法用于初始化学生信息,`initCourses`方法用于初始化课程信息,`showMenu`方法用于显示菜单,`selectCourse`方法用于学生选课,`dropCourse`方法用于学生退课,`showCourseInfo`方法用于查看课程信息,`showStudentInfo`方法用于查看学生信息,`findStudent`方法用于根据ID查找学生,`findCourse`方法用于根据ID查找课程。
以上就是一个简单的Java学生选课管理系统的源代码及解释说明。
阅读全文