更新时间:2022-09-12 19:56:54
package org.redking.demo.op;
import org.redking.demo.util.FileOperate; import org.redking.demo.util.InputData; import org.redking.demo.vo.Person; public class PersonOperate { private InputData input = null; public PersonOperate(){ this.input = new InputData(); } //完成具体的Person对象操作 public void add(){ //要使用输入数据的类 String name = null; int age = 0; float score = 0.0f; System.out.print("输入姓名:"); name = this.input.getString(); System.out.print("输入年龄:"); age = this.input.getInt(); System.out.print("输入成绩:"); score = this.input.getFloat(); //生成Person对象,把对象保存在文件中 Person p = new Person(name,age,score); try{ new FileOperate().save(p); System.out.println("数据保存成功!"); }catch(Exception e){ System.out.println("数据保存失败!"); } } public void show(){ //从文件中把内容读进来 Person p = null; try{ p = (Person) new FileOperate().read(); }catch(Exception e){ System.out.println("内容显示失败,请确定数据是否存在!"); } if(p != null){ System.out.println(p); } } public void update(){ //先将之前的信息查出来 Person p = null; try{ p = (Person) new FileOperate().read(); }catch(Exception e){ System.out.println("内容显示失败,请确定数据是否存在!"); } if(p != null){ String name = null; int age = 0; float score =0.0f; System.out.print("请输入新的姓名(原姓名:"+p.getName()+")"); name = this.input.getString(); System.out.print("请输入新的年龄(原年龄:"+p.getAge()+")"); age = this.input.getInt(); System.out.print("请输入新的成绩(原成绩:"+p.getScore()+")"); score = this.input.getFloat(); //信息重新设置 p.setName(name); p.setAge(age); p.setScore(score); try{ new FileOperate().save(p); System.out.println("数据更新成功!"); }catch(Exception e){ System.out.println("数据更新失败!"); } } } } |
package org.redking.demo.util;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InputData { private BufferedReader buf =null; public InputData(){ buf = new BufferedReader(new InputStreamReader(System.in)); }; public String getString(){ String str = null; try { str = buf.readLine(); } catch (IOException e) {} return str; } public int getInt(){ int temp = 0; //如果输入的不是数字,至少应该有一个提示,告诉用户输入错了~ //可以使用正则验证 String str = null; boolean flag = true; while(flag){ //输入数据 str = this.getString(); if (!(str.matches("\\d+"))){ //如果输入的不是一个数字,则必须重新输入 System.out.print("输入的内容必须是整数,请重新输入:"); }else{ //输入的是一个正确的数字,则可以进行转换 temp = Integer.parseInt(str); //表示退出循环 flag = false; } } return temp; } public float getFloat(){ float f = 0.0f; //如果输入的不是数字,至少应该有一个提示,告诉用户输入错了~ //可以使用正则验证 String str = null; boolean flag = true; while(flag){ //输入数据 str = this.getString(); if (!(str.matches("\\d+.?\\d{1,2}"))){ //如果输入的不是一个数字,则必须重新输入 System.out.print("输入的内容必须是小数(小数点后两位),请重新输入:"); }else{ //输入的是一个正确的数字,则可以进行转换 f = Float.parseFloat(str); //表示退出循环 flag = false; } } return f; } } |
package org.redking.demo.util;
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class FileOperate { public static final String FILENAME = "E:\\person.ser"; //把对象保存在文件之中 public void save(Object obj){ ObjectOutputStream out = null; try { out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME))); //写入对象 out.writeObject(obj); }catch(Exception e){ try { throw e; } catch (Exception e1) {} }finally { try { out.close(); }catch(Exception e){} } } //把对象从文件之中读出来 public Object read() throws Exception{ Object obj = null; ObjectInputStream input =null; try { input = new ObjectInputStream(new FileInputStream(new File(FILENAME))); obj = input.readObject(); } catch (Exception e) { throw e; }finally{ try{ input.close(); }catch(Exception e){} } return obj; } } |
package org.redking.demo.menu;
import org.redking.demo.op.PersonOperate; import org.redking.demo.util.InputData; public class Menu { InputData input = null; public Menu(){ this.input = new InputData(); //循环出现菜单 while(true){ this.show(); } } //需要定义的菜单内容 public void show(){ System.out.println("\t\t\t1、增加人员信息"); System.out.println("\t\t\t2、浏览人员信息"); System.out.println("\t\t\t3、修改人员信息"); System.out.println("\t\t\t4、退出系统"); System.out.print("\n\n请选择要使用的操作:"); int temp = input.getInt(); switch(temp){ case 1:{ new PersonOperate().add(); break; } case 2:{ new PersonOperate().show(); break; } case 3:{ new PersonOperate().update(); break; } case 4:{ System.out.println("选择的是退出系统"); System.out.println("系统退出!"); System.exit(1); } default:{ System.out.println("输入的内容不正确"); break; } } } } |
package org.redking.demo.main;
import org.redking.demo.menu.Menu; public class Test { public static void main(String[] args) { new Menu(); } } |