您的当前位置:首页正文

学生信息管理系统(运用集合、且存储本地磁盘)

2023-11-28 来源:客趣旅游网
学生信息管理系统

--基于io流,集合,面向对象知识

部分运行图:

完整代码如下,仅供学习参考:

package com.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;

public class IoObjOutputListStuMess { staticArrayList message = new ArrayList(); public static void main(String[] args) { initDate();//进入系统时读取之前所存储的数据到缓冲区 Scanner input = new Scanner(System.in); dis(); while(true){ String in = 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(); } } } public static void addStuMess(){ String flag =\"y\" ; do { booleanisInt=true,isYN = true; Scanner input = new Scanner(System.in); Student stu = new Student(null, null, 0, null);//随意设定初始值,不影响程序,只为了创建对象,类型对应即可 System.out.println(\"请输入学生的学号\"); String in = input.next(); stu.setNo(in); for(Student hadStu:message){ if(hadStu.getNo().equals(in)){

}

}

System.out.println(\"已存在该学生,不能重复添加\"); dis(); return;

System.out.println(\"请输入学生的姓名\"); stu.setName(input.next());

System.out.println(\"请输入学生的年龄\"); //对于年龄为整数的异常处理 while(isInt){ Scanner intInput = new Scanner(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 (Exception e) { 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\")); } public static void showStuMess(){ if(message.size()==0){ System.out.println(\"学生信息为空,请先添加学生信息\"); }else{ System.out.println(\"学号\姓名\年龄\性别\"); for(Student msg:message){ System.out.println(msg.getNo()+\"\\"+msg.getName()+\"\\"+msg.getAge()+\"\\"+msg.getGender()); } } dis();

}

public static void changStudentMess(){ if(message.size()==0){

}

System.out.println(\"学生信息库为空,您无法进行该项操作\"); dis(); return;

System.out.println(\"请输入您要修改的学生学号\"); Scanner input = new Scanner(System.in); String in = input.next();

Iterator it = message.iterator(); //构建迭代器 while(it.hasNext()){ Student stuMess = it.next(); if(stuMess.getNo().equals(in)){ aa:while(true){ booleanisInt = true; System.out.println(\"请选择你要修改的信息:\"); System.out.println(\"1、学号 2、姓名 3、年龄 4、性别 5、【保存】\"); String type = input.next();//修改内容选项 switch(type){ case \"1\": boolean flag =true; \");

System.out.println(\"学号修改为:\");

//修改学号时,原则是不能出现相同的学号 while(flag){ String isHad = input.next(); for(Student hadStu: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());

于150\");

System.out.println(\"姓名修改成功...\"); break; case \"3\":

System.out.println(\"年龄修改为:\"); //对于年龄为整数的异常处理 while(isInt){ Scanner intInput = new Scanner(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 (Exception e) { isInt = true;

System.out.println(\"年龄必须为整数类型,且大于0小

} } 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(); }

public static void delStudentMess(){ if(message.size()==0){ System.out.println(\"学生信息库为空,您无法进行该项操作\"); dis(); return; } System.out.println(\"请输入您要删除的学生学号\"); Scanner input = new Scanner(System.in); String in = input.next(); Iterator it = message.iterator(); //构建迭代器 while(it.hasNext()){ Student stuMess = it.next(); if(stuMess.getNo().equals(in)){ message.remove(stuMess); System.out.println(\"删除该学生信息成功...\"); dis(); return; } } System.out.println(\"对不起,没有找到该学生信息\"); dis(); }

public static void findStuMess(){ if(message.size()==0){ System.out.println(\"学生信息库为空,请先添加学生信息\"); dis(); return; } System.out.println(\"请输入您要查找的学生学号\"); Scanner input = new Scanner(System.in); String in = input.next(); Iterator it = message.iterator(); //构建迭代器

while(it.hasNext()){ Student stuMess = it.next(); if(stuMess.getNo().equals(in)){ System.out.println(\"该学生信息如下:\"); System.out.println(\"学号\姓名\年龄\性别\"); System.out.println(stuMess.getNo()+\"\\"+stuMess.getName()+\"\\"+stuMess.getAge()+\"\\"+stuMess.getGender()); dis(); return; } } System.out.println(\"删除失败,没有该学生信息...\"); dis(); } public static void savaDate(){ try ( ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream(\"IoObjOutputListStuMess.dat\"));//file 路径可根据实际需要来改 ) { oos.writeObject(message);//对象应用的序列化:直接将Student对象存入message集合中 } catch (FileNotFoundException e) { // e.printStackTrace(); dis(); } catch (IOException e) { e.printStackTrace(); } } public static void initDate(){ try ( ObjectInputStreamois = new ObjectInputStream(new FileInputStream(\"IoObjOutputListStuMess.dat\")); ){ try { message = (ArrayList)ois.readObject(); } catch (ClassNotFoundException e) {

// }

} catch (FileNotFoundException e) { e.printStackTrace();

System.out.println(\"-------首次加载,请添加学生信息!!!------\"); } catch (IOException e) { // e.printStackTrace(); }

}

static void dis(){

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|\"); System.out.println(\"*************************************\"); } }

class Student implements Serializable{//实现Serializable接口,该类就是可序列化的 private String no,name,gender; privateint age; public String getNo() { return no; } public void setNo(String no) { this.no = no;

生生息息生序)

} public String getName() { return name; } public void setName(String name) { this.name = name; } publicintgetAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Student(){}//无参构造器 public Student(String no, String name, int age, String gender) { // super(); this.no = no; this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return \"Student [no=\" + no + \+ \"]\"; } }

因篇幅问题不能全部显示,请点此查看更多更全内容