Welcome to Dream.In.Code
Click Here
Getting Java Help is Easy!

Join 118,875 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,827 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Amortization program help...

 
Reply to this topicStart new topic

Amortization program help...

confused.help
post 18 Jul, 2008 - 12:35 AM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 8

I'm trying to write this program to display an amortization schedule.... I am nearly finished, but my final principal is coming up negative, and I've tried everything I can think of to fix this but nothing is working... any ideas?? Thanks..
CODE

import java.util.Scanner;
import java.text.DecimalFormat;

public class Mortgage
{
      
    public static void main (String args[])
    {  
        Scanner input = new Scanner(System.in);
  
        int choice;
        double principal;
        double yearlyInterest;
        int paybackYears;
        double monthlyPmt;

        System.out.println("Select one of the following options");
        System.out.println();
        System.out.println("[1]   Monthly Loan Payment Computation");
        System.out.println("[2]   Amortization Schedule");
        System.out.println("[3]   Credit Card Pay-Off Schedule");
        System.out.println();
        System.out.print("Enter your choice  ===>>  ");
        choice = input.nextInt();
        System.out.println();

        switch (choice)
        {
            case 1:
                System.out.print("Enter loan amount       ===>>  ");
                principal = input.nextDouble();
                System.out.print("Enter annual percent    ===>>  ");
                yearlyInterest = input.nextDouble();
                System.out.print("Enter years to pay back ===>>  ");
                paybackYears = input.nextInt();
                Interest loan1 = new Interest(principal, yearlyInterest, paybackYears,0);
                loan1.computePayment();
                break;
            case 2:
                System.out.print("Enter principle balance  ===>>  ");
                principal = input.nextDouble();
                System.out.print("Enter annual percent     ===>>  ");
                yearlyInterest = input.nextDouble();
                System.out.print("Enter monthly payment    ===>>  ");
                monthlyPmt = input.nextDouble();
                Interest loan2 = new Interest(principal, yearlyInterest, 0, monthlyPmt);
                loan2.amortization();
                break;
                    }
        System.out.println();
    }
    
}



class Interest
{

    private double principal;
    private double percent;
    private int years;
    private double monthlyPmt;
    private int months;
    private int pmtNr;
    private double monthlyRate;
    private double interestPmt;
    private double principalPmt;
    private double totalPmt;
    private double totalInt;

    public Interest(double la, double yi, int py, double mp)
    // Do not alter anything in the constructor!
    {
        principal = la;
        percent = yi;
        years = py;
        monthlyPmt = mp;
        monthlyRate = percent/1200;
        months = years * 12;
        pmtNr = 0;
    }


    private double round(double x)
    {
        return (double) Math.round(100 * x) / 100;
    }
    
    
    public void computePayment()
    {
        System.out.println();
        System.out.println("Monthly Loan Payment Computation");
        System.out.println();
        
        DecimalFormat output = new DecimalFormat("##.0");
        monthlyPmt = (((monthlyRate)* Math.pow(1+monthlyRate,months))/(Math.pow(1+monthlyRate,months)-1))*principal;
        monthlyPmt = round(monthlyPmt);
        totalPmt =(months*monthlyPmt);
        totalInt = totalPmt - principal;
        
        System.out.println("Loan Amount:     " + principal);
        System.out.println("Yearly Interest: " + percent);
        System.out.println("Payback Years:   " + years);
        System.out.println("Monthly Payment: " + monthlyPmt);
        System.out.println("Total Payments:  " + output.format(totalPmt));
        System.out.println("Total Interest:  " + output.format(totalInt));
    }
//This is where I'm having trouble....
    public void amortization()
    {
        System.out.println();
        System.out.println("Amortization Schedule");
        System.out.println();
        System.out.println("Loan Amount:     " + principal);
        System.out.println("Yearly Interest: " + percent);
        System.out.println("Montly Payment:  " + monthlyPmt);
        double Loan = principal;
        

        do
        {
            interestPmt = round(principal*monthlyRate);
            principalPmt = round(monthlyPmt - interestPmt);
            principal = round(principal - principalPmt);
            
            
            pmtNr ++;
            

            System.out.println(pmtNr+"    \t"+monthlyPmt+"    \t"+interestPmt+"    \t"+principalPmt+"    \t"+principal);
        }
        while (principal > 0);
        
        totalPmt = pmtNr * monthlyPmt;
        totalInt = round(totalPmt - Loan);
    
    
        System.out.println("Payback Months:  " + pmtNr);  
        System.out.println("Total Payments:  " + totalPmt);
        System.out.println("Total Interest:  " + totalInt);
    }
User is offlineProfile CardPM

Go to the top of the page


confused.help
post 18 Jul, 2008 - 12:18 PM
Post #2


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 8

Ok, I modified the code a bit.... this is the improved amortization method....
CODE

public void amortization()
    {
        System.out.println();
        System.out.println("Amortization Schedule");
        System.out.println();
        System.out.println("Loan Amount:     " + principal);
        System.out.println("Yearly Interest: " + percent);
        System.out.println("Montly Payment:  " + monthlyPmt);
        double Loan = principal;
        

        do
        {
            interestPmt = round(principal*monthlyRate);
            principalPmt = round(monthlyPmt - interestPmt);
            principal = round(principal - principalPmt);
            pmtNr ++;
            
            if(monthlyPmt > principal)
            {
                  excess = principal;
                
            }
            

            System.out.println(pmtNr+"    \t"+monthlyPmt+"    \t"+interestPmt+"    \t"+principalPmt+"    \t"+principal);
        }
        while (principal > 0);
        
        totalPmt = pmtNr * (monthlyPmt)+ excess;
        totalInt = round(totalPmt - Loan);
    
    
        System.out.println("Payback Months:  " + pmtNr);  
        System.out.println("Total Payments:  " + totalPmt);
        System.out.println("Total Interest:  " + totalInt);
    }



My output is this if I enter 1000 as the loan amount, 13.5 as the yearly interest, and 250 as the monthly payment:
Select one of the following options

[1] Monthly Loan Payment Computation
[2] Amortization Schedule
[3] Credit Card Pay-Off Schedule

Enter your choice ===>> 2

Enter principle balance ===>> 1000
Enter annual percent ===>> 13.5
Enter monthly payment ===>> 250

Amortization Schedule

Loan Amount: 1000.0
Yearly Interest: 13.5
Montly Payment: 250.0
1 250.0 11.25 238.75 761.25
2 250.0 8.56 241.44 519.81
3 250.0 5.85 244.15 275.66
4 250.0 3.1 246.9 28.76
5 250.0 0.32 249.68 -220.92
Payback Months: 5
Total Payments: 1029.08
Total Interest: 29.08



I'm getting the correct final answers now, but the last output line of the amortization chart has a negative principal, and the last monthly payment should display 29.08... not 250.... help please? Thanks =)
User is offlineProfile CardPM

Go to the top of the page

pbl
post 18 Jul, 2008 - 03:48 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,339



Thanked 135 times

Dream Kudos: 75
My Contributions


Corrected your older version... the easiest way is to check when principal < 0 and correct the other amounts accordingly

CODE

       do
        {
            interestPmt = round(principal*monthlyRate);
            
            principalPmt = round(monthlyPmt - interestPmt);
            double lastPrincipal = principal;
            principal = round(principal - principalPmt);
            if(principal < 0) {
                principal = 0;
                principalPmt = lastPrincipal;
                monthlyPmt = interestPmt + principalPmt;
            }
            pmtNr ++;
            System.out.println(pmtNr+"    \t"+monthlyPmt+"    \t"+interestPmt+"    \t"+principalPmt+"    \t"+principal);
        }
        while (principal > 0);
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 02:18AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month