|
Modify for Success
Listing 2. Modify the getMortgageResponse() method until the JUnit test cases succeed. This is how the code looks when the JUnit test cases begin to pass. /*
rL(1 + r/12)^(12N)
P = -----------------------
12((1 + r/12)^(12N) - 1)
where:
r = interest rate (for 8.25% ==> r = 0.0825)
L = loan amount
N = loan time (years)
P = the monthly payment
*/
protected int getMortgageResponse(
PaymentInfo paymentInfo) {
double amount = paymentInfo.getHomePrice() -
paymentInfo.getDownPayment();
int years = paymentInfo.getYears();
double rate = paymentInfo.getInterestRate()/100;
double base = (double)(1 + rate/12);
double exponent = (double)(12 * years);
double result = java.lang.Math.pow(base, exponent);
double numerator = rate * amount * result;
double denominator = 12 * (result - 1);
int monthlyPayment = (int)(
numerator / denominator);
return monthlyPayment;
}
|