实现的功能:添加、修改和浏览记录(包括姓名,年龄,职位,薪酬)。

      运行程序显示结果如下:

1、添加记录:

image

2、修改记录:

image

3、浏览记录:

image

4、退出系统:

image

各类之间调用关系如下图所示:

image

 

程序代码如下:

import com.dr.demo.menu.Menu;

public class Main {   
        public static void main(String[] args) {   
                new Menu();   
        }   
}

 

public class Menu {
    InputData input = null;
    public Menu(){
        this.input = new InputData();
        //循环出现菜单
        while(true){
            this.show();
        }
    }
    //要定义的菜单的内容
    public void show(){
        System.out.println("\t\t\t\t1~增加人员信息");
        System.out.println("\t\t\t\t2~修改人员信息");
        System.out.println("\t\t\t\t3~浏览人员信息");
        System.out.println("\t\t\t\t4~退出本系统!");
        System.out.println("\n\n请选择要使用的操作:(1.2.3.4)");
        int temp = input.getInt();
        switch(temp){
        case 1:{//增加人员信息
            new PersonOperate().add();//业务处理层
                break;
        }
        case 2:{//修改人员信息
            new PersonOperate().update();
                break;
        }
        case 3:{//浏览人员信息
            new PersonOperate().show();
                break;
        }
        case 4:{//退出系统
            System.out.println("选择的是退出本系统!");
            System.out.println("成功退出本系统!");
            System.exit(1);
        }
        default:{
            System.out.println("输入的内容不正确,请重新输入!");
            break;
        }
        }
    }
}

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputData {
    public 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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    public  int getInt(){
        int temp = 0;
        String str = null;
        boolean flag = true;
        while(flag){
            //输入数据
            str = this.getString();
            if(!(str.matches("\\d+"))){
                //输入的不是1-4的数,提示重新输入
                System.out.println("输入的数字有误,请重新输入!(必须是整数1-4)");
            }
            else {//数据输入正确,进行转换
                temp = Integer.parseInt(str);
                //退出循环
                flag = false;
            }
        }
        return temp;
    }
    public String getpsString(){
        String str = null;
        try {
            str = buf.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    public float getFloat(){
        float f = 0;
        String str = null;
        boolean flag = true;
        while(flag){
            str = this.getString();
            if(!(str.matches("\\d+?.\\d{1,2}"))){
                //如果输入的不是两位的小数则提示重新输入
                System.out.println("输入的数必须是float类型,请重新输入!");
            }
            else{
                f = Float.parseFloat(str);
                flag = false;//退出循环哈
            }
        }       
        return f;
    }

}

 

public class PersonOperate {
    private InputData input = null;
    public PersonOperate(){
        this.input = new InputData();
    }
    //增加记录
    public void add(){
        String name = null;
        int age = 0;
        String position = null;
        float salary = 0;
        System.out.println("请输入姓名:");
        name = this.input.getString();
        System.out.println("请输入年龄:");
        age = this.input.getInt();
        System.out.println("请输入职位:");
        position = this.input.getpsString();
        System.out.println("请输入薪酬:");
        salary = this.input.getFloat();
        //生成对象并保存在文件中
        Person p = new Person(name,age,position,salary);
        try{
            new FileOperate().save(p);//IO操作层
            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;
            String position = null;
            float salary = 0.0f;
            System.out.println("请输入新姓名:(原姓名:"+p.getName()+")");
            name = this.input.getString();
            System.out.println("请输入新年龄:(原年龄"+p.getAge()+")");
            age = this.input.getInt();
            System.out.println("请输入新职位:(原职位"+p.getPosition()+")");
            position = this.input.getpsString();
            System.out.println("请输入新工资:(原工资"+p.getSalary()+")");
            salary = this.input.getFloat();
            //信息重置
            p.setName(name);
            p.setAge(age);
            p.setPosition(position);
            p.setSalary(salary);
            try{
                new FileOperate().save(p);
                System.out.println("数据更新成功!");
            }catch(Exception e){
                System.out.println("数据更新失败!");   
            }
        }
    }
}

 

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:"+File.separator+"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.flush();
                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;
    }
}

 

import java.io.Serializable;

public class Person implements Serializable{
    public String toString(){
        return "姓名:"+this.name+"\t年龄:"+this.age+"   \t职位:"+this.position+"\t薪酬:"+this.salary;
    }
    private String name;
    private int age;
    private String position;
    private float salary;
    public Person(){}
    public Person(String name,int age,String position,float salary){
        this.name = name;
        this.age = age;
        this.position = position;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
}

 

     本程序优点在于各层之间耦合性不大,独立性比较强,重在理解Menu层、文件操作层、IO操作层三层之间的调用关系。存在的缺陷希望读者可以完善。