北邮JAVA高级语言程序设计(选修课)第五次OJ作业
题目描述:
定义类factory,该类有私有属性employees,类型为HashMap。
完成下述功能:
1) 定义方法void initEmployees(List es),以socialSecurityNumber为key,将员工对象保存到HashMap中。其中Employee类定义见实验3。
2) 定义方法Employee getEmployee( String empSecNum),用于查找并返回社会保险号为empSecNum的员工,若未找到该员工,返回null, 然后输出”get: employee not found.”, 否则根据返回的员工对象输出员工信息,信息格式与实验3一致。
3) 定义方法Employee deleteEmployee(String empSecNum),用于从HashTable中删除社会保险号为empSecNum的员工,并返回该员工对象的引用。若未找到该员工,返回null, 然后输出”delete: employee not found.”,否则根据返回的员工对象输出员工信息。
4) 定义方法Employee addEmployee(Employee stu),用于添加参数定义的员工对象到HashMap中。如果此员工已经存在,则返回null,然后输出”add: employee exists.” 。否则返回该员工对象,并输出员工信息。
5) 定义方法Employee updateEmployee(String empSecNum ,Employee emp),用于更新员工集合中社会保险号为empSecNum的员工对象信息,更新后的信息为emp。如果不存在该员工,返回null,然后输出”update: employee not found.”。否则返回emp,并输出员工信息。
6) 定义方法void printEmployees(),用于输出每一个员工的信息。
样例格式说明:
每行表示一个操作。样例中首先以init操作读取多个员工信息。其中init后的数字表示员工类型,-1表示init结束,此时应该调用initEmployees方法将多个员工信息存入HashMap。0表示SalaridEmployee,1表示HourlyEmployee,2表示CommissionEmployee,3表示BasePlusCommisionEmployee。
get 对应getEmployee方法。
add 对应addEmployee方法。
update对应updateEmployee方法。
delete对应deleteEmployee方法
print 对应printEmployees方法。
输入样例:
init 0 Ai Meng 2012673901 4312
init 1 NanXiong Qimu 2016782340 15.2 200
init 2 Guo Yang 2017672347 46781.3 0.1
init 3 Rong Huang 2018768901 7854.4 0.28 7098
init -1
get 2018768902
get 2018768901
delete 2018768901
add 0 Ai Meng 2012673901 4312
add 0 Hai Kong 2015673901 4333
update 0 Hai Kong 2015673901 9999
delete 2017672347
delete 2015673909
print
输出样例:
get: employee not found.
firstName:Rong; lastName:Huang; socialSecurityNumber:2018768901; earning:9297.23
firstName:Rong; lastName:Huang; socialSecurityNumber:2018768901; earning:9297.23
add: employee exists.
firstName:Hai; lastName:Kong; socialSecurityNumber:2015673901; earning:17332.00
firstName:Hai; lastName:Kong; socialSecurityNumber:2015673901; earning:39996.00
firstName:Guo; lastName:Yang; socialSecurityNumber:2017672347; earning:4678.13
delete: employee not found.
firstName:Ai; lastName:Meng; socialSecurityNumber:2012673901; earning:17248.00
firstName:NanXiong; lastName:Qimu; socialSecurityNumber:2016782340; earning:3040.00
firstName:Hai; lastName:Kong; socialSecurityNumber:2015673901; earning:39996.00
源代码:
import java.util.*;
//这是上次作业的一部分,在这次作业中只是一个工具人,所以就可以偷懒少写点东西,能完成本次作业就可以了
class Employee
{
int Type;
String firstname, lastname, socialnumber;
double weeklySalary, wage, grossSales, hours;
double commissionrate, basesalary, earning;
Employee(int Type, String firstname, String lastname, String socialnumber)
{
this.Type = Type;
this.firstname = firstname;
this.lastname = lastname;
this.socialnumber = socialnumber;
}
double Earning(int type, double a, double b, double c)
{
if(type == 0)
{
this.weeklySalary = a;
earning = weeklySalary * 4.00;
}
if(type == 1 )
{
this.wage = a;
this.hours = b;
earning = wage * hours;
}
if(type == 2)
{
this.commissionrate = a;
this.grossSales = b;
earning = commissionrate * grossSales;
}
if(type == 3)
{
this.commissionrate = a;
this.grossSales = b;
this.basesalary = c;
earning = commissionrate * grossSales + basesalary;
}
return earning;
}
public String toString()
{
return "firstName:" +
firstname +
"; lastName:" +
lastname +
"; socialSecurityNumber:" +
socialnumber +
"; earning:" +
String.format("%.2f", earning);
}
}
class factory
{
private HashMap<String, Employee>employees = new HashMap<>();
void initEmployees(List<String> es)
{
//Integer.valueOf()是转换为Integer类型,.intValue()是转换为Int类型。
//用这种方式来实现字符串向其他方式的转换,来满足方法的输入需求。
Employee x = new Employee(Integer.valueOf(es.get(1)).intValue(), es.get(2), es.get(3), es.get(4));
x.Earning(Integer.valueOf(es.get(1)).intValue(), Double.valueOf(es.get(5)).doubleValue(), Double.valueOf(es.get(6)).doubleValue(), Double.valueOf(es.get(7)).doubleValue());
employees.put(es.get(4), x);
}
void getEmployee( String empSecNum)
{
int logic = 0;
//.keyset()方法取出了employees中所有的key值,并存入集合keyset中。
Set<String> keyset = employees.keySet();
//用迭代器遍历集合keyset。
Iterator<String> it = keyset.iterator();
while(it.hasNext())
{
String key = it.next();
//我太菜了,不知道怎么比较两个字符串,就把它们转换为整数类型再比较,而且转换为整数类型后没有超过最大限度。
int a = Integer.parseInt(empSecNum);
int b = Integer.parseInt(key);
if (a == b)
{
//找到相同号码后就返回对应的key值
logic = 1;
System.out.println(employees.get(key));
}
}
if(logic == 0)
{
System.out.println("get: employee not found.");
}
}
void deleteEmployee(String empSecNum)
{
//这就和上面一样的操作了,以后也都是这样的操作
//先取到key的值,然后再遍历key值
//这样做个人感觉很麻烦,可以考虑用Entry来遍历,不过我还没具体了解过(就是还没学会...)
int logic = 0;
Set<String> keyset = employees.keySet();
Iterator<String> it = keyset.iterator();
while(it.hasNext())
{
String key = it.next();
int a = Integer.parseInt(empSecNum);
int b = Integer.parseInt(key);
if (a == b)
{
logic = 1;
//.remove()方法删去了对应的key值和value值,但返回了value的值
//我们同样用一个对象来接收返回的值并返回这个对象
Employee body = employees.remove(empSecNum);
System.out.println(body);
break;
}
}
if(logic == 0)
{
System.out.println("delete: employee not found.");
}
}
Employee addEmployee(Employee stu)
{
int logic = 0;
Set<String> keyset = employees.keySet();
Iterator<String> it = keyset.iterator();
while(it.hasNext())
{
String key = it.next();
int a = Integer.parseInt(key);
int b = Integer.parseInt(stu.socialnumber);
if (a == b)
{
//如果存在相同的社会编号,就输出此编号已经存在了
logic = 1;
System.out.println("add: employee exists.");
return null;
}
}
if(logic == 0)
{
//如果不存在一样的社会编号,就可以把要加的员工信息写入了
employees.put(stu.socialnumber, stu);
System.out.println(stu);
return stu;
}
return null;
}
Employee updateEmployee(String empSecNum ,Employee emp)
{
int logic = 0;
Set<String> keyset = employees.keySet();
Iterator<String> it = keyset.iterator();
while(it.hasNext())
{
String key = it.next();
int a = Integer.parseInt(key);
int b = Integer.parseInt(empSecNum);
if (a == b)
{
//如果找到了要更新信息的社会编号,就重新写入,覆盖以前的信息
logic = 1;
employees.put(empSecNum,emp);
System.out.println(emp);
return emp;
}
}
if(logic == 0)
{
//如果没有找到要更新的社会编码,就输出这个编码的员工不存在
System.out.println("update: employee not found.");
return null;
}
return null;
}
void printEmployees()
{
//这里用了强for循环
//强for循环专门用来遍历数组和集合,它的内部原理其实就是个Iterator迭代器,能让我们少写几行代码
//到这里你可能会意识到上面的几次遍历都可以用强for循环来实现
//这样的小作业不考虑效率和速度,所以看个人喜好来选择遍历的方法了
for (String num : employees.keySet())
{
System.out.println(employees.get(num).toString());
}
}
}
public class FactoryTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
factory f = new factory();
while(in.hasNext())
{
//题目要求initEmployees(List es)方法的参数是列表
List<String> es = new ArrayList<String>();
String text = in.nextLine();
String[] str = text.split(" ");
for (int i = 0; i <8; i++)
{
if (i < str.length)
es.add(str[i]);
else
es.add("0");
}
if (str[0].equals("init"))
{
if (str[1].equals("-1")) continue;
else f.initEmployees(es);
}
else if(str[0].equals("get"))
{
f.getEmployee(str[1]);
}
else if(str[0].equals("add"))
{
Employee stu = new Employee(Integer.valueOf(es.get(1)).intValue(), es.get(2), es.get(3), es.get(4));
stu.Earning(Integer.valueOf(es.get(1)).intValue(), Double.valueOf(es.get(5)).doubleValue(), Double.valueOf(es.get(6)).doubleValue(), Double.valueOf(es.get(7)).doubleValue());
f.addEmployee(stu);
}
else if(str[0].equals("update"))
{
Employee emp = new Employee(Integer.valueOf(es.get(1)).intValue(), es.get(2), es.get(3), es.get(4));
emp.Earning(Integer.valueOf(es.get(1)).intValue(), Double.valueOf(es.get(5)).doubleValue(), Double.valueOf(es.get(6)).doubleValue(), Double.valueOf(es.get(7)).doubleValue());
f.updateEmployee(emp.socialnumber, emp);
}
else if(str[0].contentEquals("delete"))
{
f.deleteEmployee(es.get(1));
}
else if(str[0].contentEquals("print"))
{
f.printEmployees();
}
}
}
}