Need Help Generating Loan Repayment Schedule
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?
Code Java:
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
Code Java:
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();
}
}
Re: Need Help Generating Loan Repayment Schedule
Quote:
Originally Posted by
C++kingKnowledge
...displays the payment for the first month and keeps reprinting those values......
Why would you expect that the numbers would change? You never decrease the loan amount or unpaid balance, so the same values print every time.
Another note: Why the do you print 12 payments? What if the loan were for, say, monthly payments for 60 Months? How about 360 months?
Cheers!
Z
Re: Need Help Generating Loan Repayment Schedule
thanks for the enlightenment i should change that and not make it a constant.... i thought i had to change the unpaid balance but how would i set the new unpaid balance every time without resetting my original loanAmount??? i tried to call the setAmount in the unpaidBalance method but that messed my results up so where should i get started on doing that?
Re: Need Help Generating Loan Repayment Schedule
Step away from the compiler for a moment. Work with formulas on pencil and paper.
Suppose you have 1000 at 12% APR for 1 year (12 monthly payments)
Monthly payment is 88.85, right?
Then the amortization schedule looks something like this:
Code :
Monthly Payment: 88.85
Payment No. Interest Principal Unpaid Balance Interest to Date
1 10.00 78.85 921.15 10.00
2 9.21 79.64 841.51 19.21
3 8.42 80.43 761.08 27.63
.
.
.
11 1.75 87.10 87.97 65.31
12 0.88 87.97 00.00 66.19
(In the interest of full disclosure, I will admit that I just did the first two lines 'by hand' with a calculator. The others I got from a loan calculator on the web, and printed out values that your program should print. The point is that if the output of your program doesn't start and end like this, then there's more work to be done. I always make sure I have a test case with known output before I start writing a program to do the deed.)
Monthly payment doesn't change. It is calculated according to the initial balance and the APR and the number of payments. (By the way: Why didn't you initialize the balance in the constructor when you initialized the loan amount?) You recalculate the monthly payment every time you use it. Why? Why not make it a field of the class?
Anyhow:
Each month, the monthly interest amount is equal to current balance times monthly interest rate
Each month, the monthly principal amount is equal to monthly payment minus the monthly interest amount
Each month, balance is decreased by the amount of the monthly principal amount.
The question is: How is the balance decreased each month? In your unpaidBalance() method you calculate balance = loanAmount - principalPayment(), right? Why? loanAmount doesn't change. Shouldn't you be adjusting the balance by subtracting the principal payment from the previous balance?
Personally, I'm not too fond of using a method named unpaidBalance to adjust the balance. I would probably just have it return the balance and, perhaps make a separate method (makePayment() or some such thing) to adjust balance by subtracting current principal amount.) But the point is that calculations of interest are made on current balance, not original loan amount.
Cheers!
Z
Re: Need Help Generating Loan Repayment Schedule
thanks for the help Z i finally got it working correctly!!! initializing the balance in the constructor helped alot... i changed unpaidBalance to void and made a get method to return the balance instead and made a couple of other changes as well.... all i needed was a little insight 'preciate it