0

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);
    }

}
Riddhesh Sanghvi
  • 1,218
  • 1
  • 12
  • 22
GKra
  • 37
  • 10

3 Answers3

0

Why not run loanScheduleArray.size() amount of time (inside the for loop)?

Mado Baker
  • 109
  • 1
  • 2
  • 11
  • Actually, It printed something for me, but that's not the value in the array. It' look like "packagename.LoanSchedule@61f4e55b" – GKra Oct 02 '15 at 03:28
  • @GiangNguyen Use System.out.println(Arrays.deepToString(loanScheduleArray)); – Jude Niroshan Oct 02 '15 at 03:57
0

I just found out that I should have the code like this in my servlet class:

for (int index = 0;index < (intAmortizationPeriod * 12 ); index++) {
            LoanSchedule loanSchedule = loanScheduleArray[index];
            response.getWriter().println(loanSchedule.getPaymentNumber());
            response.getWriter().println(loanSchedule.getInterestPaid());
            response.getWriter().println(loanSchedule.getPrincipalPaid());
            response.getWriter().println(loanSchedule.getRemainingBalance());
        }

But the result is displayed too bad. How can I arrange it in to the table for better look??

GKra
  • 37
  • 10
  • Start learning JSP. It's much more suited for dynamically generating HTML output than a servlet. – BalusC Oct 02 '15 at 10:46
0

Add a toString method to LoanSchedule

public String toString() {
   return "paymentNumber=" + paymentNumber + ","
                + " interestPaid=" 
                + interestPaid + ", principalPaid=" + 
                principalPaid+ ", remainingBlance="
                + remainingBlance;
    }

and update the code in servlet as below

    StringBuffer sb = new StringBuffer("<table>");
    for (int index = 0; index < (intAmortizationPeriod * 12); index++) {

        LoanSchedule loanSchedule = loanScheduleArray[index];
        sb.append("<tr><td>");
        sb.append(loanSchedule.toString());
        sb.append("</td></tr>");
    }
    sb.append("</table>");
    response.getWriter().println(sb.toString());

You can still format the toString method as needed
Rai
  • 126
  • 5
  • Thanks for your help. But if I print the array like this: for (int index = 0;index < (intAmortizationPeriod * 12 ); index++) { LoanSchedule loanSchedule = loanScheduleArray[index]; response.getWriter().println(loanSchedule.getPaymentNumber()); response.getWriter().println(loanSchedule.getInterestPaid()); response.getWriter().println(loanSchedule.getPrincipalPaid()); response.getWriter().println(loanSchedule.getRemainingBalance()); } Is that anyway to put those result into the html table?? – GKra Oct 02 '15 at 04:57
  • Its better to build a string containing the html text instead of using the response.getWriter().println() multiple times. So in this loop try building the html text and then finally pass it to response.getWriter().println() – Rai Oct 02 '15 at 16:54