薪水支付案例(5)

协会成员

采用NULL OBJECT模式设计(消除对null对象的检查,有助于简化代码),在Employee对象包含的一组Affiliation对象中搜索UnionAffiliation对象,然后将ServiceCharge增加到搜寻到的UnionAffiliation对象中。

下面是静态图以及对应的时序图:

静态图:
这里写图片描述

时序图:
这里写图片描述

下面给出部分代码片段,依然是从测试代码开始:

测试代码中简单创建一个雇员并向其增加一个UnionAffiliation对象

ServiceChargeTransactionTest

package salary;

import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
import implement.entity.ServiceCharge;
import implement.transaction.AddHourlyEmployee;
import implement.transaction.AddServiceChargeTransaction;
import org.junit.Test;
import java.util.Date;

/**
 * 会费扣除测试
 * Created by ZD on 2017/10/24.
 */
public class ServiceChargeTransactionTest {
    PayrollDatabase payrollDatabase = PayrollDatabase.getPayrollDatabase();
    @Test
    public void testAddServiceChargeTransaction(){
         int empId = 7;
         String name = "Bob7";
         String address = "Bob7.home";
         double hourlyPay = 25;

         int memberId = 1;
         double amount = 12.5;
         Date date = new Date();

        AddHourlyEmployee addHourlyEmployee = new AddHourlyEmployee(empId,name,address,hourlyPay);
        addHourlyEmployee.execute();
        Employee e = payrollDatabase.getEmployeeById(empId);
        UnionAffiliation af = new UnionAffiliation(memberId,amount);
        e.setAffiliation(af);

        payrollDatabase.addUnionMember(memberId,e);
        AddServiceChargeTransaction serviceChargeTransaction = new AddServiceChargeTransaction(memberId,date,amount);
        serviceChargeTransaction.execute();

        ServiceCharge serviceCharge = af.getServiceCharge(date);
        System.out.println(serviceCharge.getAmount());
    }
}

ServiceCharge

package implement.entity;
import java.util.Date;
/**
 *会费
 * Created by ZD on 2017/10/24.
 */
public class ServiceCharge {
    private Date date;
    private double amount;
    public ServiceCharge(){}
    public ServiceCharge(Date date,double amount){
        this.amount = amount;
        this.date = date;
    }
    public Date getDate() {
        return date;
    }
    public double getAmount() {
        return amount;
    }
}

AddServiceChargeTransaction

package implement.transaction;
import implement.affiliation.Affiliation;
import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
import implement.entity.ServiceCharge;

import java.util.Date;

/**
 * Created by ZD on 2017/10/24.
 */
public class AddServiceChargeTransaction implements Transaction {

    private long memberId;
    private Date date;
    private double amount;


    public AddServiceChargeTransaction(){}

    public AddServiceChargeTransaction(long memberId, Date date, double amount){
        this.memberId = memberId;
        this.date = date;
        this.amount = amount;
    }

    public void execute() {
        Employee e = PayrollDatabase.getPayrollDatabase().getEmployeeByMemberId(memberId);
        ServiceCharge charge = new ServiceCharge(date,amount);

        Affiliation affiliation = e.getAffiliation();
        if (affiliation instanceof UnionAffiliation){
            ((UnionAffiliation)affiliation).addServiceCharge(new ServiceCharge(date,amount));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值