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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值