Skip to content

Commit 5d54986

Browse files
committed
[Feature] 完成临时工,雇员,销售的工作计算方法
1 parent 676ceb2 commit 5d54986

File tree

17 files changed

+422
-62
lines changed

17 files changed

+422
-62
lines changed

students/812350401/src/main/java/com/coderising/myood/payroll/liuxin_payroll/PayrollService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public Employee addSalariedEmployee(String name, String address, double salary){
3838
return e;
3939
}
4040

41-
public Employee addCommissionedEmployee(String name, String address, double salary, double saleRate){
41+
public Employee addCommissionedEmployee(String name, String address, double salary, double saleRate) {
4242
Employee e = new Employee(name, address);
4343
e.setClassification(new CommissionedClassification(salary, saleRate));
4444
e.setSchedule(new BiweeklySchedule());
4545
e.setPaymentMethod(new HoldMethod());
4646
//保存员工到数据库.. 略
4747
return e;
4848
}
49+
4950
}

students/812350401/src/main/java/com/coderising/myood/payroll/liuxin_payroll/domain/PaydayTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void execute(){
1414
List<Employee> employees = payrollService.getAllEmployees();
1515
for(Employee e : employees){
1616
if(e.isPayDay(date)){
17-
Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date),date);
17+
Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date), date);
1818
e.payDay(pc);
1919
payrollService.savePaycheck(pc);
2020
}

students/812350401/src/main/java/com/coderising/myood/payroll/my_payroll/database/DataBase.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.coderising.myood.payroll.my_payroll.database;
22

33

4+
import javax.xml.crypto.Data;
5+
import java.util.LinkedList;
46
import java.util.List;
57
import java.util.stream.Collectors;
68

@@ -9,8 +11,11 @@
911
*/
1012
public class DataBase {
1113
private String name;
12-
private List<Table> tables;
14+
private List<Table> tables = new LinkedList<>();
1315

16+
public DataBase(String name) {
17+
this.name = name;
18+
}
1419

1520
public boolean addTable(Table table) {
1621
if (!tables.stream()
@@ -29,4 +34,27 @@ public Table getTable(String name) {
2934
.findFirst()
3035
.orElse(null);
3136
}
37+
38+
public boolean drop(String name) {
39+
Table table = tables
40+
.stream()
41+
.filter(t -> name.equals(t.getName()))
42+
.findFirst()
43+
.orElse(null);
44+
if (table != null) {
45+
tables.remove(table);
46+
return true;
47+
} else {
48+
return false;
49+
}
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "DataBase{" +
55+
"database name='" + name + "', " +
56+
"\n" +
57+
"tables=" + tables +
58+
'}';
59+
}
3260
}

students/812350401/src/main/java/com/coderising/myood/payroll/my_payroll/domain/Employee.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ public Employee(String name, String address){
1818
this.address = address;
1919
}
2020
public boolean isPayDay(Date d) {
21-
return false;
21+
return schedule.isPayDate(d);
2222
}
2323

2424
public Date getPayPeriodStartDate(Date d) {
25-
return null;
25+
return schedule.getPayPeriodStartDate(d);
2626
}
2727

2828
public void payDay(Paycheck pc){
29-
29+
// TODO: 16/9/2017
30+
31+
3032
}
3133

3234
public void setClassification(PaymentClassification classification) {

students/812350401/src/main/java/com/coderising/myood/payroll/my_payroll/domain/Paycheck.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,60 @@ public class Paycheck {
88
private double grossPay;
99
private double netPay;
1010
private double deductions;
11-
11+
private int employeeId;
12+
1213
public Paycheck(Date payPeriodStart, Date payPeriodEnd){
1314
this.payPeriodStart = payPeriodStart;
1415
this.payPeriodEnd = payPeriodEnd;
1516
}
17+
18+
public Date getPayPeriodStart() {
19+
return payPeriodStart;
20+
}
21+
22+
public void setPayPeriodStart(Date payPeriodStart) {
23+
this.payPeriodStart = payPeriodStart;
24+
}
25+
26+
public Date getPayPeriodEnd() {
27+
return payPeriodEnd;
28+
}
29+
30+
public void setPayPeriodEnd(Date payPeriodEnd) {
31+
this.payPeriodEnd = payPeriodEnd;
32+
}
33+
34+
public double getGrossPay() {
35+
return grossPay;
36+
}
37+
38+
public double getNetPay() {
39+
return netPay;
40+
}
41+
42+
public double getDeductions() {
43+
return deductions;
44+
}
45+
46+
public int getEmployeeId() {
47+
return employeeId;
48+
}
49+
50+
public void setEmployeeId(int employeeId) {
51+
this.employeeId = employeeId;
52+
}
53+
1654
public void setGrossPay(double grossPay) {
1755
this.grossPay = grossPay;
1856

1957
}
2058
public void setDeductions(double deductions) {
2159
this.deductions = deductions;
2260
}
61+
2362
public void setNetPay(double netPay){
2463
this.netPay = netPay;
2564
}
26-
public Date getPayPeriodEndDate() {
27-
28-
return this.payPeriodEnd;
29-
}
30-
public Date getPayPeriodStartDate() {
31-
32-
return this.payPeriodStart;
33-
}
65+
66+
3467
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.coderising.myood.payroll.my_payroll.domain;
22

33
public interface PaymentClassification {
4-
public double calculatePay(Paycheck pc);
4+
double calculatePay(Paycheck pc);
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.coderising.myood.payroll.my_payroll.domain;
22

33
public interface PaymentMethod {
4-
public void pay(Paycheck pc);
4+
void pay(Paycheck pc);
55
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentclassification;
2+
3+
import com.coderising.myood.payroll.my_payroll.domain.Paycheck;
4+
import com.coderising.myood.payroll.my_payroll.domain.PaymentClassification;
5+
import com.coderising.myood.payroll.my_payroll.util.DateUtil;
6+
import com.sun.istack.internal.NotNull;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
/**
12+
* Created by thomas_young on 16/9/2017.
13+
* 销售计算工资
14+
*/
15+
public class CommissionClassification implements PaymentClassification {
16+
private double baseSalary;
17+
private double commissionRate;
18+
private List<SalesReceipt> salesReceipts;
19+
20+
public CommissionClassification(double baseSalary, double commissionRate) {
21+
this.baseSalary = baseSalary;
22+
this.commissionRate = commissionRate;
23+
salesReceipts = new LinkedList<>();
24+
}
25+
26+
@Override
27+
public double calculatePay(Paycheck pc) {
28+
if (pc == null) return baseSalary;
29+
return salesReceipts.stream()
30+
.filter(s -> DateUtil.between(s.getSaleDate(), pc.getPayPeriodStart(), pc.getPayPeriodEnd()))
31+
.map(this::calculateSalePay)
32+
.reduce(baseSalary, Double::sum);
33+
}
34+
35+
public void addSalesReceipt(SalesReceipt salesReceipt) {
36+
this.salesReceipts.add(salesReceipt);
37+
}
38+
39+
private double calculateSalePay(SalesReceipt salesReceipt) {
40+
return salesReceipt.getAmount() * commissionRate;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentclassification;
2+
3+
import com.coderising.myood.payroll.my_payroll.domain.Paycheck;
4+
import com.coderising.myood.payroll.my_payroll.domain.PaymentClassification;
5+
import com.coderising.myood.payroll.my_payroll.util.DateUtil;
6+
7+
import java.util.DoubleSummaryStatistics;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
/**
12+
* Created by thomas_young on 16/9/2017.
13+
* 临时工工资计算
14+
*/
15+
public class HourlyClassification implements PaymentClassification {
16+
private int MAX_HOURS = 8;
17+
private double EXTRA_SCALE= 1.5;
18+
private double hourlyRate;
19+
private List<TimeCard> timeCards;
20+
21+
public HourlyClassification(double hourlyRate) {
22+
this.hourlyRate = hourlyRate;
23+
timeCards = new LinkedList<>();
24+
}
25+
26+
@Override
27+
public double calculatePay(Paycheck pc) {
28+
double totalPay = timeCards.stream()
29+
.filter(t -> DateUtil.between(t.getDate(), pc.getPayPeriodStart(), pc.getPayPeriodEnd()))
30+
.map(this::calulateTimeCardPay)
31+
.reduce(0d, Double::sum);
32+
return totalPay;
33+
}
34+
35+
public void addTimeCard(TimeCard timeCard) {
36+
timeCards.add(timeCard);
37+
}
38+
39+
public List<TimeCard> getTimeCards() {
40+
return timeCards;
41+
}
42+
43+
private double calulateTimeCardPay(TimeCard timeCard) {
44+
if (timeCard.getHours() <= MAX_HOURS) {
45+
return hourlyRate * timeCard.getHours();
46+
} else {
47+
return hourlyRate * MAX_HOURS + EXTRA_SCALE * hourlyRate * (timeCard.getHours() - MAX_HOURS);
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentclassification;
2+
3+
import com.coderising.myood.payroll.my_payroll.domain.Paycheck;
4+
import com.coderising.myood.payroll.my_payroll.domain.PaymentClassification;
5+
6+
/**
7+
* Created by thomas_young on 16/9/2017.
8+
* 雇员工资计算
9+
*/
10+
public class SalariedClassification implements PaymentClassification {
11+
private double salary; // 月工资
12+
13+
public SalariedClassification(double salary) {
14+
this.salary = salary;
15+
}
16+
17+
@Override
18+
public double calculatePay(Paycheck pc) {
19+
return salary;
20+
}
21+
22+
public double getSalary() {
23+
return salary;
24+
}
25+
26+
public void setSalary(double salary) {
27+
this.salary = salary;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentclassification;
2+
3+
import com.coderising.myood.payroll.my_payroll.util.DateUtil;
4+
5+
import java.util.Date;
6+
7+
public class SalesReceipt {
8+
private Date saleDate;
9+
private double amount;
10+
11+
public SalesReceipt(String dateStr, double amount) {
12+
this.saleDate = DateUtil.parseDate(dateStr);
13+
this.amount = amount;
14+
}
15+
16+
public Date getSaleDate() {
17+
return saleDate;
18+
}
19+
public double getAmount() {
20+
return amount;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentclassification;
2+
3+
4+
import com.coderising.myood.payroll.my_payroll.util.DateUtil;
5+
6+
import java.util.Date;
7+
8+
/**
9+
* Created by thomas_young on 16/9/2017.
10+
*/
11+
public class TimeCard {
12+
13+
14+
private Date date;
15+
private int hours;
16+
17+
public TimeCard(String dateStr, int hours) {
18+
this.date = DateUtil.parseDate(dateStr);
19+
this.hours = hours;
20+
}
21+
22+
public Date getDate() {
23+
return date;
24+
}
25+
26+
public void setDate(Date date) {
27+
this.date = date;
28+
}
29+
30+
public int getHours() {
31+
return hours;
32+
}
33+
34+
public void setHours(int hours) {
35+
this.hours = hours;
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.coderising.myood.payroll.my_payroll.paymentmethod;
2+
3+
import com.coderising.myood.payroll.my_payroll.domain.Paycheck;
4+
import com.coderising.myood.payroll.my_payroll.domain.PaymentMethod;
5+
6+
/**
7+
* Created by thomas_young on 16/9/2017.
8+
* 把钱发到财务那里,所示支取
9+
*/
10+
public class HoldMethod implements PaymentMethod {
11+
@Override
12+
public void pay(Paycheck pc) {
13+
StringBuilder desp = new StringBuilder();
14+
desp.append("财务入账:\n")
15+
.append("employee_id: ").append(pc.getEmployeeId())
16+
.append(", 金额: ").append(pc.getNetPay())
17+
.append(", 区间: ").append(pc.getPayPeriodStart())
18+
.append("~").append(pc.getPayPeriodEnd());
19+
System.out.println(desp.toString());
20+
}
21+
}

students/812350401/src/main/java/com/coderising/myood/payroll/my_payroll/util/DateUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ public static void main(String [] args) throws Exception{
5454
public static boolean between(Date d, Date date1, Date date2){
5555
return d.after(date1) && d.before(date2);
5656
}
57+
5758
}

0 commit comments

Comments
 (0)