简单的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.mzph.cn/news/889830.shtml

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

相关文章

java全栈day16--Web后端实战(数据库)

一、数据库介绍 二、Mysql安装&#xff08;自行在网上找&#xff0c;教程简单&#xff09; 安装好了进行Mysql连接 连接语法&#xff1a;winr输入cmd&#xff0c;在命令行中再输入mysql -uroot -p密码 方法二&#xff1a;winr输入cmd&#xff0c;在命令行中再输入mysql -uroo…

CORDIC 算法实现 _FPGA

注&#xff1a;本文为 “CORDIC 算法” 相关文章合辑。 未整理去重。 如有内容异常&#xff0c;请看原文。 Cordic 算法的原理介绍 乐富道 2014-01-28 23:05 Cordic 算法知道正弦和余弦值&#xff0c;求反正切&#xff0c;即角度。 采用用不断的旋转求出对应的正弦余弦值&…

前端(vue组件)

1组件对象 1.1定义组件对象 defineComponent( {} ) 1.2注册组件 1.3使用组件 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-sca…

MySQL八股-MVCC入门

文章目录 当前读&#xff08;加锁&#xff09;快照读&#xff08;不加锁&#xff09;MVCC隐藏字段undo-log版本链A. 第一步B.第二步C. 第三步 readview MVCC原理分析RCA. 先来看第一次快照读具体的读取过程&#xff1a;B. 再来看第二次快照读具体的读取过程: RR隔离级别 当前读…

初始Python篇(6)—— 字符串

找往期文章包括但不限于本期文章中不懂的知识点&#xff1a; 个人主页&#xff1a;我要学编程(ಥ_ಥ)-CSDN博客 所属专栏&#xff1a; Python 目录 字符串的常见操作 格式化字符串 占位符 f-string 字符串的 format 方法 字符串的编码与解码 与数据验证相关的方法 …

从 CephFS 到 JuiceFS:同程旅游亿级文件存储平台构建之路

随着公司业务的快速发展&#xff0c;同程旅行的非结构化的数据突破 10 亿&#xff0c;在 2022 年&#xff0c;同程首先完成了对象存储服务的建设。当时&#xff0c;分布式文件系统方面&#xff0c;同程使用的是 CephFS&#xff0c;随着数据量的持续增长&#xff0c;CephFS 的高…

Jenkins参数化构建详解(This project is parameterized)

本文详细介绍了Jenkins中不同类型的参数化构建方法&#xff0c;包括字符串、选项、多行文本、布尔值和git分支参数的配置&#xff0c;以及如何使用ActiveChoiceParameter实现动态获取参数选项。通过示例展示了传统方法和声明式pipeline的语法 文章目录 1. Jenkins的参数化构建1…

【图像处理】利用numpy实现直方图均衡、自适应直方图均衡、对比度受限自适应直方图均衡

直方图均衡化是一种在图像处理技术&#xff0c;通过调整图像的直方图来增强图像的对比度。 本博客不利用opencv库&#xff0c;仅利用numpy、matplotlib来实现直方图均衡、自适应直方图均衡、对比度受限自适应直方图均衡 直方图均衡 包括四个流程 计算图像RGB三通道的归一化直…

组织空转数据(人类+小鼠)

空间转录组&#xff08;Spatial Transcriptomics&#xff09;是一种新兴的高通量基因组学技术&#xff0c;它允许我们在组织切片中同时获取基因表达信息和细胞的空间位置信息。其可以帮助我们更好地理解细胞在组织中的空间分布和相互作用&#xff0c;揭示组织发育、器官功能和疾…

[数据结构#1] 并查集 | FindRoot | Union | 优化 | 应用

目录 1. 并查集原理 问题背景 名称与编号映射 数据结构设计 2. 并查集基本操作 (1) 初始化 (2) 查询根节点 (FindRoot) (3) 合并集合 (Union) (4) 集合操作总结 并查集优化 (1) 路径压缩 (2) 按秩合并 3. 并查集的应用 (1) 统计省份数量 (2) 判断等式方程是否成…

JPA 基本查询(一)

JPA 查询简介示例 JPA教程 - JPA查询简介示例 最简单的JPQL查询选择单个实体类型的所有实例。 考虑下面的查询: SELECT e FROM Employee eJPQL尽可能使用SQL语法。 SQL查询从表中选择。JPQL从应用程序域模型的实体中选择。 语法 选择查询的整体形式如下: SELECT <sel…

【操作系统1】一篇文章便可入门操作系统

操作系统 (Operating System,OS)是一种系统软件&#xff0c;它负责管理计算机的硬件和软件资源。它的主要任务是组织和调度计算机的工作&#xff0c;并分配资源给用户和其他软件。操作系统为用户和软件提供了方便的接口和环境。它是计算机系统中最基本的软件之一。 一、操作系…

μC/OS-Ⅱ源码学习(6)---事件标志组

快速回顾 μC/OS-Ⅱ中的多任务 μC/OS-Ⅱ源码学习(1)---多任务系统的实现 μC/OS-Ⅱ源码学习(2)---多任务系统的实现(下) μC/OS-Ⅱ源码学习(3)---事件模型 μC/OS-Ⅱ源码学习(4)---信号量 μC/OS-Ⅱ源码学习(5)---消息队列 本文进一步解析事件模型中&#xff0c;事件标志…

【经验分享】OpenHarmony5.0.0-release编译RK3568不过问题(已解决)

问题描述 根据操作手册正常拉取代码&#xff0c;然后编译OpenHarmony5.0.0版本rk3568项目 编译命令 ./build.sh --product-name rk3568 --ccache出现如下报错 然后真正开始出错的位置是下面这句log FAILED: ../kernel/src_tmp/linux-5.10/boot_linux ../kernel/checkpoint/c…

C++重点和练习-----多态

rpg.cpp: #include <iostream>using namespace std;/*模拟一个游戏场景有一个英雄&#xff1a;初始所有属性为1atk,def,apd,hp游戏当中有以下3种武器长剑Sword&#xff1a; 装备该武器获得 1atx&#xff0c;1def短剑Blade&#xff1a; 装备该武器获得 1atk&#xff0c;1…

Qt之点击鼠标右键创建菜单栏使用(六)

Qt开发 系列文章 - menu&#xff08;六&#xff09; 目录 前言 一、示例演示 二、菜单栏 1.MenuBar 2.Menu 总结 前言 QMainWindow是一个为用户提供主窗口程序的类&#xff0c;包含一个菜单栏&#xff08;menubar&#xff09;、多个工具栏(toolbars)、一个状态栏(status…

天猫魔盒M17/M17S_超级UI 线刷固件包-可救砖(刷机取消双勾)

在智能电视盒子的领域中&#xff0c;天猫魔盒 M17 以其独特魅力占据一席之地&#xff0c;然而&#xff0c;原厂设置有时难以满足进阶用户的多元需求。此刻&#xff0c;刷机成为开启全新体验的关键钥匙&#xff0c;为您的盒子注入鲜活能量。 一、卓越固件特性概览 此款精心打造的…

Elasticsearch 7.x入门学习-Spring Data Elasticsearch框架

1 Spring Data框架 Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问&#xff0c;并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷&#xff0c;并支持 map-reduce 框架和云计算数据服务。 Spring Data 可以极大的简化 JPA的写法&#xff0c;…

【落羽的落羽 C语言篇】一些常见的字符函数、字符串函数、内存函数

文章目录 一、字符函数1. 字符分类函数2. 字符转换函数 二、字符串函数1. strlen的使用和模拟实现使用模拟实现 2. strcpy的使用和模拟实现使用模拟实现 3. strcat的使用和模拟实现使用模拟实现 4. strcmp的使用和模拟实现使用模拟实现 5. strncpy的使用6. strncat的使用7. str…

JAVA:访问者模式(Visitor Pattern)的技术指南

1、简述 访问者模式(Visitor Pattern)是一种行为型设计模式,允许你将操作分离到不同的对象中,而无需修改对象本身的结构。这种模式特别适合复杂对象结构中对其元素进行操作的场景。 本文将介绍访问者模式的核心概念、优缺点,并通过详细代码示例展示如何在实际应用中实现…