学生信息管理系统运用集合且存储本地磁盘_第1页
学生信息管理系统运用集合且存储本地磁盘_第2页
学生信息管理系统运用集合且存储本地磁盘_第3页
学生信息管理系统运用集合且存储本地磁盘_第4页
学生信息管理系统运用集合且存储本地磁盘_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

学生信息管理系统--基于io流,集合,面向对象知识部分运营图:完整代码如下,仅供学习参考:packagecom.gaobo.day13,homework;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjava.util.Scanner;publicclassIoObjOutputListStuMess{ staticArrayList<Student>message=newArrayList<Student>(); publicstaticvoidmain(String[]args){ initDate();//进入系统时读取之前所存储的数据到缓冲区 Scannerinput=newScanner(System.in); dis(); while(true){ Stringin=input.next(); switch(in){ case"1": addStuMess(); break; case"2": showStuMess(); break; case"3": changStudentMess(); break; case"4": delStudentMess(); break; case"5": findStuMess(); break; case"6": savaDate(); System.out.println("退出程序成功!"); System.exit(0); default: System.out.println("输入错误,请选择1~6!"); dis(); } } } publicstaticvoidaddStuMess(){ Stringflag="y"; do{ booleanisInt=true,isYN=true; Scannerinput=newScanner(System.in); Studentstu=newStudent(null,null,0,null);//随意设定初始值,不影响程序,只为了创建对象,类型相应即可 System.out.println("请输入学生的学号"); Stringin=input.next(); stu.setNo(in); for(StudenthadStu:message){ if(hadStu.getNo().equals(in)){ System.out.println("已存在该学生,不能反复添加"); dis(); return; } } System.out.println("请输入学生的姓名"); stu.setName(input.next()); System.out.println("请输入学生的年龄"); //对于年龄为整数的异常解决 while(isInt){ ScannerintInput=newScanner(System.in); try{ isInt=false; intintAge=intInput.nextInt(); if(intAge>150||intAge<=0){ System.out.println("年龄必须大于0小于150"); isInt=true; continue; } stu.setAge(intAge); }catch(Exceptione){ isInt=true; System.out.println("年龄必须为整数类型,且大于0小于150"); } } System.out.println("请输入学生的性别,男性输入boy,其余任意键为女性"); if(input.next().equals("boy")){ stu.setGender("男"); }else{ stu.setGender("女"); } message.add(stu);// System.out.println("message.size()"+message.size());//调试时使用 System.out.println("是否继续输入?y/n"); flag=input.next(); //对是否继续输入的选择做异常解决 while(isYN){ if(flag.equals("y")||flag.equals("n")){ isYN=false; } else{ System.out.println("请选择y或n"); flag=input.next(); } } if(flag.equals("n")){ dis(); break; } }while(flag.equals("y")); } publicstaticvoidshowStuMess(){ if(message.size()==0){ System.out.println("学生信息为空,请先添加学生信息"); }else{ System.out.println("学号\t姓名\t年龄\t性别"); for(Studentmsg:message){ System.out.println(msg.getNo()+"\t"+msg.getName()+"\t"+msg.getAge()+"\t"+msg.getGender()); } } dis(); } publicstaticvoidchangStudentMess(){ if(message.size()==0){ System.out.println("学生信息库为空,您无法进行该项操作"); dis(); return; } System.out.println("请输入您要修改的学生学号"); Scannerinput=newScanner(System.in); Stringin=input.next(); Iterator<Student>it=message.iterator();//构建迭代器 while(it.hasNext()){ StudentstuMess=it.next(); if(stuMess.getNo().equals(in)){ aa:while(true){ booleanisInt=true; System.out.println("请选择你要修改的信息:"); System.out.println("1、学号2、姓名3、年龄4、性别5、【保存】"); Stringtype=input.next();//修改内容选项 switch(type){ case"1": booleanflag=true; System.out.println("学号修改为:"); //修改学号时,原则是不能出现相同的学号 while(flag){ StringisHad=input.next(); for(StudenthadStu:message){ if(hadStu.getNo().equals(isHad)){ System.out.println("已存在该学生,不能反复添加"); flag=true; break; }else{ flag=false; } } if(flag==false){ stuMess.setNo(isHad); System.out.println("学号修改成功..."); } } break; case"2": System.out.println("姓名修改为:"); stuMess.setName(input.next()); System.out.println("姓名修改成功..."); break; case"3": System.out.println("年龄修改为:"); //对于年龄为整数的异常解决 while(isInt){ ScannerintInput=newScanner(System.in); try{ isInt=false; intintAge=intInput.nextInt(); if(intAge>150||intAge<=0){ System.out.println("年龄必须大于0小于150"); isInt=true; continue; } stuMess.setAge(intAge); System.out.println("年龄修改成功..."); }catch(Exceptione){ isInt=true; System.out.println("年龄必须为整数类型,且大于0小于150"); } } break; case"4": System.out.println("性别修改为:(男性输入boy,其余任意键为女性)"); if(input.next().equals("boy")){ stuMess.setGender("男"); }else{ stuMess.setGender("女"); } System.out.println("性别修改成功..."); break; case"5": System.out.println("学生信息更新已保存"); dis(); breakaa; default: System.out.println("输入错误,请重新输入!"); } } return; } } System.out.println("对不起,没有找到该学生信息"); dis(); } publicstaticvoiddelStudentMess(){ if(message.size()==0){ System.out.println("学生信息库为空,您无法进行该项操作"); dis(); return; } System.out.println("请输入您要删除的学生学号"); Scannerinput=newScanner(System.in); Stringin=input.next(); Iterator<Student>it=message.iterator();//构建迭代器 while(it.hasNext()){ StudentstuMess=it.next(); if(stuMess.getNo().equals(in)){ message.remove(stuMess); System.out.println("删除该学生信息成功..."); dis(); return; } } System.out.println("对不起,没有找到该学生信息"); dis(); } publicstaticvoidfindStuMess(){ if(message.size()==0){ System.out.println("学生信息库为空,请先添加学生信息"); dis(); return; } System.out.println("请输入您要查找的学生学号"); Scannerinput=newScanner(System.in); Stringin=input.next(); Iterator<Student>it=message.iterator();//构建迭代器 while(it.hasNext()){ StudentstuMess=it.next(); if(stuMess.getNo().equals(in)){ System.out.println("该学生信息如下:"); System.out.println("学号\t姓名\t年龄\t性别"); System.out.println(stuMess.getNo()+"\t"+stuMess.getName()+"\t"+stuMess.getAge()+"\t"+stuMess.getGender()); dis(); return; } } System.out.println("删除失败,没有该学生信息..."); dis(); } publicstaticvoidsavaDate(){ try( ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream("IoObjOutputListStuMess.dat"));//file途径可根据实际需要来改 ) { oos.writeObject(message);//对象应用的序列化:直接将Student对象存入message集合中 }catch(FileNotFoundExceptione){// e.printStackTrace(); dis(); }catch(IOExceptione){ e.printStackTrace(); } } publicstaticvoidinitDate(){ try( ObjectInputStreamois=newObjectInputStream(newFileInputStream("IoObjOutputListStuMess.dat")); ){ try{ message=(ArrayList<Student>)ois.readObject(); }catch(ClassNotFoundExceptione){ } }catch(FileNotFoundExceptione){// e.printStackTrace(); System.out.println("-------初次加载,请添加学生信息!!!------"); }catch(IOExceptione){// e.printStackTrace(); } } staticvoiddis(){ 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("|(请选择1~6)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论