简单的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;}@Overridepublic 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("请选择操作:");}@Overridepublic 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>() {@Overridepublic 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);}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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<>();}@Overridepublic void displayMenu() {System.out.println("===== 学生菜单 =====");System.out.println("1. 修改登录密码");System.out.println("2. 查看自己所上课程");System.out.println("3. 选修课选课");System.out.println("0. 退出");System.out.print("请选择操作:");}@Overridepublic 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<>();}@Overridepublic void displayMenu() {System.out.println("===== 学生菜单 =====");System.out.println("1. 修改登录密码");System.out.println("2. 查看自己所上课程");System.out.println("3. 选修课选课");System.out.println("0. 退出");System.out.print("请选择操作:");}@Overridepublic 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 {
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/490893.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

环境和工程搭建

1.案例介绍 1.1 需求 实现⼀个电商平台 该如何实现呢? 如果把这些功能全部写在⼀个服务⾥, 这个服务将是巨⼤的. 巨多的会员, 巨⼤的流量, 微服务架构是最好的选择. 微服务应⽤开发的第⼀步, 就是服务拆分. 拆分后才能进⾏"各⾃开发" 1.2 服务拆分 拆分原则 …

港科夜闻 | 香港科大与荷兰代尔夫特理工大学(TU Delft)建立合作伙伴关系,推动艺术科技教育与研究...

关注并星标 每周阅读港科夜闻 建立新视野 开启新思维 1、香港科大与荷兰代尔夫特理工大学(TU Delft)建立合作伙伴关系&#xff0c;推动艺术科技教育与研究。2024年12月6日&#xff0c;合作伙伴计划正式启动&#xff0c;双方期望透过合作加强艺术科技知识交流&#xff0c;借此推…

电脑游戏运行时问题解析:《Geometry Dash》DLL文件损坏的原因与解决方案

电脑游戏运行时问题解析&#xff1a;《Geometry Dash》DLL文件损坏的原因与解决方案 在探索《Geometry Dash》这款节奏明快、充满挑战的几何世界冒险游戏时&#xff0c;我们或许会遇到一些令人头疼的技术问题&#xff0c;其中之一便是DLL文件损坏。DLL&#xff08;动态链接库&…

爬虫逆向学习(十四):分享一下某数通用破解服务开发经验

阅前须知 这篇博客不是教大家怎么实现的&#xff0c;而且告知大家有这个东西&#xff0c;或者说一种趋势&#xff0c;借此分享自己大致的实现经验。具体的实现我也不好整理&#xff0c;毕竟是在别人的基础上缝缝补补。 前言 使用补环境方式破解过某数的同学都知道&#xff0…

Maven 的下载

目录 1、Maven 官方地址2、下载3、解压4、配置本地仓库 1、Maven 官方地址 https://maven.apache.org/ 2、下载 3、解压 将下载的压缩包解压到任意位置 4、配置本地仓库 在 Maven 的安装目录下新建文件夹&#xff0c;用来当作 Maven 的本地仓库 进入 conf 目录下&#xff…

【HarmonyOS】鸿蒙应用实现手机摇一摇功能

【HarmonyOS】鸿蒙应用实现手机摇一摇功能 一、前言 手机摇一摇功能&#xff0c;是通过获取手机设备&#xff0c;加速度传感器接口&#xff0c;获取其中的数值&#xff0c;进行逻辑判断实现的功能。 在鸿蒙中手机设备传感器ohos.sensor (传感器)的系统API监听有以下&#xf…

《拉依达的嵌入式\驱动面试宝典》—C/CPP基础篇(三)

《拉依达的嵌入式\驱动面试宝典》—C/CPP基础篇(三) 你好,我是拉依达。 感谢所有阅读关注我的同学支持,目前博客累计阅读 27w,关注1.5w人。其中博客《最全Linux驱动开发全流程详细解析(持续更新)-CSDN博客》已经是 Linux驱动 相关内容搜索的推荐首位,感谢大家支持。 《拉…

统一身份安全管理体系的业务协同能力

随着集团企业数字化组织转型深化&#xff0c;各组织机构间业务协同程度提升。研发业务协同、数据驱动生产决策等数字化生产协作工作体系得以展开&#xff0c;企业内数据流转加快。企业对统一身份安全管理体系的业务协同管理和支撑能力要求提升&#xff1a; 统一身份管理流程需…

华为HarmonyOS NEXT 原生应用开发:鸿蒙中组件的组件状态管理、组件通信 组件状态管理小案例(好友录)!

文章目录 组件状态管理一、State装饰器1. State装饰器的特点2. State装饰器的使用 二、Prop装饰器&#xff08;父子单向通信&#xff09;1. Prop装饰器的特点2. Prop装饰器的使用示例 三、Link装饰器&#xff08;父子双向通信&#xff09;1. Link装饰器的特点3. Link使用示例 四…

CoolEdit详细使用和安装教程

Cool Edit Pro主要用于音频录制、编辑、混音和后期处理。Cool Edit Pro 的特点包括&#xff1a; 音频编辑&#xff1a;支持多轨编辑&#xff0c;可以同时处理多个音频文件&#xff0c;支持精确的音频剪切、复制、粘贴等操作。 录音功能&#xff1a;内置强大的录音功能&#xf…

如何量化管理研发团队的技术债务?

在探讨技术债的成因之前&#xff0c;我们需要澄清一些关于技术债起因和本质的普遍误解。 误解一&#xff1a;技术债务等同于劣质代码 那么&#xff0c;什么构成了所谓的「劣质代码」&#xff1f; 所谓的好代码&#xff0c;可能是指那些整洁、不会在未来限制你决策的代码&…

使用layui的table提示Could not parse as expression(踩坑记录)

踩坑记录 报错图如下 原因&#xff1a; 原来代码是下图这样 上下俩中括号都是连在一起的&#xff0c;可能导致解析问题 改成如下图这样 重新启动项目&#xff0c;运行正常&#xff01;

代理 IP 行业现状与未来趋势分析

随着互联网的飞速发展&#xff0c;代理 IP 行业在近年来逐渐兴起并成为网络技术领域中一个备受关注的细分行业。它在数据采集、网络营销、隐私保护等多个方面发挥着重要作用&#xff0c;其行业现状与未来发展趋势值得深入探讨。 目前&#xff0c;代理 IP 行业呈现出以下几个显著…

泷羽Sec学习笔记-zmap搭建炮台

zmap搭建炮台 zmap扫描环境&#xff1a;kali-linux 先更新软件库 sudo apt update 下载zmap sudo apt install zmap 开始扫描(需要root权限) sudo zmap -p 80 -o raw_ips.txt 代码解析&#xff1a; sudo&#xff1a;以超级用户&#xff08;管理员&#xff09;权限运行…

Introduction to NoSQL Systems

What is NoSQL NoSQL database are no-tabular非數據表格 database that store data differently than relational tables 其數據的存儲方式與關係型表格不同 Database that provide a mechanism機制 for data storage retrieval 檢索 that is modelled in means other than …

规则引擎(一)-技术要点

本文是规则引擎的第一篇&#xff0c;首先介绍规则引擎的技术要点&#xff0c;系列后续文章大纲 1. 事实 事实是规则的依据&#xff0c;来源于业务&#xff0c;或是业务实体&#xff0c;或是多个业务实体的汇集 2. 项目 描述规则的项目结构&#xff1b;KIE核心api&#xff1b;s…

redis集群 服务器更换ip,怎么办,怎么更换redis集群的ip

redis集群 服务器更换ip&#xff0c;怎么办&#xff0c;怎么更换redis集群的ip 1、安装redis三主三从集群2、正常状态的redis集群3、更改redis集群服务器的ip 重启服务器 集群会down4、更改redis集群服务器的ip 重启服务器 集群down的原因5、更改redis集群服务器的ip后&#xf…

详解负载均衡

什么是负载均衡&#xff1f; 想象一下&#xff0c;你有一家餐厅&#xff0c;当有很多客人同时到来时&#xff0c;如果只有一名服务员接待&#xff0c;可能会导致服务变慢。为了解决这个问题&#xff0c;你可以增加更多的服务员来分担工作&#xff0c;这样每位服务员就可以更快…

SpringCloud微服务实战系列:03spring-cloud-gateway业务网关灰度发布

目录 spring-cloud-gateway 和zuul spring webflux 和 spring mvc spring-cloud-gateway 的两种模式 spring-cloud-gateway server 模式下配置说明 grayLb://system-server 灰度发布代码实现 spring-cloud-gateway 和zuul zuul 是spring全家桶的第一代网关组件&#x…

win10配置免密ssh登录远程的ubuntu

为了在终端ssh远程和使用VScode远程我的VM上的ubuntu不需要设置密码&#xff0c;需要在win10配置免密ssh登录远程的ubuntu。 在win10打开cmd&#xff0c;执行下面的代码生成密钥对&#xff08;会提示进行设置&#xff0c;按照默认的配置就行&#xff0c;一直回车&#xff09;&…