JAVA学习笔记13:IO&类集
2016-08-28 04:26
330 查看
完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,可以实现对学生信息的学号查找、输出全部学生信息的功能。
import java.io.*; public class Student implements Serializable { private String name; private int age; public Student(String name,int age){ this.setName(name); this.setAge(age); } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public String toString(){ return "姓名:" + this.getName() + ";年龄:" + this.getAge(); } public boolean equals(Object obj){ if (this==obj) { return true; } if (!(obj instanceof Student)) { return false; } Student stu = (Student)obj; if (this.getName().equals(stu.getName())&&this.getAge()==stu.getAge()) { return true; }else{ return false; } } public int hashCode(){ return this.getName().hashCode()*this.getAge(); } };
import java.io.*; public class FileOperate { private File f; public FileOperate(String path){ this.f = new File(path); } public boolean store(Object obj) throws Exception{ ObjectOutputStream oos = null; boolean flag = false; try{ oos = new ObjectOutputStream(new FileOutputStream(this.f)); oos.writeObject(obj); flag = true; }catch(Exception e){ throw e; }finally{ if (oos!=null) { oos.close(); } } return flag; } public Object load() throws Exception{ ObjectInputStream ois = null; Object obj = null; try{ if (!this.f.exists()) { this.store(null); } ois = new ObjectInputStream(new FileInputStream(this.f)); obj = ois.readObject(); }catch(Exception e){ throw e; }finally{ if (ois!=null) { ois.close(); } } return obj; } };
import java.io.*; public class InputData { private static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); public static String getString(String info){ String temp = null; System.out.print(info); try{ temp = InputData.buf.readLine(); }catch(IOException e){ e.printStackTrace(); } return temp; } public static int getInt(String info,String err){ int temp = 0; boolean flag = true; String str = null; while (flag) { str = InputData.getString(info); if (str.matches("^\\d+$")) { temp = Integer.parseInt(str); flag = false; }else{ System.out.println(err); } } return temp; } public static float getFloat(String info,String err){ float temp = 0.0f; boolean flag = true; String str = null; while (flag) { str = InputData.getString(info); if (str.matches("^\\d+\\.\\d+$")||str.matches("^\\d+$")) { temp = Float.parseFloat(str); flag = false; }else{ System.out.println(err); } } return temp; } };
import java.util.*; public class StuOperate { private FileOperate fo; private Map<String,Student> allStudent; @SuppressWarnings("unchecked") public StuOperate(String path){ this.fo = new FileOperate(path); try{ this.allStudent = (Map<String,Student>)this.fo.load(); }catch(Exception e){ e.printStackTrace(); } if (this.allStudent==null) { this.allStudent = new HashMap<String,Student>(); } } public void add(){ String number = null; while (true) { number = InputData.getString("请输入学号<全为数字>:"); if (number.matches("^\\d+$")) { break; }else{ System.out.println("学号不正确,请重新输入!"); } } if (this.allStudent.containsKey(number)) { while (true) { String str = InputData.getString("系统已包含该学号的学生 ---> " + this.allStudent.get(number) + ",确定要覆盖?<Y/N>:"); if (str.equalsIgnoreCase("n")) { return; }else if(str.equalsIgnoreCase("y")){ break; }else{ System.out.println("输入不正确!"); } } } String name = InputData.getString("请输入姓名:"); int age = InputData.getInt("请输入年龄:","年龄必须为整数,请重新输入!"); this.allStudent.put(number,new Student(name,age)); try{ this.fo.store(this.allStudent); }catch(Exception e){ e.printStackTrace(); } System.out.println("添加成功!"); } public void remove(){ String number = InputData.getString("请输入要删除的学生的学号:"); if (this.allStudent.containsKey(number)) { while (true) { String str = InputData.getString("确定要删除该学生吗?<Y/N>:"); if (str.equalsIgnoreCase("n")) { return; }else if(str.equalsIgnoreCase("y")){ break; }else{ System.out.println("输入不正确!"); } } this.allStudent.remove(number); try{ this.fo.store(this.allStudent); }catch(Exception e){ e.printStackTrace(); } System.out.println("删除成功!"); }else{ System.out.println("系统不包含指定的学生信息!"); } } public void search(){ String number = InputData.getString("请输入要查询的学生的学号:"); if (this.allStudent.containsKey(number)) { System.out.println("学号:" + number + this.allStudent.get(number)); }else{ System.out.println("系统没有查询到指定学生的信息!"); } } public void update(){ String number = InputData.getString("请输入要更新信息的学生学号:"); if (this.allStudent.containsKey(number)) { String name = InputData.getString("请设置姓名<原姓名:" + this.allStudent.get(number).getName()+">:"); int age = InputData.getInt("请设置年龄<原年龄:" + this.allStudent.get(number).getAge()+">:","年龄必须为整数,请重新输入!"); this.allStudent.put(number,new Student(name,age)); try{ this.fo.store(this.allStudent); }catch(Exception e){ e.printStackTrace(); } System.out.println("更新成功!"); }else{ System.out.println("系统不包含指定的学生信息!"); } } public void clear(){ while (true) { String str = InputData.getString("确定要清空全部学生信息吗?<Y/N>:"); if (str.equalsIgnoreCase("n")) { return; }else if(str.equalsIgnoreCase("y")){ break; }else{ System.out.println("输入不正确!"); } } this.allStudent.clear(); try{ this.fo.store(this.allStudent); }catch(Exception e){ e.printStackTrace(); } System.out.println("清空成功!"); } public void findAll(){ if (!this.allStudent.isEmpty()) { this.printAll(); }else{ System.out.println("系统没有发现任何学生信息!"); } } private void printAll(){ Set<Map.Entry<String,Student>> allSet = this.allStudent.entrySet(); Iterator<Map.Entry<String,Student>> iter = allSet.iterator(); while (iter.hasNext()) { Map.Entry<String,Student> me = iter.next(); System.out.println("学号:" + me.getKey() + ";" + me.getValue()); } } };
public class StuMenu { private StuOperate so; public StuMenu(String path){ this.so = new StuOperate(path); while (true) { this.show(); } } public void show(){ 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("【0】退出系统\n"); int i = InputData.getInt("请选择:","请输入正确的选项!"); switch (i){ case 1:{ this.so.add(); break; } case 2:{ this.so.remove(); break; } case 3:{ this.so.update(); break; } case 4:{ this.so.search(); break; } case 5:{ this.so.findAll(); break; } case 6:{ this.so.clear(); break; } case 0:{ System.exit(1); break; } default:{ System.out.println("请选择正确的操作!"); break; } } } };
import java.io.*; public class StuManage { public static void main(String[] args){ new StuMenu("d:" + File.separator + "student.dat"); } };
相关文章推荐
- java IO总结
- JavaSE入门:this 关键字
- The first day learning JAVA
- The 2nd Day Learning JAVA: enable JAVA demo run in Chrome
- The 3rd day learning Java
- The 4th day learning Java
- The 5th Day Learning Java
- 一个自动删除闲置过久文件的小程序
- Javase01Java入门
- Javase02标识符,关键字和基本数据类型
- Javase02project
- java IO学习笔记
- WriteOutputStream
- Mac OS X “to open Eclipse, you need a Java SE 6 runtime”问题解决
- JavaSE学习笔记(2016.3.30)
- JavaSE学习笔记(2016.3.31)
- JavaSE学习笔记(2016.4.1)
- JavaSE学习笔记(2016.4.3)
- JavaSE学习笔记(2016.4.6)
- JavaSE学习笔记(2016.4.12)