Skip to content

Instantly share code, notes, and snippets.

@brianjgamble
Created September 17, 2010 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianjgamble/585100 to your computer and use it in GitHub Desktop.
Save brianjgamble/585100 to your computer and use it in GitHub Desktop.
package payroll;
public class ContractorCalculator extends PayCalculator {
@Override
protected double determinePay(double hours, double rate) {
return hours * rate;
}
}
package payroll;
public class HourlyCalculator extends PayCalculator {
@Override
protected double determinePay(double hours, double rate) {
double overtime = Math.max(0, hours - 40);
return hours * rate + overtime * rate * 0.5;
}
}
package payroll;
public abstract class PayCalculator {
public final double calculate(double hours, double rate) {
validateHours(hours);
return determinePay(hours, rate);
}
protected void validateHours(double hours) {
if (hours < 0 || hours > 80) {
throw new RuntimeException("Hours out of range: " + hours);
}
}
protected abstract double determinePay(double hours, double rate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment