I'm supposed to get three inputs (loan amount, annual interest rate & loan period in years to repay loan) from the user and display a recommended repayment table based on the number of payments needed to pay back the loan... when i run the program it accepts the values but it only displays the payment for the first month and keeps reprinting those values.... is it a problem with my variables and data members or is something wrong with my methods?
class LoanInfo { private static final int MONTHS_IN_YEAR = 12; private double loanAmount; private double monthlyInterest; private double principal; private double balance; private int numberOfPayments; public LoanInfo(double amount, double rate, int period){ setAmount(amount); setRate(rate); setPeriod(period); } public double getAmount(){ return loanAmount; } public int getPeriod(){ return numberOfPayments / MONTHS_IN_YEAR; } public double getRate(){ return monthlyInterest * MONTHS_IN_YEAR * 100; } public double monthlyInterestPayment(){ double interestPayment; interestPayment = monthlyInterest * loanAmount; return interestPayment; } public double monthlyPayment(){ double monthlyPayment; monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow(1/(1+monthlyInterest), numberOfPayments)); return monthlyPayment; } public double principalPayment(){ principal = monthlyPayment() - monthlyInterestPayment(); return principal; } public double unpaidBalance(){ balance = loanAmount - principalPayment(); return balance; } public void setAmount(double amount){ loanAmount = amount; } public void setPeriod(int periodInYears){ numberOfPayments = periodInYears * MONTHS_IN_YEAR; } public void setRate(double annualRate){ monthlyInterest = annualRate / 100.0 / MONTHS_IN_YEAR; } }
Assume a LoanInfo object is created in the main... this is the code to display the table
public void displayOutput(){ String[] title = {"Payment No.", "Interest", "Principal", "Unpaid Balance", "Total Interest to Date"}; DecimalFormat df = new DecimalFormat("0.00"); double sum = 0.0; for(int i = 0; i < title.length; i++){ System.out.print(title[i] + " "); } System.out.println(); for(int j = 1; j < 13; j++){ System.out.format("%5d ", j); System.out.print("\t" + df.format(loan.monthlyInterestPayment())); System.out.print("\t " + df.format(loan.principalPayment())); System.out.print("\t " + df.format(loan.unpaidBalance())); sum += loan.monthlyInterestPayment(); System.out.print("\t " + df.format(sum)); System.out.println(); } }