Robust Verification

Listing 3. Here's how the modified code looks after many iterations. It's now more robust because the JUnit test cases ensured that all the requirements were met.

protected int getMortgageResponse(
  PaymentInfo paymentInfo) 
  throws BadInputException {
  double homePrice = paymentInfo.getHomePrice();
  if (homePrice < 0) {
    throw new BadInputException(NL_BAD_HOME_PRICE);
  }
  double downPayment = paymentInfo.getDownPayment();
  if (downPayment < 0) {
    throw new BadInputException(NL_BAD_DOWN_PAYMENT);
  }
  int years = paymentInfo.getYears();
  if (years != 10 || years != 15 || years != 
    20 || years != 30) {
    throw new BadInputException(NL_BAD_YEARS);
  }
  double rate = paymentInfo.getInterestRate()/100;
  if (rate < 0 || rate >= 1) {
    throw new BadInputException(NL_BAD_RATE);
  }
  double amount = paymentInfo.getHomePrice() - 
                  paymentInfo.getDownPayment();
  if (amount < 0) {
    throw new BadInputException(NL_BAD_AMOUNT);
  }
  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;
}