薪水支付案例(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));
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Solidity 编写的员工薪酬合约的案例: ``` pragma solidity ^0.8.0; contract EmployeeSalaryContract { address public owner; address public employee; uint public salary; uint public lastPayday; constructor(address _employee, uint _salary) { owner = msg.sender; employee = _employee; salary = _salary * 1 ether; // 将薪资转换为以 wei 为单位的整数 lastPayday = block.timestamp; } // 发放薪资 function paySalary() public { require(msg.sender == owner, "Only owner can pay salary."); uint nextPayday = lastPayday + 30 days; require(nextPayday < block.timestamp, "It's not time to pay salary yet."); uint payments = (block.timestamp - lastPayday) / 30 days; uint amount = payments * salary; lastPayday = nextPayday; payable(employee).transfer(amount); } // 修改员工地址 function changeEmployee(address newEmployee) public { require(msg.sender == owner, "Only owner can change employee."); employee = newEmployee; } // 修改员工薪资 function changeSalary(uint newSalary) public { require(msg.sender == owner, "Only owner can change salary."); salary = newSalary * 1 ether; } // 查询合约余额 function getBalance() public view returns(uint) { return address(this).balance; } } ``` 该合约包含了发放薪资、修改员工地址、修改员工薪资和查询合约余额等功能。在合约创建时需要指定员工地址和薪资,薪资会以以 wei 为单位的整数存储,每月发放一次,发放前需要检查是否到了发薪日,并计算应发的薪资金额。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值