Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need Help Generating Loan Repayment Schedule

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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?

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


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need Help Generating Loan Repayment Schedule

    Quote Originally Posted by C++kingKnowledge View Post
    ...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

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default 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:

    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
    Last edited by Zaphod_b; November 3rd, 2012 at 09:04 AM.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

Similar Threads

  1. How to schedule this to happen daily?
    By dynamix24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2012, 07:19 PM
  2. Timer.schedule
    By johnboyofsj in forum Java Theory & Questions
    Replies: 1
    Last Post: October 14th, 2012, 11:02 PM
  3. Loan Repayment
    By Langolier in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 18th, 2012, 01:29 PM
  4. Loan Payments
    By clarafeb1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 04:36 AM
  5. How to schedule a Java Program for Background processing
    By rangarajank in forum Java Theory & Questions
    Replies: 5
    Last Post: May 13th, 2010, 04:57 AM