Skip to content

Commit d7e7296

Browse files
committed
[Add] 老师代码先搬过来
1 parent 2b64d13 commit d7e7296

30 files changed

+527
-8
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.coderising.myood.payroll.liuxin_payroll;
2+
3+
import java.util.List;
4+
5+
import com.coderising.payroll.classification.CommissionedClassification;
6+
import com.coderising.payroll.classification.HourlyClassification;
7+
import com.coderising.payroll.classification.SalariedClassification;
8+
import com.coderising.payroll.domain.Employee;
9+
import com.coderising.payroll.domain.HoldMethod;
10+
import com.coderising.payroll.domain.Paycheck;
11+
import com.coderising.payroll.schedule.BiweeklySchedule;
12+
import com.coderising.payroll.schedule.MonthlySchedule;
13+
import com.coderising.payroll.schedule.WeeklySchedule;
14+
15+
public class PayrollService {
16+
public List<Employee> getAllEmployees(){
17+
return null;
18+
}
19+
public void savePaycheck(Paycheck pc){
20+
21+
}
22+
23+
public Employee addHourlyEmployee(String name, String address, double hourlyRate){
24+
Employee e = new Employee(name, address);
25+
e.setClassification(new HourlyClassification(hourlyRate));
26+
e.setSchedule(new WeeklySchedule());
27+
e.setPaymentMethod(new HoldMethod());
28+
//保存员工到数据库.. 略
29+
return e;
30+
}
31+
32+
public Employee addSalariedEmployee(String name, String address, double salary){
33+
Employee e = new Employee(name, address);
34+
e.setClassification(new SalariedClassification(salary));
35+
e.setSchedule(new MonthlySchedule());
36+
e.setPaymentMethod(new HoldMethod());
37+
//保存员工到数据库.. 略
38+
return e;
39+
}
40+
41+
public Employee addCommissionedEmployee(String name, String address, double salary, double saleRate){
42+
Employee e = new Employee(name, address);
43+
e.setClassification(new CommissionedClassification(salary, saleRate));
44+
e.setSchedule(new BiweeklySchedule());
45+
e.setPaymentMethod(new HoldMethod());
46+
//保存员工到数据库.. 略
47+
return e;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.affiliation;
2+
3+
import com.coderising.payroll.domain.Affiliation;
4+
import com.coderising.payroll.domain.Paycheck;
5+
6+
public class NonAffiliation implements Affiliation{
7+
public double calculateDeductions(Paycheck pc){
8+
return 0.0;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.affiliation;
2+
3+
import com.coderising.payroll.domain.Affiliation;
4+
import com.coderising.payroll.domain.Paycheck;
5+
6+
public class UnionAffiliation implements Affiliation {
7+
8+
@Override
9+
public double calculateDeductions(Paycheck pc) {
10+
11+
return 0;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.classification;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
import com.coderising.payroll.domain.Paycheck;
7+
import com.coderising.payroll.domain.PaymentClassification;
8+
import com.coderising.payroll.domain.SalesReceipt;
9+
import com.coderising.payroll.util.DateUtil;
10+
11+
public class CommissionedClassification implements PaymentClassification {
12+
double salary;
13+
double rate;
14+
public CommissionedClassification(double salary , double rate){
15+
this.salary = salary;
16+
this.rate = rate;
17+
}
18+
Map<Date, SalesReceipt> receipts;
19+
@Override
20+
public double calculatePay(Paycheck pc) {
21+
double commission = 0.0;
22+
for(SalesReceipt sr : receipts.values()){
23+
if(DateUtil.between(sr.getSaleDate(), pc.getPayPeriodStartDate(),
24+
pc.getPayPeriodEndDate())){
25+
commission += sr.getAmount() * rate;
26+
}
27+
}
28+
return salary + commission;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.classification;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
import com.coderising.payroll.domain.Paycheck;
7+
import com.coderising.payroll.domain.PaymentClassification;
8+
import com.coderising.payroll.domain.TimeCard;
9+
import com.coderising.payroll.util.DateUtil;
10+
11+
public class HourlyClassification implements PaymentClassification {
12+
private double rate;
13+
private Map<Date, TimeCard> timeCards;
14+
15+
public HourlyClassification(double hourlyRate) {
16+
this.rate = hourlyRate;
17+
}
18+
public void addTimeCard(TimeCard tc){
19+
timeCards.put(tc.getDate(), tc);
20+
}
21+
@Override
22+
public double calculatePay(Paycheck pc) {
23+
double totalPay = 0;
24+
for(TimeCard tc : timeCards.values()){
25+
if(DateUtil.between(tc.getDate(), pc.getPayPeriodStartDate(),
26+
pc.getPayPeriodEndDate())){
27+
totalPay += calculatePayForTimeCard(tc);
28+
}
29+
}
30+
return totalPay;
31+
32+
}
33+
private double calculatePayForTimeCard(TimeCard tc) {
34+
int hours = tc.getHours();
35+
36+
if(hours > 8){
37+
return 8*rate + (hours-8) * rate * 1.5;
38+
} else{
39+
return 8*rate;
40+
}
41+
}
42+
}
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.classification;
2+
3+
import com.coderising.payroll.domain.Paycheck;
4+
import com.coderising.payroll.domain.PaymentClassification;
5+
6+
public class SalariedClassification implements PaymentClassification {
7+
private double salary;
8+
public SalariedClassification(double salary){
9+
this.salary = salary;
10+
}
11+
@Override
12+
public double calculatePay(Paycheck pc) {
13+
return salary;
14+
}
15+
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
public interface Affiliation {
4+
public double calculateDeductions(Paycheck pc);
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
5+
public class Employee {
6+
private String id;
7+
private String name;
8+
private String address;
9+
private Affiliation affiliation;
10+
11+
12+
private PaymentClassification classification;
13+
private PaymentSchedule schedule;
14+
private PaymentMethod paymentMethod;
15+
16+
public Employee(String name, String address){
17+
this.name = name;
18+
this.address = address;
19+
}
20+
public boolean isPayDay(Date d) {
21+
return this.schedule.isPayDate(d);
22+
}
23+
24+
public Date getPayPeriodStartDate(Date d) {
25+
return this.schedule.getPayPeriodStartDate(d);
26+
}
27+
28+
public void payDay(Paycheck pc){
29+
double grossPay = classification.calculatePay(pc);
30+
double deductions = affiliation.calculateDeductions(pc);
31+
double netPay = grossPay - deductions;
32+
pc.setGrossPay(grossPay);
33+
pc.setDeductions(deductions);
34+
pc.setNetPay(netPay);
35+
paymentMethod.pay(pc);
36+
}
37+
38+
public void setClassification(PaymentClassification classification) {
39+
this.classification = classification;
40+
}
41+
public void setSchedule(PaymentSchedule schedule) {
42+
this.schedule = schedule;
43+
}
44+
public void setPaymentMethod(PaymentMethod paymentMethod) {
45+
this.paymentMethod = paymentMethod;
46+
}
47+
}
48+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
public class HoldMethod implements PaymentMethod {
4+
5+
@Override
6+
public void pay(Paycheck pc) {
7+
8+
9+
}
10+
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
public class Paycheck {
7+
private Date payPeriodStart;
8+
private Date payPeriodEnd;
9+
private double grossPay;
10+
private double netPay;
11+
private double deductions;
12+
private Map<String, String> itsFields;
13+
public Paycheck(Date payPeriodStart, Date payPeriodEnd){
14+
this.payPeriodStart = payPeriodStart;
15+
this.payPeriodEnd = payPeriodEnd;
16+
}
17+
public void setGrossPay(double grossPay) {
18+
this.grossPay = grossPay;
19+
20+
}
21+
public void setDeductions(double deductions) {
22+
this.deductions = deductions;
23+
}
24+
public void setNetPay(double netPay){
25+
this.netPay = netPay;
26+
}
27+
public Date getPayPeriodEndDate() {
28+
29+
return this.payPeriodEnd;
30+
}
31+
public Date getPayPeriodStartDate() {
32+
33+
return this.payPeriodStart;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import com.coderising.payroll.PayrollService;
7+
8+
public class PaydayTransaction {
9+
private Date date;
10+
private PayrollService payrollService;
11+
12+
public void execute(){
13+
List<Employee> employees = payrollService.getAllEmployees();
14+
for(Employee e : employees){
15+
if(e.isPayDay(date)){
16+
Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date),date);
17+
e.payDay(pc);
18+
payrollService.savePaycheck(pc);
19+
}
20+
}
21+
}
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
public interface PaymentClassification {
4+
public double calculatePay(Paycheck pc);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
public interface PaymentMethod {
4+
public void pay(Paycheck pc);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
5+
public interface PaymentSchedule {
6+
public boolean isPayDate(Date date);
7+
public Date getPayPeriodStartDate(Date payPeriodEndDate);
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
5+
public class SalesReceipt {
6+
private Date saleDate;
7+
private double amount;
8+
public Date getSaleDate() {
9+
return saleDate;
10+
}
11+
public double getAmount() {
12+
return amount;
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.domain;
2+
3+
import java.util.Date;
4+
5+
public class TimeCard {
6+
private Date date;
7+
private int hours;
8+
9+
public Date getDate() {
10+
return date;
11+
}
12+
public int getHours() {
13+
return hours;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.coderising.myood.payroll.liuxin_payroll.schedule;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import com.coderising.payroll.domain.PaymentSchedule;
7+
import com.coderising.payroll.util.DateUtil;
8+
9+
public class BiweeklySchedule implements PaymentSchedule {
10+
Date firstPayableFriday = DateUtil.parseDate("2017-6-2");
11+
12+
@Override
13+
public boolean isPayDate(Date date) {
14+
15+
long interval = DateUtil.getDaysBetween(firstPayableFriday, date);
16+
return interval % 14 == 0;
17+
}
18+
19+
@Override
20+
public Date getPayPeriodStartDate(Date payPeriodEndDate) {
21+
return DateUtil.add(payPeriodEndDate, -13);
22+
23+
}
24+
25+
public static void main(String [] args) throws Exception{
26+
BiweeklySchedule schedule = new BiweeklySchedule();
27+
28+
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
29+
Date d = sdf.parse("2017-06-30");
30+
31+
System.out.println(schedule.isPayDate(d));
32+
33+
System.out.println(DateUtil.isFriday(d));
34+
35+
System.out.println(schedule.getPayPeriodStartDate(d));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)