Created
September 17, 2010 17:11
-
-
Save unclebob/584566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package payroll; | |
public class ContractorCalculator extends PayCalculator { | |
public ContractorCalculator() { | |
super(false); | |
} | |
public double calculate(double hours, double rate) { | |
if (hours < 0 || hours > 80) { | |
throw new RuntimeException("Hours out of range: " + hours); | |
} | |
if (isHourlyWorker) { | |
double overtime = Math.max(0, hours - 40); | |
return hours * rate + overtime * rate * 0.5; | |
} else { | |
return hours * rate; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package payroll; | |
public class HourlyCalculator extends PayCalculator { | |
public HourlyCalculator() { | |
super(true); | |
} | |
public double calculate(double hours, double rate) { | |
if (hours < 0 || hours > 80) { | |
throw new RuntimeException("Hours out of range: " + hours); | |
} | |
if (isHourlyWorker) { | |
double overtime = Math.max(0, hours - 40); | |
return hours * rate + overtime * rate * 0.5; | |
} else { | |
return hours * rate; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package payroll; | |
public abstract class PayCalculator { | |
protected boolean isHourlyWorker; | |
public PayCalculator(boolean hourlyWorker) { | |
isHourlyWorker = hourlyWorker; | |
} | |
public abstract double calculate(double hours, double rate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment