I have 2 classes in java to calculate the mortgage and monthly payment:
The first one is:
public class Loan {
private double mortgageAmount;
private double annualInterestRate;
private int amortizationPeriod;
//Setter and getter here
public Loan(double mortgageAmount, double annualInterestRate, int amortizationPeriod) {
super();
this.mortgageAmount = mortgageAmount;
this.annualInterestRate = annualInterestRate;
this.amortizationPeriod = amortizationPeriod;
getMonthlyPayment();
getLoanScheduleArray();
}
public double getMonthlyPayment() {
// TODO Auto-generated method stub
double monthlyPayment;
monthlyPayment = roundTo2Decimal(mortgageAmount * (Math.pow((1 + (annualInterestRate) / 200), (double) 1 / 6) - 1)
/ (1 - Math.pow(Math.pow((1 + (annualInterestRate) / 200), (double) 1 / 6), -12 * amortizationPeriod)));
return monthlyPayment;
}
public Loan() {
super();
}
public LoanSchedule[] getLoanScheduleArray() {
LoanSchedule[] loanScheduleArray = new LoanSchedule[amortizationPeriod * 12];
double remainingBalance = mortgageAmount;
for (int index = 0; index < (amortizationPeriod * 12); index++) {
int paymentNumber = index + 1;
if (paymentNumber < amortizationPeriod * 12) {
double monthlyPercentageRate = (Math.pow(1 + annualInterestRate / 200, (double) 1 / 6) - 1);
double interestPaid = roundTo2Decimal(remainingBalance * monthlyPercentageRate);
double principalPaid = roundTo2Decimal(getMonthlyPayment() - interestPaid);
remainingBalance = roundTo2Decimal(remainingBalance - principalPaid);
LoanSchedule loanSchedule = new LoanSchedule();
loanSchedule.setPaymentNumber(paymentNumber);
loanSchedule.setInterestPaid(interestPaid);
loanSchedule.setPrincipalPaid(principalPaid);
loanSchedule.setRemainingBalance(remainingBalance);
loanScheduleArray[index] = loanSchedule;
}
}
return loanScheduleArray;
}
}
And the 2nd one is
public class LoanSchedule {
//getter and setter here
public LoanSchedule(int paymentNumber, double interestPaid, double principalPaid, double remainingBlance) {
super();
}
public LoanSchedule() {
super();
}
}
Now I want to print out the interest paid, principal paid and remaining balance for every month, so I have these code in java servlet, but it doesn't print anything in the array. Where am I going wrong?
for (int index = 0; index < (intAmortizationPeriod * 12); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
response.getWriter().println(loanSchedule);
}
This is my servlet code:
public class LoanServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoanServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
String mortgageAmount = request.getParameter("mortgageAmount");
String annualInterestRate = request.getParameter("annualInterestRate");
String amortizationPeriod = request.getParameter("amortizationPeriod");
double dbMortgageAmount = Double.parseDouble(mortgageAmount);
double dbAnnualInterestRate = Double.parseDouble(annualInterestRate);
int intAmortizationPeriod = Integer.parseInt(amortizationPeriod);
int errorCount = 0;
StringBuffer errors = new StringBuffer("Please fix the following issue(s):");
errors.append("<ul class=\"bg-danger\">");
if (mortgageAmount.isEmpty()) {
errors.append("<li>Mortgage amount is required.</li>");
errorCount++;
}
if (annualInterestRate.isEmpty()) {
errors.append("<li>Annual interes trate is required.</li>");
errorCount++;
}
if (amortizationPeriod.isEmpty()) {
errors.append("<li>Amortization period is required.</li>");
errorCount++;
}
errors.append("</ul>");
if (errorCount == 0) {
//double taxableIncome = Double.parseDouble( taxableIncomeString );
Loan loanbean = new Loan(dbMortgageAmount, dbAnnualInterestRate, intAmortizationPeriod);
loanbean.getMonthlyPayment();
LoanSchedule[] loanScheduleArray = loanbean.getLoanScheduleArray();
String htmlResponseText = "Your monthly payment is <strong>" + loanbean.getMonthlyPayment() + "</strong>.";
response.getWriter().println(htmlResponseText);
for (int index = 0; index < (intAmortizationPeriod * 12); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
response.getWriter().println(loanSchedule.toString());
}
} else {
response.getWriter().println(errors.toString());
}
}
private void println(Object setPaymentNumber) {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}