用Map写的员工信息的录入(逻辑业务分开了)
//package app1;import java.io.*;import java.util.HashMap;import java.util.Iterator;class EmployeeRepeat {private String strEmpID;// 员工编号private String chrGender;// 性别private String strTelNumber;// 电话号码private double dblSalary;// 工资//构造函数,初始化成员变量EmployeeRepeat(String strEmpID, String chrGender, String strTelNumber,double dblSalary) { this.strEmpID = strEmpID; this.chrGender = chrGender; this.strTelNumber = strTelNumber; this.dblSalary = dblSalary; }EmployeeRepeat(){}public String getStrEmpID() //员工编号{return strEmpID;//返回员工编号}//比较录入的数据是否已经出现public String checkAndPut(EmployeeRepeat er,HashMap<String,EmployeeRepeat> hm) {if (hm.containsKey(er.getStrEmpID())){return "员工编号重复,数据录入失败";}else{hm.put(er.getStrEmpID(), er);}return "数据录入成功";}public String toString()//重写toString()方法{return strEmpID + "\t\t\t" + chrGender + "\t\t\t" + strTelNumber + "\t\t\t" + dblSalary;}public void Info() throws IOException{HashMap<String, EmployeeRepeat> hm = new HashMap<String, EmployeeRepeat>();System.out.println("***************************录入员工信息******************************");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));boolean flag = true;// 标记是否继续录入的标志while (flag) {try {EmployeeRepeat er = new EmployeeRepeat();System.out.print("员工编号:");strEmpID = br.readLine();System.out.print("员工性别:");chrGender = br.readLine();System.out.print("员工电话:");strTelNumber = br.readLine();System.out.print("员工工资:");dblSalary = Double.parseDouble(br.readLine());System.out.println(checkAndPut(new EmployeeRepeat(strEmpID, chrGender, strTelNumber, dblSalary), hm));System.out.println("是否继续录入员工信息(Y 继续 其他键停止录入)");String ok = br.readLine();if (!(ok.equals("y") || ok.equals("Y"))) // 键盘控制事件{flag = false;System.out.println("****************员工基本信息表*******************");System.out.println("员工编号\t\t员工性别\t\t员工电话\t\t员工工资");for (Iterator<String> it = hm.keySet().iterator();it.hasNext();){System.out.println(hm.get(it.next()));}} } catch (Exception m)//捕获输入的类型错误的情况(员工工资必须是double类型的) {System.out.println("输入错误,请重新输入:"); } } }}public class EmployeeTestRepeat{ public static void main(String[] args) { EmployeeRepeat ee = new EmployeeRepeat(); try { ee.Info(); }catch (Exception e){ System.out.println(e.toString()); } }}?