Java-学生选课系统

目的

实现学生登录-选课-课程添加等操作,以下代码分三部分来实现:学生系统部分,课程系统部分与主方法选课部分


学生系统部分

package CourseSelectionSystem;import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;public class studentSystem{Scanner in=new Scanner(System.in);private ArrayList<student> studentList;public boolean isEmpty(){return studentList.isEmpty();}public student getStudent(int index){if(index < 0 || index >= studentList.size()){System.out.println("下标错误,获取失败!");return null;}return studentList.get(index);}// 输出所有学生信息public void out_print(){System.out.println("\n---------输出所有学生信息-----------");studentList.forEach(s -> System.out.println(s.toString()));}public studentSystem(){studentList=new ArrayList<>();}// 查找学生public student lookUpStudent(){if(studentList.size() == 0) {System.out.println("暂无学生信息,无法进行查找操作");return null;}System.out.println("\n-------------查找学生------------");System.out.println("1、根据学生姓名进行查找 \t\t\t\t 2、根据学生学号进行查找");System.out.println("3、退出操作");System.out.print("请输入需要使用的功能序号:");String choose=in.next();// 查找成功返回该学生,查找失败返回nullif("2".equals(choose)){System.out.print("\n请输入需要查找的学生学号:");int ans = judgmentId(in.next());if(-1 == ans) {System.out.println("查找失败,该学生学号不存在!!!");return null;}System.out.println("\n查找成功!!!");System.out.println(studentList.get(ans).toString());return studentList.get(ans);}// 按姓名开始查找if("1".equals(choose)){System.out.print("请输入需要查找学生的姓名:");String targetName=in.next();List<student> ans = lookUpTargetName(targetName);if(0 == ans.size()){System.out.println("查找失败,无此姓名学生");return null;}System.out.println("查找成功!以下是姓名为"+targetName+"的学生信息");ans.forEach(s -> System.out.println(s.toString()));return null;}System.out.println("退出成功!");return null;}// 按姓名查找学生private List<student> lookUpTargetName(String targetName){List<student> res=new ArrayList<>();for(int i=0,end=studentList.size(); i<end; ++i){if(studentList.get(i).getName().equals(targetName)){res.add(studentList.get(i));}}return res;}// 修改public boolean amend(){if(studentList.size() == 0) {System.out.println("暂无学生信息,无法进行修改操作");return false;}System.out.println("\n--------修改学生信息--------------");System.out.print("请输入需要修改学生的学号:");String targetId=in.next();int ans = judgmentId(targetId);if(-1 == ans){System.out.println("该学号不存在,修改失败,请重新操作");return false;}student target = studentList.get(ans);System.out.println("学生信息为:"+target.toString());System.out.print("请输入需要修改后的学生姓名:");target.setName(in.next());System.out.print("请输入需要修改后的学生年龄:");target.setAge(in.nextInt());System.out.println("-----修改成功-------");System.out.println(target.toString());return true;}// 删除public boolean delect(){if(studentList.size() == 0) {System.out.println("暂无学生信息,无法进行删除操作");return false;}System.out.println("\n--------删除学生-------");System.out.print("请输入需要删除学生的学号:");String targetId=in.next();int ans = judgmentId(targetId);if(-1 == ans){System.out.println("该学号不存在,删除失败,请重新操作");return false;}System.out.println("删除成功!被删除学生的信息为:"+studentList.get(ans).toString());studentList.remove(ans);    //删除return true;}// 添加学生public boolean add(){System.out.println("\n-----------添加学生-----------");student newStudent=new student();System.out.print("请输入添加学生的id:");String id=in.next();int index = judgmentId(id);if(index != -1){System.out.print(studentList.get(index).toString()+"\t");System.out.println("该学号已被占用,请重新操作!");return false;}newStudent.setId(id);System.out.print("请输入姓名:");newStudent.setName(in.next());System.out.print("请输入年龄:");newStudent.setAge(in.nextInt());studentList.add(newStudent);System.out.println("添加成功");return true;}// 判断是否id存在public int judgmentId(String targetId){for(int i=0,end=studentList.size(); i< end; ++i){if(studentList.get(i).getId().equals(targetId)){return i;}}return -1;}
}class student {private String id;              //学生idprivate String name;            //姓名private int age;                //年龄private HashSet<courses> courseNumber; // 课程号//空参构造public student(){this.courseNumber=new HashSet<>();}public student(String id, String name, int age) {this.id = id;this.name = name;this.age = age;this.courseNumber=new HashSet<>();}/*** 获取* @return id*/public String getId() {return id;}/*** 设置* @param id*/public void setId(String id) {this.id = id;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}/*** 获取* @return courseNumber*/public HashSet<courses> getCourseNumber() {return courseNumber;}// 重载public boolean setCourseNumber(courses curCourses){return this.courseNumber.add(curCourses);}public String toString() {return "学号 = " + id + ", 姓名 = " + name + ", 年龄 = " + age;}public void out_Courses(){if(0 == courseNumber.size()){System.out.println("暂无已选课程");return;}System.out.println("学号:"+this.id+",姓名:"+this.name+" 已选的课程有:");this.courseNumber.forEach(s -> System.out.println(s));}// 需要构造一个输出学生选课信息的方法
}

课程管理部分

package CourseSelectionSystem;import java.util.HashMap;
import java.util.Scanner;public class coursesSystem{private static HashMap<String,courses> map; // 课程名为key,课程信息为valScanner in=new Scanner(System.in);public coursesSystem() {map=new HashMap<>();}public boolean isEmpty(){return map.isEmpty();}public courses getCourses(String target){return map.get(target);}public void out_data(){map.forEach((String e1,courses e2) -> System.out.println(e2));}//修改指定课程信息public boolean amendCoursesMessage(){System.out.print("请输入需要修改的课程名称:");String target=in.next();if(!map.containsKey(target)){System.out.println("修改失败,该课程不存在");return false;}courses res = map.get(target);System.out.print("请输入需要修改的课程号:");res.setId(in.nextInt());System.out.println("修改成功!!!");System.out.println("修改后的信息为:"+res.toString());return true;}// 删除public courses deleteCourse(){System.out.println("\n---------课程删除---------");System.out.print("请输入需要删除的课程名:");String target=in.next();courses res = map.remove(target);if(null == res){System.out.println("删除失败,该课程不存在");return null;}System.out.println("删除成功!!!");return res;}// 查找public boolean seekTargetCourse(){System.out.println("\n-----------课程查找------------");System.out.print("请输入需要查找的课程名:");String target=in.next();courses res = map.get(target);if(null == res){System.out.println("查找失败,该课程不存在");return false;}System.out.println("查找成功!!!");System.out.println(res.toString());return true;}// 添加public boolean add(){System.out.println("\n---------------课程添加-------------");System.out.print("请输入需要添加的课程名称:");String courseName=in.next();if(map.containsKey(courseName)){System.out.println("添加失败,该课程已存在");return false;}System.out.print("请输入需要添加的课程id:");int id=in.nextInt();map.put(courseName,new courses(id,courseName));System.out.println("添加成功!!!");return true;}}class courses {private int id; //课程序号private String CourseName;  // 课程名public courses() {}public courses(int id, String CourseName) {this.id = id;this.CourseName = CourseName;}/*** 获取* @return id*/public int getId() {return id;}/*** 设置* @param id*/public void setId(int id) {this.id = id;}/*** 获取* @return CourseName*/public String getCourseName() {return CourseName;}/*** 设置* @param CourseName*/public void setCourseName(String CourseName) {this.CourseName = CourseName;}public String toString() {return "课程号:" + id + ", 课程名:《" + CourseName + "》";}
}


主方法

package CourseSelectionSystem;// 学生选课系统import java.util.*;public class Main {static Scanner in=new Scanner(System.in);static studentSystem studentSystemList;   // 学生系统static coursesSystem coursesSystemList;   // 选课系统public static void main(String[] args) {studentSystemList=new studentSystem();coursesSystemList=new coursesSystem();HomePage(); // 主界面}public static void HomePage(){boolean flag=true;while(flag){System.out.println("\n--------------欢迎来到学生选课系统----------------");System.out.println("1、学生管理 \t\t\t\t 2、课程管理");System.out.println("3、学生登录 \t\t\t\t 4、退出");switch (in.next()){case "1" : studentInitialMenu(); break; //学生管理系统case "2" : coursesInitialMenu(); break; // 课程管理系统case "3" : studentLonIn(); break;default: flag=false; break;}System.out.println("\n\n\n\n\n");}System.out.println("退出成功!!!");}// 学生登录界面public static void studentLonIn(){if(studentSystemList.isEmpty()){System.out.println("暂无学生信息,请添加后进行登录");return;}System.out.println("\n----------学生登录-----------");System.out.print("请输入学号:");String id=in.next();student targetStudent = studentSystemList.getStudent(studentSystemList.judgmentId(id));if(null == targetStudent){System.out.println("查无此学号,登录失败!请确认再进行操作");return;}boolean flag=true;while(flag){System.out.println("\n-----------登录成功------------");System.out.println("1、输出已选课程 \t\t\t\t 2、选课");System.out.println("其他输入:退出");switch (in.next()){case "1" : targetStudent.out_Courses(); break;case "2" : studnetCourseSelection(targetStudent); break;default: flag=false; break;}}System.out.println("退出成功!");return;}// 学生选课private static void studnetCourseSelection(student targetStudent){if(coursesSystemList.isEmpty()){System.out.println("暂无课程信息,请等候公布");return;}do{System.out.println("课程列表:");coursesSystemList.out_data();   // 输出课程界面System.out.print("请输入需要选的课程名称:");courses course = coursesSystemList.getCourses(in.next());if(null == course){System.out.println("无此课程信息,请重新输入");continue;}System.out.println(course.toString());if(!targetStudent.setCourseNumber(course)){System.out.println("添加失败!你已选过该课程。");}else{System.out.println("添加成功");}System.out.println("是否继续选课?(yes/no)");}while(!"yes".equals(in.next()));System.out.println("------------选课结束-------------");}// 课程系统界面public static void coursesInitialMenu(){boolean flag=true;while(flag){System.out.println("\n-------------欢迎来到课程管理系统----------------");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("请输入您的选择:");switch (in.next()){case "1" : coursesSystemList.add(); break;case "2" : coursesSystemList.deleteCourse(); break;case "3" : coursesSystemList.amendCoursesMessage(); break;case "4" : coursesSystemList.seekTargetCourse(); break;case "5" : coursesSystemList.out_data(); break;case "6" : flag=false; break;default:System.out.println("输入错误,请重新输入");break;}}System.out.println("退出成功!!!");}// 学生管理管理界面public static void studentInitialMenu(){boolean flag=true;while(flag){System.out.println("\n-------------欢迎来到学生管理系统----------------");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("请输入您的选择:");switch (in.next()){case "1" : studentSystemList.add(); break;case "2" : studentSystemList.delect(); break;case "3" : studentSystemList.amend(); break;case "4" : studentSystemList.lookUpStudent(); break;case "5" : studentSystemList.out_print(); break;case "6" : flag=false; break;default:System.out.println("输入错误,请重新输入");break;}}System.out.println("退出成功!!!");}}


end

以上代码分别使用了三个类来实现要求,最后启动的入口在主方法部分。实现的比较粗糙,如有错误,欢迎指正。

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Python读取Excel每一行为列表—大PK(openpyxl\pandas\xlwings\xlrd)看谁用时少?

目录 背景使用—openpyxl&#xff08;耗时89秒输出&#xff09;使用—pandas&#xff08;耗时44秒输出&#xff09;使用—xlwings&#xff08;耗时15秒输出&#xff09;使用—xlrd&#xff08;耗时47秒输出&#xff09;总结 背景 我们在平常办公的时候&#xff0c;尤其是财务人…

asisctf 2023 web hello wp

hello 开题&#xff0c;直接给了源码。 <?php /* Read /next.txt Hint for beginners: read curls manpage. */ highlight_file(__FILE__); $url file:///hi.txt; if(array_key_exists(x, $_GET) &&!str_contains(strtolower($_GET[x]),file) && !str_c…

ElementUI之动态树+数据表格+分页

目录 前言 一.ElementUI之动态树 1.前端模板演示 2.数据绑定 2.1 通过链接获取后台数据 2.2 对链接进行绑定 2.3添加动态路由 2.4 配置路由 3.效果演示 二.数据表格动态分页 1.前端模板 2.通过JS交互获取后端数据 3 效果演示 前言 Element UI 是一个基于 Vue.js 的开…

64位Ubuntu20.04.5 LTS系统安装32位运行库

背景&#xff1a; 在ubutu&#xff08;版本为20.04.5 LTS&#xff09;中运行./arm-none-linux-gnueabi-gcc -v 后提示“no such device”。 经多方查证&#xff0c;是ubutu的版本是64位的&#xff0c;而需要运行的编译工具链是32位的&#xff0c;因此会不兼容。 解决方法就是在…

反射学习笔记

反射学习笔记 一、反射入门案例 在反射中&#xff0c;万物皆对象&#xff0c;方法也是对象。反射可以在不修改源码的情况下&#xff0c;只需修改配置文件&#xff0c;就能实现功能的改变。 实体类 /*** 动物猫类*/ public class Cat {private String name;public void hi()…

系统安全测试要怎么做?

进行系统安全测试时&#xff0c;可以按照以下详细的步骤进行&#xff1a; 1、信息收集和分析&#xff1a; 收集系统的相关信息&#xff0c;包括架构、部署环境、使用的框架和技术等。 分析系统的安全需求、威胁模型和安全策略等文档。 2、威胁建模和风险评估&#xff1a; …

gitee-快速设置

快速设置— 如果你知道该怎么操作&#xff0c;直接使用下面的地址 HTTPS SSH: gitgitee.com:liuzl33078235/esp-idf.git 我们强烈建议所有的git仓库都有一个README, LICENSE, .gitignore文件 初始化 readme 文件 Git入门&#xff1f;查看 帮助 , Visual Studio / TortoiseG…

el-table 指定层级展开

先来看看页面默认全部展开时页面的显示效果&#xff1a;所有节点被展开&#xff0c;一眼望去杂乱无章&#xff01; 那么如何实现只展开指定的节点呢&#xff1f;最终效果如下&#xff1a;一眼看去很舒爽。 干货上代码&#xff1a; <el-table border v-if"refreshTabl…

软件测试-测试用例

软件测试-测试用例 1.什么是测试用例 为了实施测试而向被测系统提供的一组集合。这组集合包括测试环境、操作步骤、测试数据、预期结果等要素。 举例&#xff1a;对一个垃圾桶设计测试用例 2.设计测试用例的万能公式 设计测试用例的万能公式&#xff1a;功能测试性能测试界…

卤制品配送经营商城小程序的用处是什么

卤制品也是食品领域重要的分支&#xff0c;尤其对年轻人来说&#xff0c;只要干净卫生好吃价格合理&#xff0c;那复购率宣传性自是不用说&#xff0c;而随着互联网发展&#xff0c;传统线下门店也须要通过线上破解难题或进一步扩大生意。 而商城小程序无疑是商家通过线上私域…

【漏洞复现】企望制造 ERP命令执行

漏洞描述 由于企望制造 ERP comboxstore.action接口权限设置不当&#xff0c;默认的配置可执行任意SQL语句&#xff0c;利用xp_cmdshell函数可远程执行命令&#xff0c;未经认证的攻击者可通过该漏洞获取服务器权限。 免责声明 技术文章仅供参考&#xff0c;任何个人和组织…

Maven项目package为jar包后在window运行报A JNI error has occurred

原因&#xff1a;本地java版本与项目结构中使用的java版本不一致&#xff08;之前因为别的需求把idea的java版本改为了18&#xff09; 解决方法 打开项目结构&#xff0c;将idea的java版本改为与本地一致 再修改项目中的pom.xml 重新编译&#xff0c;package即可

Spark集成ClickHouse(笔记)

目录 前言&#xff1a; 一.配置环境 1.安装clickhouse驱动 2.配置clickhouse环境 二.spark 集成clickhouse 直接上代码&#xff0c;里面有一些注释哦&#xff01; 前言&#xff1a; 在大数据处理和分析领域&#xff0c;Spark 是一个非常强大且广泛使用的开源分布式计算框架…

uni-app:实现页面效果1

效果 代码 <template><view><view class"add"><image :src"add_icon" mode""></image></view><view class"container_position"><view class"container_info"><view c…

【SQL server】数据库入门基本操作教学

个人主页&#xff1a;【&#x1f60a;个人主页】 系列专栏&#xff1a;【❤️初识JAVA】 前言 数据库是计算机系统中用于存储和管理数据的一种软件系统。它通常由一个或多个数据集合、管理系统和应用程序组成&#xff0c;被广泛应用于企业、政府和个人等各种领域。目前常用的数…

服务器搭建(TCP套接字)-epoll版(服务端)

epoll 是一种在 Linux 系统上用于高效事件驱动编程的 I/O 多路复用机制。它相比于传统的 select 和 poll 函数具有更好的性能和扩展性。 epoll 的主要特点和原理&#xff1a; 1、事件驱动&#xff1a;epoll 是基于事件驱动的模型&#xff0c;它通过监听事件来触发相应的回调函…

爬楼梯Java(斐波那契数列)

题目:有n阶楼梯,一次只能爬一层或者两层,请问有多少种方法? 这类题目其实都可以用斐波那契数列来解决,比如: 一阶楼梯只有一种方法 二阶楼梯有(11,2)两种方法 三阶楼梯有(111,12,21)三种方法 四阶楼梯有(1111,121,112,22,211)五种方式 五阶楼梯有(11111,1112,122,1211,1…

Servlet执行流程生命周期方法介绍体系结构、Request和Response的功能详解

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaEE 操作系统 Redis 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 Servlet 一、 Servlet执行流程二、Servlet生…

【ONE·Linux || 进程间通信】

总言 进程间通信&#xff1a;简述进程间通信&#xff0c;介绍一些通信方式&#xff0c;管道通信&#xff08;匿名、名命&#xff09;、共享内存等。 文章目录 总言1、进程间通信简述2、管道2.1、简介2.2、匿名管道2.2.1、匿名管道的原理2.2.2、编码理解&#xff1a;用fork来共…

Linux 系统移植(二)--系统调试

文章目录 一、 编译文件系统1.1 下载资源安装包1.2 配置模板ARM64目标平台1.3 配置交叉编译器1.4 配置登录用户名和密码1.5 配置Linux 控制台1.6 配置文件系统格式1.7 编译buildroot文件系统 二、编译ARM64 Linux三、启动 Qemu Linux系统参考链接&#xff1a; 一、 编译文件系统…