简单的Java小项目

学生选课系统

在控制台输入输出信息:

在eclipse上面的超级简单文件结构:

Main.java

package experiment_4;

import java.util.*;
import java.io.*;

public class Main {
    public static List<Course> courseList = new ArrayList<>();
    public static List<Teacher> teacherList = new ArrayList<>();
    public static List<Student> studentList = new ArrayList<>();

    public static void main(String[] args) {
        loadData();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("===== 欢迎使用学生选课系统 =====");
            System.out.print("请输入用户名(输入exit退出):");
            String username = scanner.nextLine();
            if (username.equals("exit")) {
                saveData();
                break;
            }
            System.out.print("请输入密码:");
            String password = scanner.nextLine();
            User user = null;
            if (username.equals("admin")) {
                user = new Admin(username, courseList, teacherList, studentList);
            } else {
                user = findUserByUsername(username);
            }
            if (user == null) {
                System.out.println("用户不存在!");
                continue;
            }
            if (!user.verifyPassword(password)) {
                System.out.println("密码错误!");
                continue;
            }
            user.operate();
        }
        scanner.close();
    }

    private static User findUserByUsername(String username) {
        for (Teacher teacher : teacherList) {
            if (teacher.getUsername().equals(username)) {
                return teacher;
            }
        }
        for (Student student : studentList) {
            if (student.getUsername().equals(username)) {
                return student;
            }
        }
        return null;
    }

    private static void loadData() {
        try (ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("C:\\Users\\86183\\Desktop\\courses.txt"));
             ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("C:\\Users\\86183\\Desktop\\teachers.txt"));
             ObjectInputStream ois3 = new ObjectInputStream(new FileInputStream("C:\\Users\\86183\\Desktop\\students.txt"))) {
            courseList = (List<Course>) ois1.readObject();
            teacherList = (List<Teacher>) ois2.readObject();
            studentList = (List<Student>) ois3.readObject();
        } catch (Exception e) {
            System.out.println("初次运行,未找到数据文件。");
        }
    }

    private static void saveData() {
        try (ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("C:\\Users\\86183\\Desktop\\courses.txt"));
             ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("C:\\Users\\86183\\Desktop\\teachers.txt"));
             ObjectOutputStream oos3 = new ObjectOutputStream(new FileOutputStream("C:\\Users\\86183\\Desktop\\students.txt"))) {
            oos1.writeObject(courseList);
            oos2.writeObject(teacherList);
            oos3.writeObject(studentList);
            System.out.println("数据已保存。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Admin.java

package experiment_4;

import java.util.*;
import java.io.*;


public class Admin extends User {
    private List<Course> courseList;
    private List<Teacher> teacherList;
    private List<Student> studentList;

    public Admin(String username, List<Course> courseList, List<Teacher> teacherList, List<Student> studentList) {
        super(username);
        this.courseList = courseList;
        this.teacherList = teacherList;
        this.studentList = studentList;
    }

    @Override
    public void displayMenu() {
        System.out.println("===== 管理员菜单 =====");
        System.out.println("1. 添加课程");
        System.out.println("2. 删除课程");
        System.out.println("3. 按照选课人数排序");
        System.out.println("4. 显示课程清单");
        System.out.println("5. 修改授课教师");
        System.out.println("6. 显示学生列表");
        System.out.println("7. 显示教师列表");
        System.out.println("8. 恢复学生和教师初始密码");
        System.out.println("9. 添加老师和学生");
        System.out.println("10. 删除老师和学生");
        System.out.println("0. 退出");
        System.out.print("请选择操作:");
    }

    @Override
    public void operate() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            displayMenu();
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addCourse();
                    break;
                case 2:
                    deleteCourse();
                    break;
                case 3:
                    sortCoursesByStudentNumber();
                    break;
                case 4:
                    displayCourseList();
                    break;
                case 5:
                    modifyCourseTeacher();
                    break;
                case 6:
                    displayStudentList();
                    break;
                case 7:
                    displayTeacherList();
                    break;
                case 8:
                    resetPasswords();
                    break;
                case 9:
                    addUser();
                    break;
                case 10:
                    deleteUser();
                    break;
                case 0:
                    System.out.println("退出管理员系统。");
                    return;
                default:
                    System.out.println("无效的选择,请重新输入。");
            }
        }
    }

    private void addCourse() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入课程ID:");
        String courseID = scanner.nextLine();
        System.out.print("请输入课程名称:");
        String courseName = scanner.nextLine();
        System.out.print("请输入授课教师用户名:");
        String teacherName = scanner.nextLine();
        Teacher teacher = findTeacherByUsername(teacherName);
        if (teacher == null) {
            System.out.println("教师不存在!");
            return;
        }
        System.out.print("课程类型(1.必修 2.选修):");
        int type = scanner.nextInt();
        if (type == 1) {
            Course course = new CompulsoryCourse(courseID, courseName, teacher);
            courseList.add(course);
            // 所有学生自动加入必修课
            for (Student student : studentList) {
                student.getCoursesEnrolled().add(course);
                course.incrementStudentNumber();
            }
        } else if (type == 2) {
            System.out.print("请输入选修课最大人数:");
            int maxStudents = scanner.nextInt();
            Course course = new ElectiveCourse(courseID, courseName, teacher, maxStudents);
            courseList.add(course);
        } else {
            System.out.println("无效的课程类型!");
            return;
        }
        teacher.getCoursesTaught().add(courseList.get(courseList.size() - 1));
        System.out.println("课程添加成功!");
    }

    private void deleteCourse() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的课程ID:");
        String courseID = scanner.nextLine();
        Course course = findCourseByID(courseID);
        if (course != null) {
            courseList.remove(course);
            // 从教师和学生的课程列表中删除该课程
            course.getTeacher().getCoursesTaught().remove(course);
            for (Student student : studentList) {
                student.getCoursesEnrolled().remove(course);
            }
            System.out.println("课程删除成功!");
        } else {
            System.out.println("课程不存在!");
        }
    }

    private void sortCoursesByStudentNumber() {
        Collections.sort(courseList, new Comparator<Course>() {
            @Override
            public int compare(Course c1, Course c2) {
                return c2.getNumberOfStudents() - c1.getNumberOfStudents();
            }
        });
        System.out.println("课程已按照选课人数排序!");
    }

    private void displayCourseList() {
        System.out.println("===== 课程清单 =====");
        for (Course course : courseList) {
            System.out.println(course);
        }
    }

    private void modifyCourseTeacher() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的课程ID:");
        String courseID = scanner.nextLine();
        Course course = findCourseByID(courseID);
        if (course == null) {
            System.out.println("课程不存在!");
            return;
        }
        System.out.print("请输入新教师的用户名:");
        String teacherName = scanner.nextLine();
        Teacher newTeacher = findTeacherByUsername(teacherName);
        if (newTeacher == null) {
            System.out.println("教师不存在!");
            return;
        }
        // 更新教师信息
        course.getTeacher().getCoursesTaught().remove(course);
        course.setTeacher(newTeacher);
        newTeacher.getCoursesTaught().add(course);
        System.out.println("授课教师修改成功!");
    }

    private void displayStudentList() {
        System.out.println("===== 学生列表 =====");
        for (Student student : studentList) {
            System.out.println(student.getUsername());
        }
    }

    private void displayTeacherList() {
        System.out.println("===== 教师列表 =====");
        for (Teacher teacher : teacherList) {
            System.out.println(teacher.getUsername());
        }
    }

    private void resetPasswords() {
        for (Student student : studentList) {
            student.setPassword("123456");
        }
        for (Teacher teacher : teacherList) {
            teacher.setPassword("123456");
        }
        System.out.println("学生和教师密码已重置为初始密码!");
    }

    private void addUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("添加对象(1.教师 2.学生):");
        int type = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入用户名:");
        String username = scanner.nextLine();
        if (type == 1) {
            Teacher teacher = new Teacher(username);
            teacherList.add(teacher);
            System.out.println("教师添加成功!");
        } else if (type == 2) {
            Student student = new Student(username);
            // 将学生加入所有必修课
            for (Course course : courseList) {
                if (course instanceof CompulsoryCourse) {
                    student.getCoursesEnrolled().add(course);
                    course.incrementStudentNumber();
                }
            }
            studentList.add(student);
            System.out.println("学生添加成功!");
        } else {
            System.out.println("无效的类型!");
        }
    }

    private void deleteUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("删除对象(1.教师 2.学生):");
        int type = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入用户名:");
        String username = scanner.nextLine();
        if (type == 1) {
            Teacher teacher = findTeacherByUsername(username);
            if (teacher != null) {
                teacherList.remove(teacher);
                // 从课程中移除教师
                for (Course course : courseList) {
                    if (course.getTeacher().equals(teacher)) {
                        course.setTeacher(null);
                    }
                }
                System.out.println("教师删除成功!");
            } else {
                System.out.println("教师不存在!");
            }
        } else if (type == 2) {
            Student student = findStudentByUsername(username);
            if (student != null) {
                studentList.remove(student);
                // 从课程中移除学生
                for (Course course : student.getCoursesEnrolled()) {
                    course.decrementStudentNumber();
                }
                System.out.println("学生删除成功!");
            } else {
                System.out.println("学生不存在!");
            }
        } else {
            System.out.println("无效的类型!");
        }
    }

    private Teacher findTeacherByUsername(String username) {
        for (Teacher teacher : teacherList) {
            if (teacher.getUsername().equals(username)) {
                return teacher;
            }
        }
        return null;
    }

    private Student findStudentByUsername(String username) {
        for (Student student : studentList) {
            if (student.getUsername().equals(username)) {
                return student;
            }
        }
        return null;
    }

    private Course findCourseByID(String courseID) {
        for (Course course : courseList) {
            if (course.getCourseID().equals(courseID)) {
                return course;
            }
        }
        return null;
    }
}

CompulsoryCourse.java

package experiment_4;

public class CompulsoryCourse extends Course {
    public CompulsoryCourse(String courseID, String courseName, Teacher teacher) {
        super(courseID, courseName, teacher);
    }

    @Override
    public String toString() {
        return super.toString() + "(必修课)";
    }
}

Course.java

package experiment_4;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Course implements Serializable {
    protected String courseID;
    protected String courseName;
    protected Teacher teacher;
    protected int numberOfStudents;
    protected List<Student> enrolledStudents;

    public Course(String courseID, String courseName, Teacher teacher) {
        this.courseID = courseID;
        this.courseName = courseName;
        this.teacher = teacher;
        this.numberOfStudents = 0;
        this.enrolledStudents = new ArrayList<>();
    }

    public String getCourseID() {
        return courseID;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public void incrementStudentNumber() {
        this.numberOfStudents++;
    }

    public void decrementStudentNumber() {
        this.numberOfStudents--;
    }

    public List<Student> getEnrolledStudents() {
        return enrolledStudents;
    }

    @Override
    public String toString() {
        return "课程ID:" + courseID + ", 课程名称:" + courseName + ", 授课教师:" + teacher.getUsername() +
                ", 选课人数:" + numberOfStudents;
    }
}

ElectiveCourse.java

package experiment_4;

public class ElectiveCourse extends Course {
    private int maxStudents;

    public ElectiveCourse(String courseID, String courseName, Teacher teacher, int maxStudents) {
        super(courseID, courseName, teacher);
        this.maxStudents = maxStudents;
    }

    public int getMaxStudents() {
        return maxStudents;
    }

    @Override
    public String toString() {
        return super.toString() + ", 最大选课人数:" + maxStudents + "(选修课)";
    }
}

Student.java

package experiment_4;

import java.util.*;

public class Student extends User {
    private List<Course> coursesEnrolled;

    public Student(String username) {
        super(username);
        this.coursesEnrolled = new ArrayList<>();
    }

    @Override
    public void displayMenu() {
        System.out.println("===== 学生菜单 =====");
        System.out.println("1. 修改登录密码");
        System.out.println("2. 查看自己所上课程");
        System.out.println("3. 选修课选课");
        System.out.println("0. 退出");
        System.out.print("请选择操作:");
    }

    @Override
    public void operate() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            displayMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            switch (choice) {
                case 1:
                    changePassword(scanner); // 传递 Scanner 对象
                    break;
                case 2:
                    viewCoursesEnrolled();
                    break;
                case 3:
                    selectElectiveCourse();
                    break;
                case 0:
                    System.out.println("退出学生系统。");
                    return;
                default:
                    System.out.println("无效的选择,请重新输入。");
            }
        }
    }

    public void changePassword(Scanner scanner) { // 接受 Scanner 作为参数
        System.out.print("请输入新密码:");
        String newPassword = scanner.nextLine();
        super.changePassword(newPassword); // 调用父类的方法
    }

    public void viewCoursesEnrolled() {
        System.out.println("===== 已选课程列表 =====");
        for (Course course : coursesEnrolled) {
            System.out.println(course);
        }
    }

    public void selectElectiveCourse() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入选修课ID:");
        String courseID = scanner.nextLine();
        Course course = findCourseByID(courseID);
        if (course == null) {
            System.out.println("课程不存在!");
            return;
        }
        if (!(course instanceof ElectiveCourse)) {
            System.out.println("这不是一门选修课!");
            return;
        }
        ElectiveCourse electiveCourse = (ElectiveCourse) course;
        if (electiveCourse.getNumberOfStudents() >= electiveCourse.getMaxStudents()) {
            System.out.println("选课人数已满!");
            return;
        }
        if (coursesEnrolled.contains(course)) {
            System.out.println("您已选过此课程!");
            return;
        }
        coursesEnrolled.add(course);
        course.incrementStudentNumber();
        System.out.println("选课成功!");
    }

    private Course findCourseByID(String courseID) {
        for (Course course : Main.courseList) {
            if (course.getCourseID().equals(courseID)) {
                return course;
            }
        }
        return null;
    }

    public List<Course> getCoursesEnrolled() {
        return coursesEnrolled;
    }
}

Teacher.java

package experiment_4;

import java.util.*;

public class Student extends User {
    private List<Course> coursesEnrolled;

    public Student(String username) {
        super(username);
        this.coursesEnrolled = new ArrayList<>();
    }

    @Override
    public void displayMenu() {
        System.out.println("===== 学生菜单 =====");
        System.out.println("1. 修改登录密码");
        System.out.println("2. 查看自己所上课程");
        System.out.println("3. 选修课选课");
        System.out.println("0. 退出");
        System.out.print("请选择操作:");
    }

    @Override
    public void operate() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            displayMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            switch (choice) {
                case 1:
                    changePassword(scanner); // 传递 Scanner 对象
                    break;
                case 2:
                    viewCoursesEnrolled();
                    break;
                case 3:
                    selectElectiveCourse();
                    break;
                case 0:
                    System.out.println("退出学生系统。");
                    return;
                default:
                    System.out.println("无效的选择,请重新输入。");
            }
        }
    }

    public void changePassword(Scanner scanner) { // 接受 Scanner 作为参数
        System.out.print("请输入新密码:");
        String newPassword = scanner.nextLine();
        super.changePassword(newPassword); // 调用父类的方法
    }

    public void viewCoursesEnrolled() {
        System.out.println("===== 已选课程列表 =====");
        for (Course course : coursesEnrolled) {
            System.out.println(course);
        }
    }

    public void selectElectiveCourse() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入选修课ID:");
        String courseID = scanner.nextLine();
        Course course = findCourseByID(courseID);
        if (course == null) {
            System.out.println("课程不存在!");
            return;
        }
        if (!(course instanceof ElectiveCourse)) {
            System.out.println("这不是一门选修课!");
            return;
        }
        ElectiveCourse electiveCourse = (ElectiveCourse) course;
        if (electiveCourse.getNumberOfStudents() >= electiveCourse.getMaxStudents()) {
            System.out.println("选课人数已满!");
            return;
        }
        if (coursesEnrolled.contains(course)) {
            System.out.println("您已选过此课程!");
            return;
        }
        coursesEnrolled.add(course);
        course.incrementStudentNumber();
        System.out.println("选课成功!");
    }

    private Course findCourseByID(String courseID) {
        for (Course course : Main.courseList) {
            if (course.getCourseID().equals(courseID)) {
                return course;
            }
        }
        return null;
    }

    public List<Course> getCoursesEnrolled() {
        return coursesEnrolled;
    }
}

User.java

package experiment_4;

import java.io.Serializable;//允许类的对象可以被序列化

public abstract class User implements Serializable {
    protected String username;
    protected String password;//在定义该成员的类内部、同一包中的其他类、以及任何子类中(无论子类是否在同一个包中)都可以访问。

    public User(String username) {
        this.username = username;
        this.password = "123456"; // 初始密码
    }

    public String getUsername() {
        return username;
    }
    
    public void setPassword(String newPassword) {//设置密码
        this.password = newPassword;
    }


    public boolean verifyPassword(String password) {//验证密码
        return this.password.equals(password);
    }

    public void changePassword(String newPassword) {//修改密码
        this.password = newPassword;
        System.out.println("密码修改成功!");
    }
    
    

    public abstract void displayMenu();

    public abstract void operate();
}

无需用到模块化module-info.java

/**
 * 
 */
/**
 * 
 */
module experiment_4 {
}
相关推荐
我爱写代码?几秒前
Scala的隐式对象
开发语言·后端·scala
小参宿1 分钟前
【Stream流】
java·开发语言
ruleslol6 分钟前
java基础概念49-数据结构2
java·数据结构
qq_10613834578 分钟前
快速搭建SpringBoot3+Vue3+ElementPlus管理系统
java·vue.js·spring boot·idea
爱跨境的笑笑10 分钟前
代理IP地址和端口是什么?怎么进行设置?
开发语言·php
Koikoi12320 分钟前
java引用相关(四大引用类型,软引用避免oom,弱引用表,虚引用和引用队列,可达性分析算法)
java·开发语言
qystca21 分钟前
洛谷 P8824 [传智杯 #3 初赛] 终端 C语言
c语言·开发语言
橘颂TA22 分钟前
C语言:编译与链接
c语言·开发语言
GoodStudyAndDayDayUp27 分钟前
一个老是用的SQL
java·数据库·sql
奶香臭豆腐29 分钟前
C++ 泛编程 —— 函数模板(中)
开发语言·c++·学习