Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed dicegame.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void setAttribute(String name, String value) {
Attribute attr = findAttribute(name);
if(attr != null){
attr.value = value;
return;
}

attributes.add(new Attribute(name,value));
Expand Down
Binary file removed shopping.png
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.payroll;

public interface Affiliation {
public double calculateDeductions(Paycheck pc);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.coderising.payroll;

public class BankMethod implements PaymentMethod{
private String account;
public void pay(Paycheck pc){
System.out.println("转到银行账号:"+account+"。备注:"+pc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.coderising.payroll;

import java.util.Date;

public class BiWeeklySchedule implements PaymentSchedule{
private Date firstPayDay;
public BiWeeklySchedule(String firstPayDay){
this.firstPayDay = DateUtil.parseDate(firstPayDay);
}

public boolean isPayDate(Date date){
return DateUtil.getDaysBetween(firstPayDay, date)%14 == 0;
}

public Date getPayPeriodStartDate( Date payPeriodEndDate){
return DateUtil.add(payPeriodEndDate, -13);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.coderising.payroll;

import java.util.ArrayList;
import java.util.List;

public class ComissionClassification implements PaymentClassification {
private double salary;
private double rate;
List<SalesReceipt> receipts = new ArrayList<>();

public ComissionClassification(double salary, double rate, List<SalesReceipt> receipts) {
super();
this.salary = salary;
this.rate = rate;
this.receipts = receipts;
}


public double calculatePay(Paycheck pc){
double totalAmount = 0.0;
for(SalesReceipt sr:receipts){
if(DateUtil.between(sr.getSaleDate(),pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())){
totalAmount += sr.getAmount();
}
}
return salary + totalAmount*rate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.coderising.payroll;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
public static final long SECOND_IN_MS = 1000L;
public static final long MINUTE_IN_S = 60L;
public static final long HOUR_IN_MINUTE = 60L;
public static final long DAY_IN_HOUR = 24L;
public static final long DAY_IN_MS = DAY_IN_HOUR*HOUR_IN_MINUTE*MINUTE_IN_S*SECOND_IN_MS;
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

public static boolean isFriday(Date date){
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY;
}

public static int getDaysBetween(Date beginDate, Date endDate){
assert(endDate.after(beginDate));
return (int)((endDate.getTime() - beginDate.getTime())/DAY_IN_MS);
}

public static Date parseDate(String strDate){
try{
return sdf.parse(strDate);
}
catch(ParseException e){
throw new RuntimeException(e);
}
}

public static Date add(Date date, int dayCount){
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, dayCount);
return calendar.getTime();
}

/**
* 判断当前日期是否是当月最后一个工作日
* true: 当前日期是工作日并且与下一工作日不同月
* */
public static boolean isLastWorkDayOfMonth(Date d){
if(isWeekday(d)){
Date nextWeekday = nextWeekday(d);
Calendar calendar = new GregorianCalendar();
calendar.setTime(d);
int mouth = calendar.get(Calendar.MONTH);
calendar.setTime(nextWeekday);
return mouth != calendar.get(Calendar.MONTH);
}
return false;
}

public static boolean isWeekday(Date d){
Calendar calendar = new GregorianCalendar();
calendar.setTime(d);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return Calendar.MONDAY <= dayOfWeek && dayOfWeek <= Calendar.FRIDAY;
}

public static Date nextWeekday(Date d){
Calendar calendar = new GregorianCalendar();
calendar.setTime(d);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek) {
case Calendar.FRIDAY:
calendar.add(Calendar.DATE, 3);
break;
case Calendar.SATURDAY:
calendar.add(Calendar.DATE, 2);
break;
default:
calendar.add(Calendar.DATE, 1);
break;
}
return calendar.getTime();
}

public static Date getFirstDayOfMonth(Date d){
Calendar calendar = new GregorianCalendar();
calendar.setTime(d);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}

public static boolean between(Date d, Date startDate, Date endDate){
return d.compareTo(startDate)>=0 && d.compareTo(endDate)<=0;
}

public static String format(Date d){
return sdf.format(d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.coderising.payroll;

import java.util.Date;

public class Employee {
String id;
String name;
String address;
Affiliation affiliation = new NonAffiliation();


PaymentClassification classification;
PaymentSchedule schedule;
PaymentMethod paymentMethod;

public Employee(String id, String name, String address){
this.id = id;
this.name = name;
this.address = address;
}
public boolean isPayDay(Date d) {
return schedule.isPayDate(d);
}

public Date getPayPeriodStartDate(Date d) {
return schedule.getPayPeriodStartDate(d);
}

public void payDay(Paycheck pc){
double grossPay = classification.calculatePay(pc);
double deductions = affiliation.calculateDeductions(pc);
double netPay = grossPay - deductions;
pc.setGrossPay(grossPay);
pc.setDeductions(deductions);
pc.setNetPay(netPay);
this.paymentMethod.pay(pc);
}

public void setClassification(PaymentClassification classification) {
this.classification = classification;
}
public void setSchedule(PaymentSchedule schedule) {
this.schedule = schedule;
}
public void setPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void setAffiliation(Affiliation affiliation) {
this.affiliation = affiliation;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coderising.payroll;

public class HoldMethod implements PaymentMethod{
public void pay(Paycheck pc){
System.out.println("转到财务处:"+pc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.coderising.payroll;

import java.util.ArrayList;
import java.util.List;

public class HourlyClassification implements PaymentClassification{
private double hourlyRate;
private List<TimeCard> timeCards = new ArrayList<>();

public HourlyClassification(double hourlyRate, List<TimeCard> timeCards) {
super();
this.hourlyRate = hourlyRate;
this.timeCards = timeCards;
}

public double getHourlyRate() {
return hourlyRate;
}

public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}

public double calculatePay(Paycheck pc){
double salary = 0.0;
for(TimeCard tc:timeCards){
if(DateUtil.between(tc.getDate(), pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())){
salary += calculatePayForTimeCard(tc);
}
}
return salary;
}

private double calculatePayForTimeCard(TimeCard tc){
if(tc.getHours() > 8){
return 8*hourlyRate + (tc.getHours()-8)*1.5*hourlyRate;
}
return tc.getHours()*hourlyRate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.coderising.payroll;

public class MailMethod implements PaymentMethod{
private String address;
public void pay(Paycheck pc){
System.out.println("邮寄到:"+address+"。备注:"+pc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.coderising.payroll;

import java.util.Date;

public class MonthlySchedule implements PaymentSchedule{
public boolean isPayDate(Date date){
return DateUtil.isLastWorkDayOfMonth(date);
}

public Date getPayPeriodStartDate( Date payPeriodEndDate){
return DateUtil.getFirstDayOfMonth(payPeriodEndDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coderising.payroll;

public class NonAffiliation implements Affiliation{
public double calculateDeductions(Paycheck pc){
return 0.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.coderising.payroll;

import java.util.Date;

public class Paycheck {
private Date payPeriodStart;
private Date payPeriodEnd;
private double grossPay;
private double netPay;
private double deductions;

public Paycheck(Date payPeriodStart, Date payPeriodEnd){
this.payPeriodStart = payPeriodStart;
this.payPeriodEnd = payPeriodEnd;
}
public void setGrossPay(double grossPay) {
this.grossPay = grossPay;

}
public void setDeductions(double deductions) {
this.deductions = deductions;
}
public void setNetPay(double netPay){
this.netPay = netPay;
}
public Date getPayPeriodEndDate() {

return this.payPeriodEnd;
}
public Date getPayPeriodStartDate() {

return this.payPeriodStart;
}
@Override
public String toString() {
return "[开始时间=" + DateUtil.format(payPeriodStart) + ", 结束时间=" + DateUtil.format(payPeriodEnd) + ", 应发="
+ grossPay + ", 扣款=" + deductions + ", 实发=" + netPay + "]";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.payroll;

public interface PaymentClassification {
public double calculatePay(Paycheck pc);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.payroll;

public interface PaymentMethod {
public void pay(Paycheck pc);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.coderising.payroll;

import java.util.Date;

public interface PaymentSchedule {
public boolean isPayDate(Date date);
public Date getPayPeriodStartDate( Date payPeriodEndDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.coderising.payroll;

public class SalariedClassification implements PaymentClassification {
private double salary;

public SalariedClassification(double salary) {
super();
this.salary = salary;
}

public double calculatePay(Paycheck pc){
return salary;
}
}
Loading