更改雇员属性
更改雇员属性有多种操作,主要为三大类,一类包括更改员工姓名、地址、员工类别;第二类为更改支付方式;第三类为更改从属关系,其中第二类和第三类操作类似。
所有操作都是以empId为参数,创建一个基类ChangeEmployeeTransaction;更改员工类别,会修改Employee对象中的同一个字段,所以创建一个抽象基类ChageClassifiationTransaction;同理,更改支付方式和从属关系也需各创建一个抽象基类。
设计模式:模板模式
把所有通用代码放入一个抽象基类的实现方法中,将所有实现细节交给基类的抽象方法。在这里,更改操作中的通用操作,从payrollDatabase中取出对应于empid的Employee对象。不同操作:是修改何种属性。
下图为更改雇员属性的结构图:
1.更改员工基本信息(姓名、地址)
下图为更改员工基本信息的动态图
下面给出更改员工地址的测试代码:
/**
* 改变员工地址
*/
@Test
public void testChangeAddressTransaction(){
int empId = 2;
String name = "Bob";
String address = "Bob.home";
double monthlyPay = 2000;
AddSalariedEmployee addSalariedEmployee = new AddSalariedEmployee(empId,name,address,monthlyPay);
addSalariedEmployee.execute();
Employee e = payrollDatabase.getEmployeeById(empId);
String newAddress = "Bob.address";
ChangeAddressTransaction changeAddressTransaction = new ChangeAddressTransaction(empId,newAddress);
changeAddressTransaction.execute();
assertEquals(e.getAddress(),newAddress);
}
根据测试代码,完善代码。
ChangeEmployeeTransaction:(采用模板模式,其中根据empId获取employee是每个更改都会执行的通用操作,修改哪一项属性是不定的,用抽象方法表示)
package implement.transaction;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
/**
* Created by ZD on 2017/10/24.
*/
public abstract class ChangeEmployeeTransaction implements Transaction {
private long empID;
public ChangeEmployeeTransaction(int empID) {
this.empID = empID;
}
public ChangeEmployeeTransaction(){}
public void execute() {
Employee e = PayrollDatabase.getPayrollDatabase().getEmployeeById(empID);
if (e != null){
change(e);
}
}
public abstract void change(Employee e);
}
ChangeAddressTransaction:(实现抽象基类中的change方法)
package implement.transaction;
import implement.entity.Employee;
/**
* Created by ZD on 2017/10/25.
*/
public class ChangeAddressTransaction extends ChangeEmployeeTransaction {
long empId;
String address;
public ChangeAddressTransaction(){}
public ChangeAddressTransaction(long empId,String address){
super((int) empId);
this.address = address;
}
public void change(Employee e) {
e.setAddress(address);
}
}
更改雇员姓名与更改地址类似。
2.更改雇员类别:
更改员工类别同样采用模板模式
下面给出更改员工类别的测试代码:
/**
* 改变员工属性–>转为小时工
*/
@Test
public void testChangeHourlyClassificationTransaction(){
int empId = 3;
String name = "Bob";
String address = "Bob.home";
double monthlyPsay = 3000;
double commissionRate = 5;
AddCommissionedEmployee addCommissionedEmployee = new AddCommissionedEmployee(empId,name,address,monthlyPsay,commissionRate);
addCommissionedEmployee.execute();
double hourlyPay = 15;
ChangeEmployeeTransaction changeHourlyTransaction = new ChangeHourlyTransaction(empId,hourlyPay);
changeHourlyTransaction.execute();
Employee e = payrollDatabase.getEmployeeById(empId);
HourlyClassification hourlyClassification = (HourlyClassification) e.getPaymentClassification();
assertEquals(e.getPaymentClassification(),hourlyClassification);
}
根据测试用例完善代码:
ChangeClassificationTransaction
package implement.transaction;
import implement.classification.PaymentClassification;
import implement.entity.Employee;
import implement.schedule.PaymentSchedule;
/**
* 更改类别
* Created by ZD on 2017/10/25.
*/
public abstract class ChangeClassificationTransaction extends ChangeEmployeeTransaction {
public ChangeClassificationTransaction(){}
public ChangeClassificationTransaction(int empId){
super(empId);
}
public void change(Employee e) {
e.setClassification(getClassification());
e.setSchedule(getSchedule());
}
protected abstract PaymentSchedule getSchedule();
protected abstract PaymentClassification getClassification();
}
ChangeHourlyTransaction
package implement.transaction;
import implement.classification.HourlyClassification;
import implement.classification.PaymentClassification;
import implement.schedule.PaymentSchedule;
import implement.schedule.WeeklySchedule;
/**
* Created by ZD on 2017/10/25.
*/
public class ChangeHourlyTransaction extends ChangeClassificationTransaction {
private double hourlyRate;
public ChangeHourlyTransaction(){}
public ChangeHourlyTransaction(long empId,double hourlyRate){
super((int) empId);
this.hourlyRate = hourlyRate;
}
public PaymentSchedule getSchedule(){
return new WeeklySchedule();
}
public PaymentClassification getClassification(){
return new HourlyClassification(hourlyRate);
}
}
3.更改雇员协会成员身份
测试用例:
/**
* 改变员工属性-->加入协会
*/
@Test
public void testChangeMemberTransaction(){
int empId = 1;
String name = "Bob";
String address = "Bob.home";
double monthlyPay = 3000;
AddSalariedEmployee addSalariedEmployee = new AddSalariedEmployee(empId,name,address,monthlyPay);
addSalariedEmployee.execute();
int memberId = 20;
ChangeEmployeeTransaction changeMemberTransaction = new ChangeMemberTransaction(empId,memberId,99.42);
changeMemberTransaction.execute();
Employee employee = payrollDatabase.getEmployeeById(empId);
UnionAffiliation af = (UnionAffiliation) employee.getAffiliation();
Employee member = payrollDatabase.getEmployeeByMemberId(memberId);
assertEquals(member,employee);
}
根据测试用例完善代码
ChangeAffiliationTransaction
package implement.transaction;
import implement.affiliation.Affiliation;
import implement.affiliation.NoAffiliation;
import implement.affiliation.UnionAffiliation;
import implement.entity.Employee;
/**
* Created by ZD on 2017/10/25.
*/
public abstract class ChangeAffiliationTransaction extends ChangeEmployeeTransaction {
public ChangeAffiliationTransaction(){}
public ChangeAffiliationTransaction(int empId){
super(empId);
}
public void change(Employee e) {
recordMemberShip(e);
e.setAffiliation(getAffiliation());
}
protected abstract Affiliation getAffiliation();
protected abstract void recordMemberShip(Employee e);
}
ChangeMemberTransaction
package implement.transaction;
import implement.affiliation.Affiliation;
import implement.affiliation.NoAffiliation;
import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
/**
* Created by ZD on 2017/10/25.
*/
public class ChangeMemberTransaction extends ChangeAffiliationTransaction {
private int memberId;
private double dues;//会费
public ChangeMemberTransaction(long empId,int memberId,double dues){
super((int) empId);
this.memberId = memberId;
this.dues = dues;
}
public ChangeMemberTransaction(){}
@Override
protected UnionAffiliation getAffiliation() {
return new UnionAffiliation(memberId,dues);
}
protected void recordMemberShip(Employee e) {
PayrollDatabase.getPayrollDatabase().addUnionMember(memberId,e);
}
}
ChangeUnaffiliatedTransaction
package implement.transaction;
import implement.affiliation.Affiliation;
import implement.affiliation.NoAffiliation;
import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
/**
* Created by ZD on 2017/10/26.
*/
public class ChangeUnaffiliatedTransaction extends ChangeAffiliationTransaction {
public ChangeUnaffiliatedTransaction(){}
public ChangeUnaffiliatedTransaction(int empId){
super(empId);
}
protected NoAffiliation getAffiliation() {
return new NoAffiliation();
}
protected void recordMemberShip(Employee e) {
UnionAffiliation unionAffiliation = (UnionAffiliation) e.getAffiliation();
int memberId = (int) unionAffiliation.getMemberID();
PayrollDatabase.getPayrollDatabase().removeUnionMember(memberId);
}
}