evak77 0 Newbie Poster

I cannot seem to get the array to work and display the second and third loans, only the first one displays. Here is what I need to clarify the situation. Can someone help me out please.

Modify the mortgage program again. Remove the amortization table. This time enter 3 different loan terms for the same amount financed. Use an array(s) to store the loan terms. Then print out a comparison of the payment amount, the total interest paid over the course of the loan, and total of payments. Use a loop to insert and extract data from your arrays. Be sure that the columns are aligned with proper headings and the numbers are properly formatted. Insert appropriate comments into the program. The output would be something like this:

Loan 1 15,000 12% 60 $333.67 $5,020.20 $20,020.20
Loan 2 15,000 10% 48 $xxx.xx $x.xxx $xx,xxx.xx
Loan 3 15,000 8% 36 $xxx.xx $x,xxx $xx,xxx.xx

/ MortgageCalculatorProgram.java
// program to calculate mortgage
// Erik Voigt
// 05/16/10


import java.text.DecimalFormat;
import java.io.*; 
import java.util.Arrays;


public class MortgageCalculatorProgram
{
	public static void main(String[] arguments)
    {

  // define variables, payments, principle, interest, rate, and loan information

        double monthlyPayment;
		double principal;
		double interestRateYears;
		double interestRateMonths;
		int termYears;
		int termMonths;
		int linecount;

// will display balance, interest paid, principle, interest payments

		double balance;
		double interestPaid;
		double principalPaid;
		double monthlyInterestPayment;
		double monthlyPrincipalPayment;


// assign values to each item

        principal = 15000;
       	interestRateYears = .012;
       	interestRateMonths = (interestRateYears / 12) / 100;
        termYears = 3;
        termMonths = (termYears * 12);
        monthlyInterestPayment = 0;
        monthlyPrincipalPayment = 0;
        balance = principal;

// display 10 lines of results at one time

        linecount = 10;

// will only display two decimal places out

        java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");

// hard coded information per assignment

        System.out.println("\n\n\t*** Mortgage Calculator ***\n\n" +
        				"\nLoan Amount: \t$" + dec.format(principal) +
       					"\nInterest Rate: \t" + interestRateYears +"%" +
        				"\nTerm (Years): \t" + termYears);

// will calculate monthly mortgage payment

       	monthlyPayment = (principal * interestRateMonths) /
                       	(1 - Math.pow(1 + interestRateMonths, - termMonths));

// Print the information

      	System.out.println("\n\nBased on the information, "  +
                        "the monthly payment amount will be: " +
                        "$" + dec.format(monthlyPayment));

// calculation

        monthlyInterestPayment = (balance * interestRateMonths);
        monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);

// provides columns

        System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
        System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
        System.out.println("---------  \t---------  \t---------  \t---------");

// start the while loop
       while (termMonths > 0)

{

// information to be displayed

       System.out.println(termMonths + "\t\t$" + dec.format(monthlyPrincipalPayment) +
                         "\t\t$" + dec.format(monthlyInterestPayment) +
                         "\t\t$" + dec.format(balance - monthlyPrincipalPayment));

// decrement of  months
       termMonths--;

// calculate interest and principal payments on the loan

       monthlyInterestPayment = (balance * interestRateMonths);
       monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
       balance = (balance - monthlyPrincipalPayment);

// this will cause the results to pause
        if(linecount == 10)
		{

        linecount = 0;
        try
		{
        Thread.sleep(2000);

        }
        catch (InterruptedException e)
        {
          }
        }
// end if
        else
        {
        linecount++;
        }
// end else
        }
// end while
        }
// end main
}
	
class MortgageArray1

{
   
	public static void main(String[] arguments)

{


	double amount = 15000;
	int[]term = {36, 48, 60};
	double[] rate = {.012, .010, .008};
	DecimalFormat twoDigits = new DecimalFormat("$000.00");

	System.out.println("With the loan amount of  " +twoDigits.format
	(amount));

	for (int i = 0; i < rate.length; i++)
	{

	System.out.print("for " + term[i] + " years");
	System.out.print("\tat a rate of " + rate[i]);
	double payment = (amount*(rate[i]/12))/(1-(Math.pow(1/(1+(rate
	[i]/12)),(term[i]*12))));

	System.out.println("\tThe monthly payment will be " +
	twoDigits.format(payment));

	}
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.