Mortgage Calculator

I had this question before but I am stuck on how to get it to output correctly.  This is my assignment:
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan.

Here is my code:

/*Joshua Gutierrez
      PRG 420
      Week 4
      Individual Assignment
      3/26/3011
*/


import java.io.IOException;

public class GutierrezMortgages {

      //Method to Calculate Mortgage using the principal amount, the interest, and the term

    static int MAX_LINECOUNT = 200;


           static  double[] loanTerm = {360.0,180.0,84.0}; //*30 years, 15 years, 7 years
           static  double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
           static  int loanAmount = 200000; //*$200,000 Loan

    // float myCalculatedMortgage;
       float myCalculatedMortgageTwo;
       float myCalculatedMortgageThree;


      public double calculateMortgage (int loanAmount, double loanInterest, double loanTerm) {

            double myMortgage = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));
          double myMortgageTwo = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));      
            double myMortgageThree = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));
            return myMortgage;


      }

      //Method to display the calculated mortgage

      public void displayTheMortgage(double myCalculatedMortgage)  {

 

        System.out.println();

        System.out.println("\t\tLoan Amount: $200,000.00");

        System.out.println("\t\tTerm: 7 Years");

        System.out.println("\t\tInterest Rate 5.35%");

        System.out.printf("\t\tThe monthly payment amount is $%.2f", +myCalculatedMortgage);

        System.out.println();

        System.out.println("\t\tTerm: 15 Years");

        System.out.println("\t\tInterest Rate 5.5%");

        System.out.printf("\t\tThe monthly payment amount is $%.2f", +myCalculatedMortgageTwo);

        System.out.println();

        System.out.println("\t\tTerm: 30 Years");

        System.out.println("\t\tInterest Rate 5.75%");

        System.out.printf("\t\tThe monthly payment amount is $%.2f", +myCalculatedMortgageThree);

        System.out.println();

       
      }

     


      //Delcare main () method created instance//

      public static void main(String[] args){

            // Method Variables

            // double loanTerm[] = {360.0,180.0,84.0}; //*30 years, 15 years, 7 years
            // double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
            // int loanAmount = 200000; //*$200,000 Loan
           
             double myCalculatedMortgage, myCalculatedMortgageTwo, myCalculatedMortgageThree; //*Three Payment Amounts


            //Array for Loan Term
               int[] aloanTerm;
               aloanTerm = new int[3];

               aloanTerm[0] = 360;
 
               aloanTerm[1] = 180;
               aloanTerm[2] = 84;

            //Array for Interest
            // double loanInterest[];
               loanInterest = new double[3];

               loanInterest[0] = .0575;
               loanInterest[1] = .0550;
               loanInterest[2] = .0535;


                 // Create an instance of the class

                       GutierrezMortgages myInstance = new GutierrezMortgages ();

                // Use my the class instance and the dot notation to call the method that calculates the mortgage
                       
                        int index = Integer.parseInt(args[0]);

                        double myMortgage = myInstance.calculateMortgage (loanAmount, loanInterest[index-1], loanTerm[index-1]);

                // Display the calculated mortgage

                         myInstance.displayTheMortgage(myMortgage);


            //Display Mortgage Given Array Variables
            for(int x = 0; x < loanInterest.length; x++) {
                  System.out.println(aloanTerm[x] + " year at " + loanInterest[x] + "%:");
                  myMortgage = (loanAmount*loanInterest[index-1]/12)/(1-Math.pow(1+loanInterest[index-1]/12, -loanTerm[index-1]));
                  System.out.println(myMortgage);




            //Conditional Statement To Pause and Continue
                int linecount = 0;

            if (linecount == MAX_LINECOUNT) {
                  linecount = 1;

                  try {
                        System.in.read();
                  } catch (IOException e) {
                              e.printStackTrace();
                  }

            }
            else {
                  linecount++;
            }

      }


      }
}

My Output is rather funky and I am stuck on how to display it neatly:

C:\Users\Joshua\Documents\UoPhoenix\PRG 420\Week 5>java GutierrezMortgages 1

                Loan Amount: $200,000.00
                Term: 7 Years
                Interest Rate 5.35%
                The monthly payment amount is $1167.15
                Term: 15 Years
                Interest Rate 5.5%
                The monthly payment amount is $0.00
                Term: 30 Years
                Interest Rate 5.75%
                The monthly payment amount is $0.00
360 year at 0.0575%:
1167.1457128870984
180 year at 0.055%:
1167.1457128870984
84 year at 0.0535%:
1167.1457128870984


PapaDragonXAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

What is it in particular that you are talking about - do you mean it is format of numbers 1167.1457128870984
with many decimals that you don't like, or you are talking abouyrt numbers themsaelves?
0
for_yanCommented:
change these to lines:

int index = Integer.parseInt(args[0]);
   double myMortgage = myInstance.calculateMortgage (loanAmount, loanInterest[index-1], loanTerm[index-1]);

to the following three lines:

 double myMortgage = myInstance.calculateMortgage (loanAmount, loanInterest[0], loanTerm[0]);
double myMortgageOne = myInstance.calculateMortgage (loanAmount, loanInterest[1], loanTerm[1]);
double myMortgageTwo = myInstance.calculateMortgage (loanAmount, loanInterest[2], loanTerm[2]);

and then you''ll not need any command line arguments.


0
PapaDragonXAuthor Commented:
Numbers themselves. I would like the output to display the payment, interest, term organized instead of jumbled.
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

for_yanCommented:


Change these lines:


   for(int x = 0; x < loanInterest.length; x++) {
                  System.out.println(aloanTerm[x] + " year at " + loanInterest[x] + "%:");
                  myMortgage = (loanAmount*loanInterest[index-1]/12)/(1-Math.pow(1+loanInterest[index-1]/12, -loanTerm[index-1]));
                  System.out.println(myMortgage);


to the following:

NumberFormat numForm = NumberFormat.getCurrencyInstance();
for(int x = 0; x < loanInterest.length; x++) {
                  System.out.println(aloanTerm[x] + " year at " + loanInterest[x] + "%:");
                  myMortgage = (loanAmount*loanInterest[index-1]/12)/(1-Math.pow(1+loanInterest[index-1]/12, -loanTerm[index-1]));
                  System.out.println(numForm.format(myMortgage));



0
PapaDragonXAuthor Commented:
Will change some coding and will get back with you on the results. Thanks!
0
for_yanCommented:
Still you need to explain do you want printed for many different variants or for one of them - that's
waht I mean by index - to select variant. If not
you want to print all of them and you don't need to read index
0
dougaugCommented:
See if the attached code works for you.

It produces the following output:

            Loan Amount: $200000,00
            Term: 30 Years            Interest Rate: 5,75%            The monthly payment amount is $1167,15
            Term: 15 Years            Interest Rate: 5,50%            The monthly payment amount is $1634,17
            Term: 7 Years            Interest Rate: 5,35%            The monthly payment amount is $2859,79

import java.io.IOException;

public class GutierrezMortgages {

    //Method to Calculate Mortgage using the principal amount, the interest, and the term
    static  int[] loanTerm = {360,180,84}; //*30 years, 15 years, 7 years
    static  double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
    static  double loanAmount = 200000; //*$200,000 Loan

    public static double calculateMortgage (double loanAmount, double loanInterest, int loanTerm) {
       double myMortgage = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));
       return myMortgage;
    }

    //Method to display the calculated mortgage
    public static void displayTheMortgages()  {
       System.out.println();
       System.out.printf("\t\tLoan Amount: $%.2f\n", loanAmount);
       
       for (int i = 0; i < loanTerm.length; i++) {
    	   System.out.printf("\t\tTerm: %d Years", loanTerm [i] / 12);
           System.out.printf("\t\tInterest Rate: %.2f%%", loanInterest[i] * 100);
           System.out.printf("\t\tThe monthly payment amount is $%.2f", calculateMortgage (loanAmount, loanInterest[i], loanTerm[i]));
           System.out.println();
       }
    }

    public static void main(String[] args){
       displayTheMortgages();
    }
}

Open in new window

0
for_yanCommented:
If it is about many different variants - then each one should be calculated in the code:
I added index argument because I saw that you in fact calculate only one variant.
If you want to calculate for all cases it will also make
sense to make array out of myMortgage, myMortGagaOn2, myMortgageTwo - why not to have double myMortGage[3];
- it will simplify code
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PapaDragonXAuthor Commented:
Thanks for your help.  I wanted it to display the different variants all the calculations so what you mentioned is how I want it.  I guess I am still trying to figure indexes and arrays and it is confusing to get them to all work together when you have a calculation.
0
for_yanCommented:
Yes, this works for me:


	Loan Amount: $200000.00
		Term: 30 Years		Interest Rate: 5.75%		The monthly payment amount is $1167.15
		Term: 15 Years		Interest Rate: 5.50%		The monthly payment amount is $1634.17
		Term: 7 Years		Interest Rate: 5.35%		The monthly payment amount is $2859.79

Open in new window

0
PapaDragonXAuthor Commented:
Thanks again for your help!  Java is very confusing to me but when it is explained with patience involved, I benefit big time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.