JAVA学生信息管理系统IO流版
JAVA学生信息管理系统IO流版
1. Student类
public class Student implements Serializable{//学号private int sid;//姓名private String name;//年龄private int age;//住址private String address;//无参构造器public Student() {super();// TODO Auto-generated constructor stub}//有参构造器public Student(int sid, String name, int age, String address) throws SidException,AgeException {super();setSid(sid);setName(name);setAge(age);setAddress(address);}public int getSid() {return sid;}public void setSid(int sid) throws SidException {if(sid<0) {throw new SidException("学号不能小于0");}this.sid = sid;}@Overridepublic String toString() {return "Student [sid=" + sid + ", name=" + name + ", age=" + age + ", address=" + address + "]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) throws AgeException {if(age<0 || age>100) {throw new AgeException("年龄必须在0-100之间!!!\n"); }this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
2.AgeException类
public class AgeException extends Exception{public AgeException() {}public AgeException(String str) {super(str);}}
3.SidException类
public class SidException extends Exception{public SidException() {}public SidException(String str) {super(str);}}
4.StudentManage
public class StudentManage {private static List<Student> list = new ArrayList<Student>();//类加载完毕时,就执行一次静态代码块,读取文件中保存的集合static {try {FileInputStream fis = new FileInputStream("D:\\java企业课\\学生信息管理系统IO流版\\a.txt");ObjectInputStream ois = new ObjectInputStream(fis);list = (List<Student>) ois.readObject();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {loop:for(;;) {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("请输入您的选择:");Scanner sc = new Scanner(System.in);int choice = sc.nextInt();switch (choice) {case 1:try {showStudent();} catch (Exception e) {System.out.println(e.getMessage());}break;case 2:try {addStudent();} catch (Exception e) {System.out.println(e.getMessage());} break;case 3:try {updateStudent();} catch (Exception e) {System.out.println(e.getMessage());}break;case 4:try {deleteStudent();} catch (Exception e) {System.out.println(e.getMessage());}break;case 5:exitStudent();break loop;default:System.out.println("输入非法,请重试");break;}}}//查询学生private static void showStudent() {System.out.println("学号\t姓名\t年龄\t地址");for(Student s : list) {System.out.println(s.getSid()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());}}//添加学生private static void addStudent() throws Exception {//创建文件对象File file = new File("D:\\java企业课\\学生信息管理系统IO流版\\a.txt");if(!file.isFile()) {file.createNewFile();}//提示用户输入信息Scanner sc = new Scanner(System.in);System.out.println("请输入学号:");int id = sc.nextInt();System.out.println("请输入姓名:");String name = sc.next();System.out.println("请输入年龄:");int age = sc.nextInt();System.out.println("请输入地址:");String address = sc.next();//将对象保存到集合中Student stu = new Student(id,name,age,address);list.add(stu);//再将集合保存到文件中FileOutputStream fos = new FileOutputStream(file);ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(list);oos.close();fos.close();System.out.println("添加成功√");}//修改学生private static void updateStudent() throws Exception {//提示用户输入学号修改,如果存在修改,如果不存在退出Scanner sc = new Scanner(System.in);System.out.println("请输入要修改的学生学号:");int id = sc.nextInt();int index = -1; //-1 表示该学生不存在for(int i=0;i<list.size();i++) {//如果当学生对象的学号 等于 输入的学号if(list.get(i).getSid() == id) {//说明找到了index = i;break;}}if(index == -1) {System.out.println("没有找到该学生信息,请重新输入");}else {System.out.println("请输入姓名:");String nameNew = sc.next();System.out.println("请输入年龄:");int ageNew = sc.nextInt();System.out.println("请输入地址:");String addressNew = sc.next();//给学生对象重新赋值Student s = list.get(index);s.setName(nameNew);s.setAge(ageNew);s.setAddress(addressNew);list.set(index, s);}//将集合重新保存到文件中FileOutputStream fos = new FileOutputStream("D:\\java企业课\\学生信息管理系统IO流版\\a.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(list);oos.flush();oos.close();fos.close();System.out.println("修改成功√");}//删除学生private static void deleteStudent() throws Exception {//提示用户输入学号删除,如果存在删除,如果不存在退出Scanner sc = new Scanner(System.in);System.out.println("请输入要删除的学生学号:");int id = sc.nextInt();int index = -1;for(int i = 0;i<list.size();i++) {if(list.get(i).getSid() == id) {index = i;break;}}if(index == -1) {System.out.println("没有找到该学生信息,请重新输入");}else {//删除该学生list.remove(index);//将该集合重新保存到文件中FileOutputStream fos = new FileOutputStream("D:\\java企业课\\学生信息管理系统IO流版\\a.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(list);oos.flush();oos.close();fos.close();System.out.println("删除成功√");}}//退出系统private static void exitStudent() {System.out.println("您已成功退出系统");}}
JAVA学生信息管理系统IO流版相关推荐
- JAVA 学生信息管理系统
java 学生信息管理系统 主要使用的增删改查操作 首先,需要先建一个文件(点击左上角File,选择第一个New里的第一个Java Project) 然后再建一个Javabean包,在这个包里新建一个 ...
- java学生信息管理系统(GUI+mysql数据库)
java学生信息管理系统+GUI界面布局+mysql数据库 代码已经更新!,重新设计了UI界面,代码之间的逻辑更加清晰 新的代码不需要手动建立数据库和表,全部由程序自动执行 用户名和密码为你数据库的用 ...
- JAVA学生信息管理系统课程设计简介以及开发源码介绍
java学生信息管理系统简介 1.系统简介 主要实现功能:登录,注册,增加,删除,更改,查找,条件查找,查看在线账号,更改密码. 涉及到内容:java mysql8.0 Navicat 可视化工具 2 ...
- java考勤管理系统源码java学生考勤请假系统java学生考勤管理系统 java考勤系统java学生信息管理系统java学生管理系统
java考勤管理系统源码java学生考勤请假系统java学生考勤管理系统 java考勤系统java学生信息管理系统java学生管理系统 public Swingtest002() {// 设置标题se ...
- 基于JAVA学生信息管理系统录像演示2021计算机毕业设计源码+系统+数据库+lw文档+部署
基于JAVA学生信息管理系统录像演示2021计算机毕业设计源码+系统+数据库+lw文档+部署 基于JAVA学生信息管理系统录像演示2021计算机毕业设计源码+系统+数据库+lw文档+部署 本源码技术栈 ...
- 学生信息管理系统(C++语言版)
学生信息管理系统(C语言版) #include "stdio.h" #include "bios.h" #include "conio.h" ...
- Java 学生信息管理系统 (mysql版)
引言:如果需要定制类似的图形界面版的Java 管理系统,比如控制台版的,Java web版的,ssm版,开发工具为idea和eclipse.myEclipse的,提供远程服务,需要源码,或者需要项目实 ...
- Java学生信息管理系统——管理员登录模块(简单易上手)
前言 这一篇是用来记录我编写学生信息管理系统时实现登录操作的过程,这是学生信息管理系统的第一个模块,之后我还会陆续将其它模块分享出来. 其它章节 ------------------------> ...
- 上海交通大学出版社python教材答案学生信息管理系统_学生信息管理系统(最终版)...
<学生信息管理系统.doc>由会员分享,可免费在线阅读全文,更多与<学生信息管理系统(最终版)>相关文档资源请在帮帮文库(www.woc88.com)数亿文档库存里搜索. 1. ...
最新文章
- 写给Java程序员的Java虚拟机学习指南
- 每日命令:(13)more
- android 电容屏(二):驱动调试之基本概念篇
- 【java】带时区的时间格式化
- Vue和iview-admin搭建的项目进行兼容
- mysql字段编码转换_mysql字符集编码转换
- 前端学习(1809):前端调试之微博头部开发
- C++基础09-货物售卖和MyArray实现
- android studio 拉取分支,AndroidStudio中使用Git-高级篇(二)——新建分支(branch)和拉取请求(Pull request)...
- 今日恐慌与贪婪指数为92 贪婪程度与昨日持平
- C++之string的底层真的是用char数组来实现的么?
- UI设计中金刚区图标设计总结
- 2012年5月16日,Google发布“知识图谱(Knowledge Graph)”
- Win10 原版系统安装
- 4.3.5 导频插入
- android开发软件!Android免打包多渠道统计如何实现?满满干货指导
- linux usb gadget 日志
- ​从微信后端仓库发展史谈谈单仓和多仓
- Python爬虫爬取动态网页
- Python爬虫是个啥?学了Python爬虫有什么用?
热门文章
- 往map里的vector添加_面试官:同步容器(如Vector)的所有操作一定是线程安全的吗?...
- 展示面-网络安全相关学习总结
- 不思议迷宫c语言基础,不思议迷宫——新手入门所学
- 读取文件卡顿_奥睿科IV300固态硬盘评测:35克,读取超900M/s?
- linux ioctl 设备只读,linux – 尝试SSH时设备的ioctl不合适
- springmvc流程_基于Spring MVC框架的Http流程分析
- 近一个月来的学习总结(今天的你比昨天的你进步了吗?)
- radiobutton 设置单选项目标
- 设计模式--6大原则--迪米特法则
- RecyclerView(六)设置下拉刷新