Skip to content

Commit aba6c86

Browse files
author
sheng
committed
payroll
1 parent b5f9d48 commit aba6c86

28 files changed

+668
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.AddEmployeeTransaction;
4+
import com.coderising.payroll.api.Affiliation;
5+
import com.coderising.payroll.api.PaymentClassification;
6+
import com.coderising.payroll.api.PaymentMethod;
7+
import com.coderising.payroll.api.PaymentSchedule;
8+
9+
public class AddCommissionEmployeeTransaction extends AddEmployeeTransaction{
10+
private double rate;
11+
private double salary;
12+
public AddCommissionEmployeeTransaction(String name, String address, PaymentMethod paymentMethod,
13+
Affiliation affiliation) {
14+
super(name, address, paymentMethod, affiliation);
15+
this.rate=rate;
16+
}
17+
18+
@Override
19+
public PaymentClassification getClassification() {
20+
return new CommissionClassification(rate,salary);
21+
}
22+
23+
@Override
24+
public PaymentSchedule getSchedule() {
25+
return new WeeklySchedule();
26+
}
27+
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.Affiliation;
4+
import com.coderising.payroll.api.PaymentClassification;
5+
import com.coderising.payroll.api.PaymentMethod;
6+
import com.coderising.payroll.api.PaymentSchedule;
7+
8+
public abstract class AddEmployeeTransaction {
9+
private String name;
10+
private String address;
11+
private PaymentMethod paymentMethod;
12+
private Affiliation affiliation;
13+
14+
15+
public AddEmployeeTransaction(String name, String address, PaymentMethod paymentMethod, Affiliation affiliation) {
16+
super();
17+
this.name = name;
18+
this.address = address;
19+
this.paymentMethod = paymentMethod;
20+
this.affiliation = affiliation;
21+
}
22+
23+
public abstract PaymentClassification getClassification();
24+
25+
public abstract PaymentSchedule getSchedule();
26+
27+
public void execute(){
28+
PaymentClassification pc=getClassification();
29+
PaymentSchedule ps=getSchedule();
30+
Employee e=new Employee(name, address);
31+
e.setClassification(pc);
32+
e.setSchedule(ps);
33+
e.setPaymentMethod(paymentMethod);
34+
e.setAffiliation(affiliation);
35+
}
36+
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.AddEmployeeTransaction;
4+
import com.coderising.payroll.api.Affiliation;
5+
import com.coderising.payroll.api.PaymentClassification;
6+
import com.coderising.payroll.api.PaymentMethod;
7+
import com.coderising.payroll.api.PaymentSchedule;
8+
9+
public class AddHourlyEmployeeTransaction extends AddEmployeeTransaction{
10+
private double rate;
11+
public AddHourlyEmployeeTransaction(String name, String address, PaymentMethod paymentMethod,
12+
Affiliation affiliation) {
13+
super(name, address, paymentMethod, affiliation);
14+
this.rate=rate;
15+
}
16+
17+
@Override
18+
public PaymentClassification getClassification() {
19+
return new HourlyClassification(rate);
20+
}
21+
22+
@Override
23+
public PaymentSchedule getSchedule() {
24+
return new WeeklySchedule();
25+
}
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.AddEmployeeTransaction;
4+
import com.coderising.payroll.api.Affiliation;
5+
import com.coderising.payroll.api.PaymentClassification;
6+
import com.coderising.payroll.api.PaymentMethod;
7+
import com.coderising.payroll.api.PaymentSchedule;
8+
9+
public class AddSalariedEmployeeTransaction extends AddEmployeeTransaction{
10+
private double salary;
11+
public AddSalariedEmployeeTransaction(String name, String address, PaymentMethod paymentMethod,
12+
Affiliation affiliation) {
13+
super(name, address, paymentMethod, affiliation);
14+
this.salary=salary;
15+
}
16+
17+
@Override
18+
public PaymentClassification getClassification() {
19+
return new SalariedClassification(salary);
20+
}
21+
22+
@Override
23+
public PaymentSchedule getSchedule() {
24+
return new MonthlySchedule();
25+
}
26+
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.PaymentMethod;
4+
5+
public class BankMethod implements PaymentMethod{
6+
7+
private String bank;
8+
private String account;
9+
10+
@Override
11+
public void pay(Paycheck pc) {
12+
System.out.println("BankMethod");
13+
}
14+
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
5+
import com.coderising.payroll.api.PaymentSchedule;
6+
import com.coderising.payroll.util.DateUtil;
7+
8+
public class BiWeeklySchedule implements PaymentSchedule {
9+
Date firstPayableFriday = DateUtil.parseDate("2017-06-02");
10+
11+
@Override
12+
public boolean isPayDate(Date date) {
13+
int interval = DateUtil.getDaysBetween(firstPayableFriday, date);
14+
return interval % 14 == 0;
15+
}
16+
17+
@Override
18+
public Date getPayPeriodStartDate(Date payPeriodEndDate) {
19+
return DateUtil.add(payPeriodEndDate, -13);
20+
}
21+
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
import com.coderising.payroll.api.PaymentClassification;
7+
import com.coderising.payroll.util.DateUtil;
8+
9+
public class CommissionClassification implements PaymentClassification{
10+
private double salary;
11+
private double rate;
12+
private Map<Date,SalesReceipt> receipts;
13+
14+
public CommissionClassification(double rate, double salary) {
15+
this.rate=rate;
16+
this.salary=salary;
17+
}
18+
19+
public void addSalesReceipt(SalesReceipt sr) {
20+
receipts.put(sr.getSaleDate(), sr);
21+
}
22+
23+
@Override
24+
public double calculatePay(Paycheck pc) {
25+
double commission=0;
26+
for (SalesReceipt sr : receipts.values()) {
27+
if (DateUtil.between(sr.getSaleDate(), pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) {
28+
commission+=sr.getAmount()*rate;
29+
}
30+
}
31+
return salary+commission;
32+
}
33+
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
5+
import com.coderising.payroll.api.Affiliation;
6+
import com.coderising.payroll.api.PaymentClassification;
7+
import com.coderising.payroll.api.PaymentMethod;
8+
import com.coderising.payroll.api.PaymentSchedule;
9+
10+
public class Employee {
11+
String id;
12+
String name;
13+
String address;
14+
15+
Affiliation affiliation;
16+
PaymentClassification classification;
17+
PaymentSchedule schedule;
18+
PaymentMethod paymentMethod;
19+
20+
public Employee(String name, String address){
21+
this.name = name;
22+
this.address = address;
23+
}
24+
public boolean isPayDay(Date d) {
25+
return schedule.isPayDate(d);
26+
}
27+
28+
public Date getPayPeriodStartDate(Date d) {
29+
return schedule.getPayPeriodStartDate(d);
30+
}
31+
32+
public void payDay(Paycheck pc){
33+
double grossPay=classification.calculatePay(pc);
34+
double deductions=affiliation.calculateDeductions(pc);
35+
double netPay=grossPay-deductions;
36+
pc.setGrossPay(grossPay);
37+
pc.setDeductions(deductions);
38+
pc.setNetPay(netPay);
39+
paymentMethod.pay(pc);
40+
}
41+
42+
public void setClassification(PaymentClassification classification) {
43+
this.classification = classification;
44+
}
45+
public void setSchedule(PaymentSchedule schedule) {
46+
this.schedule = schedule;
47+
}
48+
public void setPaymentMethod(PaymentMethod paymentMethod) {
49+
this.paymentMethod = paymentMethod;
50+
}
51+
52+
public void setAffiliation(Affiliation affiliation) {
53+
this.affiliation = affiliation;
54+
}
55+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.PaymentMethod;
4+
5+
public class HoldMethod implements PaymentMethod{
6+
7+
@Override
8+
public void pay(Paycheck pc) {
9+
System.out.println("HoldMethod");
10+
}
11+
12+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
import com.coderising.payroll.api.PaymentClassification;
7+
import com.coderising.payroll.util.DateUtil;
8+
9+
public class HourlyClassification implements PaymentClassification {
10+
11+
private double rate;
12+
private Map<Date, TimeCard> timeCards;
13+
14+
public HourlyClassification(double rate) {
15+
this.rate=rate;
16+
}
17+
18+
public void addTimeCard(TimeCard tc) {
19+
timeCards.put(tc.getDate(), tc);
20+
}
21+
22+
@Override
23+
public double calculatePay(Paycheck pc) {
24+
double totalPay = 0;
25+
for (TimeCard tc : timeCards.values()) {
26+
if (DateUtil.between(tc.getDate(), pc.getPayPeriodStartDate(), 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+
if (hours > 8) {
36+
return 8 * rate + (hours - 8) * 1.5 * rate;
37+
} else {
38+
return 8 * rate;
39+
}
40+
}
41+
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.PaymentMethod;
4+
5+
public class MailMethod implements PaymentMethod{
6+
private String address;
7+
8+
@Override
9+
public void pay(Paycheck pc) {
10+
System.out.println("MailMethod");
11+
}
12+
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
5+
import com.coderising.payroll.api.PaymentSchedule;
6+
import com.coderising.payroll.util.DateUtil;
7+
8+
public class MonthlySchedule implements PaymentSchedule{
9+
10+
@Override
11+
public boolean isPayDate(Date date) {
12+
return DateUtil.isLasyDayOfMonth(date);
13+
}
14+
15+
@Override
16+
public Date getPayPeriodStartDate(Date payPeriodEndDate) {
17+
return DateUtil.getFirstDay(payPeriodEndDate);
18+
}
19+
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.payroll;
2+
3+
import com.coderising.payroll.api.Affiliation;
4+
5+
public class NonAffiliation implements Affiliation{
6+
7+
@Override
8+
public double calculateDeductions(Paycheck pc) {
9+
return 0;
10+
}
11+
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderising.payroll;
2+
3+
import java.util.Date;
4+
5+
public class Paycheck {
6+
private Date payPeriodStart;
7+
private Date payPeriodEnd;
8+
private double grossPay;
9+
private double netPay;
10+
private double deductions;
11+
12+
public Paycheck(Date payPeriodStart, Date payPeriodEnd){
13+
this.payPeriodStart = payPeriodStart;
14+
this.payPeriodEnd = payPeriodEnd;
15+
}
16+
public void setGrossPay(double grossPay) {
17+
this.grossPay = grossPay;
18+
19+
}
20+
public void setDeductions(double deductions) {
21+
this.deductions = deductions;
22+
}
23+
public void setNetPay(double netPay){
24+
this.netPay = netPay;
25+
}
26+
public Date getPayPeriodEndDate() {
27+
28+
return this.payPeriodEnd;
29+
}
30+
public Date getPayPeriodStartDate() {
31+
32+
return this.payPeriodStart;
33+
}
34+
}

0 commit comments

Comments
 (0)