Created
September 17, 2010 16:59
-
-
Save unclebob/584547 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 PayCalculator { | |
private boolean isHourlyWorker; | |
public PayCalculator(boolean hourlyWorker) { | |
isHourlyWorker = hourlyWorker; | |
} | |
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
import org.junit.Test; | |
import payroll.PayCalculator; | |
import static org.junit.Assert.assertEquals; | |
public class PayCalculatorTest { | |
private PayCalculator hourlyCalculator = new PayCalculator(true); | |
private PayCalculator contractorCalculator = new PayCalculator(false); | |
private void assertPay(double expectedPay, double actualPay) { | |
assertEquals(expectedPay, actualPay, .001); | |
} | |
@Test | |
public void ZeroHours_ShouldBeZero() throws Exception { | |
assertPay(0.0, hourlyCalculator.calculate(0, 10)); | |
assertPay(0.0, contractorCalculator.calculate(0, 10)); | |
} | |
@Test(expected = RuntimeException.class) | |
public void LessThanZeroHours_ShouldThrow() throws Exception { | |
hourlyCalculator.calculate(-1, 10); | |
} | |
@Test(expected = RuntimeException.class) | |
public void MoreThan80Hours_ShouldThrow() throws Exception { | |
hourlyCalculator.calculate(81, 10); | |
} | |
@Test | |
public void FortyHours_StraightTime() throws Exception { | |
assertPay(400.0, hourlyCalculator.calculate(40, 10)); | |
assertPay(400.0, contractorCalculator.calculate(40, 10)); | |
} | |
@Test | |
public void FiftyHoursHourly_TimeAndHalf() throws Exception { | |
assertPay(550.0, hourlyCalculator.calculate(50, 10)); | |
} | |
@Test | |
public void FiftyHoursNonHourly_StraightTime() throws Exception { | |
assertPay(500.0, contractorCalculator.calculate(50, 10)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment