开发者

Need help Java amortization table

I need help to iterate the calculations and list for months in a loop.

I don't know how to list the loan balance with updated information. This code lists items in each place but each number is the same as the last. The first months calculations are displayed for the entire 360 months.

Write the program in Java (without a graphical user interface) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan. If the list would scroll off the screen, use loops to display a partial list, hesitate, and then display more of the list.

/Declare all Variables for Week 3/

     double anualInterest = .0575;
     double interestCompoundedMonthly = 0;
     double interestForPeriod = 0;
     double principalAtEndOfPeriod = 200000.00;
     double portionToPrincipal = 0;
     double amountOfPaymentMonthly = 1167.15;
     double newPrincipalAtEndOfPeriod = 0;

/Calculate Payments Week 3/

        interestCompoundedMonthly = (anualInterest/12); //.0575/12=.0047916

        interestForPeriod = interestCompoundedMonthly * principalAtEndOfPeriod; // 958.32 =.0049916*200,000

        portionToPrincipal = amountOfPaymentMonthly - interestForPeriod; // 208.83 = 1167.15-958.32

        newPrincipalAtEndOfPeriod = principalAtEndOfPeriod - portionToPrincipal; //199791.18 = 200000-208.83

        System.out.println (i+ "\t\t" + dcm.format(monthlyPayment)+"\t\t" +dcm.format开发者_如何转开发(interestForPeriod)+"\t\t\t"+dcm.format(portionToPrincipal)+ "\t\t\t" +dcm.format(newPrincipalAtEndOfPeriod));  

Thanks in advance for any advice.

/****************
 * Week 2       *
 ****************/
   /*Monthly Payment Program
     A program written in Java (without a graphical user interface)
     that will calculate and display the monthly payment amount
     to fully amortize a $200,000.00 loan 
     over a 30 year term at 5.75‰ interest.*/

/****************
 * Week 3       *
 ****************/

  /* Write the program in Java (without a graphical user interface)
  using a loan amount of $200,000 with an interest rate of 5.75% 
  and a 30 year term. Display the mortgage payment amount and then
  list the loan balance and interest paid for each payment over 
  the term of the loan. If the list would scroll off the screen, 
  use loops to display a partial list, hesitate, 
  and then display more of the list.*/

 import java.io.IOException;      //Code that delays ending the program


  public class Monthly_Payment_Calculator {
  public static void main (String [] args)  {



  /*Declare all Variables Week 2*/

    /*Variables provided by customer*/

     double loanAmount = 200000.00;   // $ amount borrowed
     double interestRate = 5.75;     // interest rate 5.75%
     int    years = 30;             // years of loan

    /*Variables needed for calculating*/ 

     int    months = 0;            // months for calculating
     double monthlyPayment = 0;   // monthly payment for calculating
     double interest = 0;        // interest rate for calculating

    /*Declare all Variables for Week 3*/

     double anualInterest = .0575;
     double interestCompoundedMonthly = 0;
     double interestForPeriod = 0;
     double principalAtEndOfPeriod = 200000.00;
     double portionToPrincipal = 0;
     double amountOfPaymentMonthly = 1167.15;
     double newPrincipalAtEndOfPeriod = 0;

     /*Variables for storing previous balances*/       




     java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");    
                                // format for currency


  /*Calculate Payment Week 2*/

     interest = interestRate / 100;

     months = years * 12;

     monthlyPayment = (loanAmount * (interest/12))/(1 - 1 /Math.pow((1 + interest/12), months));    


   /*Display the mortgage payment amount as per WK3 assignment*/

     System.out.println ("Total Monthly Payment is ");        
     System.out.println (dcm.format(monthlyPayment));           


  /*Display columns*/
     System.out.println("Month #\t Amount of Payment\tInterest for Period\tPortion to Principal\tPrincipal at End of Period\n");
     System.out.println("0\t\t\t0\t\t0\t\t\t0\t\t\t"+ dcm.format(principalAtEndOfPeriod));
     //Prints headers for columns


  /*Loop to calculate and print monthly payments*/

     for(int i=1; i <= months; i++)    // 360 months
     { 

     /*Calculate Payments Week 3*/


        interestCompoundedMonthly = (anualInterest/12); //.0575/12=.0047916

        interestForPeriod = interestCompoundedMonthly * principalAtEndOfPeriod; // 958.32 =.0049916*200,000

        portionToPrincipal = amountOfPaymentMonthly - interestForPeriod; // 208.83 = 1167.15-958.32

        newPrincipalAtEndOfPeriod = principalAtEndOfPeriod - portionToPrincipal; //199791.18 = 200000-208.83

        System.out.println (i+ "\t\t" + dcm.format(monthlyPayment)+"\t\t" +dcm.format(interestForPeriod)+"\t\t\t"+dcm.format(portionToPrincipal)+ "\t\t\t" +dcm.format(newPrincipalAtEndOfPeriod));  


     //recalculate interest for period
     //recalculate the  portion to principal
     //recalculate principal at end of period
     //set the remaining balance as the mortgage loan for the next repetition



        if(i%12==0 && i<months){


          /*Code to delay ending the program*/

           System.out.println( );
           System.out.println( );
           System.out.println ("(Please Press Enter to Continue the List)");
           System.out.println( );
           System.out.println( );
           System.out.println("Month #\t Amount of Payment\tInterest for Period\tPortion to Principal\tPrincipal at End of Period\n");
           try {
              System.in.read();  //Read input from the keyboard

           } 
              catch (IOException e) {  //Catch the input exception
                 return;  //and just return

              }
        }
     }
  }

}


You need to reset your principal balance at the end of the payment calculation. Try adding:

prindipalAtEndOfPeriod = newPrincipalAtEndOfPeriod;

I know it's been a while but maybe someone else will find this helpful.


i'm not sure how your interest works but i'm guessing that you are trying to compute a summation. perhaps you can try the following in your 'for' loop:

// a is the cummulative sum
// b is the monthly calculation
a = a + b;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜