This problem is composed of two parts.
1a. Enforce the precondition for the splitBil();
1b. Enforce the precondition for the calculateMonthlyTakehomePay();
Here is the code:
* @precondition: amount > 0 AND tipRate >= 0 AND numDiners >= 1
* @postcondition: none
*
* @param amount the amount of the bill before tip
* @param tipRate the tip rate to be applied to the bill, as a percentage (e.g., 20.0 for 20% tip, 15.0 for 15% tip, etc)
* @param numDiners the number of people splitting the bill
* @return the amount of money each diner must pay
*/
public double splitBill(double amount, double tipRate, int numDiners) {
double totalBill = amount * (1 + (tipRate / 100));
double fairShare = totalBill / numDiners;
return fairShare;
}
public double calculateMonthlyTakehomePay(double yearlySalary, double taxRate) {
double taxes = yearlySalary * taxRate;
double netPay = yearlySalary - taxes;
return netPay / 12;
}
Here are the tests for the splitbillmethod() code for 1A.:
public class TestSplitBill {
@Test
public void testShouldNotAllowZeroAmount() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(0, 10, 2);
});
}
@Test
public void testShouldNotAllowNegativeAmount() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(-1, 10, 2);
});
}
@Test
public void testShouldNotAllowNegativeTipRate() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, -1, 2);
});
}
@Test
public void testShouldNotAllowZeroDiners() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, 10, 0);
});
}
@Test
public void testShouldNotAllowNegativeDiners() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, 10, -1);
});
}
@Test
public void testShouldSplitBillWithOneDiner() {
FinancialCalculator theCalculator = new FinancialCalculator();
double actualBill = theCalculator.splitBill(100.00, 10, 1);
assertEquals(110.00, actualBill, 0.001);
}
@Test
public void testShouldSplitBillWithSeveralDiners() {
FinancialCalculator theCalculator = new FinancialCalculator();
double actualBill = theCalculator.splitBill(100.00, 10, 5);
assertEquals(22.00, actualBill, 0.001);
}
@Test
public void testShouldSplitBillWithZeroTip() {
FinancialCalculator theCalculator = new FinancialCalculator();
double actualBill = theCalculator.splitBill(100.00, 0, 2);
assertEquals(50.00, actualBill, 0.001);
}
@Test
public void testShouldSplitBillWith100PercentTip() {
FinancialCalculator theCalculator = new FinancialCalculator();
double actualBill = theCalculator.splitBill(100.00, 100, 2);
assertEquals(100.00, actualBill, 0.001);
}
}
For 1B here is the tests for the calculateMonthlyTakeHomePay method:
public class TestCalculateMonthlyTakehomePay {
@Test
public void testShouldNotAllowNegativeSalary() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(-1, 0.3);
});
}
@Test
public void testShouldNotAllowZeroSalary() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(0, 0.3);
});
}
@Test
public void testShouldNotAllowNegativeTaxRate() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(25000, -0.3);
});
}
@Test
public void testShouldNotAllowTaxRateOverOne() {
FinancialCalculator theCalculator = new FinancialCalculator();
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(25000, 1.3);
});
}
What I have tried:
I have tried to type in the if statements as asked in the precondition, but when I have tried the some of the j units failed and some of them passed.