Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




Java Mortgage Calc help needed with loops

 
Reply to this topicStart new topic

Java Mortgage Calc help needed with loops, Need to list monthly interest payment totals

rastymasher
21 Apr, 2007 - 07:58 AM
Post #1

New D.I.C Head
*

Joined: 20 Apr, 2007
Posts: 3


My Contributions
I've been hammering away at this every free moment of my last week or two (I can't remember) and the project is due tonight. So I hope someone can provide some guidance soon. My mortgage calculator needs to list the total of interest payments for each month and I'm not sure how to proceed.

I need to somehow recalulate my principal and interest payments each month, replacing the original balance with the new balance in the formula. It must also pause occasionally during the listing to give the user a chance to look at the results. This is a non-GUI project as I am not yet that experienced in Java.

I stress that I am not looking for the answer, but some guidance as to what techniques to use, where to start looking for resources and perhaps an idea or two of how to apply them. Trust me, I will be sitting in front of this computer all day long trying to hammer it out myself until I have something that I can turn in.

Thanks, here is my code as it exists so far

CODE

/*

    Title:         Mortgage2.java
    Writen by: Jim Blum
    Date:         4/15/2007
    Version:    2.0

    Description: Modify the mortgage program to display the mortgage payment amount.
    Then, list the loan balance and interest paid for each payment over
    the term of the loan.  The list would scroll off the screen, but use
    loops to display a partial list, hesitate, and then display more of
    the list.  Do not use a graphical user interface.
    Insert comments in the program to document the program.

*/

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

public class Mortgage2
{
    //Format Currency
    static DecimalFormat currency = new DecimalFormat("$#,000.00");

    //Declare Variables
    static int iTerm = 360; //hardcoded mortage term in months
    static double dBalance = 200000; //hardcoded mortage principal
    static double dAPR = 5.75; //hardcoded Annual Percentage Rate (APR)
    static double dMonthlyInterest = getMonthlyInterest();//Monthly interest rate
    static double dPayment = getPayment();//Monthly payment
    static double newBalance = getNewBalance();//Monthly balance after principal payment applied
    static double dPrincipalPymnt = dBalance - newBalance; //Amount applied monthly to principal
    static double dInterestPymnt = dPayment - dPrincipalPymnt; //Amount applied monthly to interest

    //Date Constructor
    static Date currentDate = new Date();

    public static void main (String[] args) throws IOException
    {

        // Program Header
        System.out.println();
        System.out.println("\t\t\tMortage Calculator");
        System.out.println("\t\t\tBy Jim Blum");
        System.out.println("\t\t\tJava Programming");
        System.out.println("\t\t\t" + currentDate);
        System.out.println();

        //Output Total Payment
        System.out.println("The monthly payment on $200,000.00 at 5.75% APR is "+ currency.format(dPayment));
        System.out.println();
        System.out.println("New Balance "+ currency.format(newBalance) + "\t\tPrincipal = "+ currency.format(dPrincipalPymnt) +"\tInterest = "+ currency.format(dInterestPymnt));
        System.out.println();
        System.out.println();

    } //end of main

        public static double getMonthlyInterest()//calculates monthly interest based on APR

        {

            double dMonthlyInterest = dAPR / (12 * 100);
            return dMonthlyInterest;

        }//end of dMonthlyInterest method

        public static double getPayment()//calculates monthly mortgage payment

        {

            double dPayment = (dBalance * getMonthlyInterest()) / (1 - Math.pow(1 + getMonthlyInterest(), - iTerm));
            return dPayment;

        }//end of getPayment method

        public static double getNewBalance()//calculates new mortgage balance

        {

            double newBalance = dBalance * (1 + dMonthlyInterest) - dPayment;
            return newBalance;

        }//end of getNewBalance method

} //end of class

User is offlineProfile CardPM
+Quote Post

rastymasher
RE: Java Mortgage Calc Help Needed With Loops
21 Apr, 2007 - 02:50 PM
Post #2

New D.I.C Head
*

Joined: 20 Apr, 2007
Posts: 3


My Contributions
OK, so I guess here is where I try to prove that I have shot my Saturday trying to get this to work. Since this morning I have decided to try using a do while statement to make my loop happen but I'm still not sure I am heading in the right direction.

I've left a bunch of stuff at the bottom that I have commented out just so that I do have will compile and I can hopefully demonstrate that I have not been squating on my beanbag all day.

I'm about to have an anuerism over this so it has now becoming a matter of life and death.

I hope someone can offer some suggestions PDQ otherwise I am pooched.

Thanks

CODE

/*

    Title:         Mortgage2.java
    Writen by: Jim Blum
    Date:         4/15/2007
    Version:    2.0

    Description: Modify the mortgage program to display the mortgage payment amount.
    Then, list the loan balance and interest paid for each payment over
    the term of the loan.  The list would scroll off the screen, but use
    loops to display a partial list, hesitate, and then display more of
    the list.  Do not use a graphical user interface.
    Insert comments in the program to document the program.

*/

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

public class Mortgage2
{
    //Format Currency
    static DecimalFormat currency = new DecimalFormat("$#,000.00");

    //Declare Variables
    static int iTerm = 360; //hardcoded mortage term in months
    static double dBalance = 200000; //hardcoded mortage principal
    static double dAPR = 5.75; //hardcoded Annual Percentage Rate (APR)
    static double dMonthlyInterest = getMonthlyInterest();//Monthly interest rate
    static double dPayment = getPayment();//Monthly payment
    static double dNewBalance = getNewBalance();//Monthly balance after principal payment applied
    static double dPrincipalPymnt = dBalance - dNewBalance; //Amount applied monthly to principal
    static double dInterestPymnt = dPayment - dPrincipalPymnt; //Amount applied monthly to interest
    static double dMonthlyBalance = dNewBalance * (1 + dMonthlyInterest) - dPayment;
    static double dNewPrincipalPymnt = dNewBalance - dMonthlyBalance;;
    static double dNewInterestPymnt = dPayment - dNewPrincipalPymnt;

    //Date Constructor
    static Date currentDate = new Date();

    public static void main (String[] args) throws IOException
    {

        //program Header
        System.out.println();
        System.out.println("\t\t\tMortage Calculator");
        System.out.println("\t\t\tBy Jim Blum");
        System.out.println("\t\t\tJava Programming");
        System.out.println("\t\t\t"+ currentDate);
        System.out.println();

        //output Total Payment
        System.out.println("The monthly payment on $200,000.00 at 5.75% APR is "+ currency.format(dPayment));
        System.out.println();
        System.out.println("New Balance "+ currency.format(dNewBalance) + "\t\tPrincipal = "+ currency.format(dPrincipalPymnt) +"\tInterest = "+ currency.format(dInterestPymnt));
        System.out.println();

        // Starts loop statement,and declares formula for loan balance and interest paid


//        while (dMonthlyBalance > 0);

//        {

        System.out.println("New Balance "+ currency.format(dMonthlyBalance) + "\t\tPrincipal = "+ currency.format(dNewPrincipalPymnt) +"\tInterest = "+ currency.format(dNewInterestPymnt));
        System.out.println();

//        {




//        }

    } //end of main

        //start of getMonthlyInterest method
        public static double getMonthlyInterest()//calculates monthly interest based on APR

        {

            double dMonthlyInterest = dAPR / (12 * 100);
            return dMonthlyInterest;

        }//end of dMonthlyInterest method

        //start of getPayment method
        public static double getPayment()//calculates monthly mortgage payment

        {

            double dPayment = (dBalance * getMonthlyInterest()) / (1 - Math.pow(1 + getMonthlyInterest(), - iTerm));
            return dPayment;

        }//end of getPayment method

        //start of getNewBalance method
        public static double getNewBalance()//calculates new mortgage balance

        {

            double dNewBalance = dBalance * (1 + dMonthlyInterest) - dPayment;
            return dNewBalance;

        }//end of getNewBalance method







        //Declares and builds new variables
//        double dInterestPaid = 0;
//        int lineCount = 12;
//        double loanBalance = newBalance - dPrincipalPymnt;

        //Starts loop statement,and declares formula for loan balance and interest paid
//        do dPayment() = (getNewBalance() * getMonthlyInterest()) / (1 - Math.pow(1 + getMonthlyInterest(), - iTerm))

//        interestPaid = monthlyPayment * interest;

//        while (newBalance > 0)

//        {

        //Displays the loan balance and interest paid

//        System.out.println("The loan balance is: $" + currency.format(newBalance));
//        System.out.println("The interest paid on the loan is: $" + currency.format(dInterestPymnt));

        //Pauses screen
//        if (lineCount == 12)

//        {
//        lineCount = 0;
//        try

//        {
//        Thread.sleep(1500);
//        }

//        catch (InterruptedException e)

//        {
//        }
//        }
//        }
        //Stops loop statement
//        if (loanBalance == 0)

//        {

//        System.out.println("The loan balance is: $0.00");

//        }

} //end of class



User is offlineProfile CardPM
+Quote Post

vasdueva
RE: Java Mortgage Calc Help Needed With Loops
21 Apr, 2007 - 05:42 PM
Post #3

D.I.C Head
**

Joined: 3 Apr, 2007
Posts: 69


My Contributions
Insert a while loop when you do the calculations on the loan have it return the balance and reuse the "balance variable" in the next iteration of the loop.

As far as pausing, ask the user for an input character and make that the the boolean controlling your loop.


Here I felt ambitious so I went through and changed a few things to make it easier to loop. Donno if you can do without those static vars or not, if so here try this.

CODE


/*

    Title:         Mortgage2.java
    Writen by: Jim Blum
    Date:         4/15/2007
    Version:    2.0

    Description: Modify the mortgage program to display the mortgage payment amount.
    Then, list the loan balance and interest paid for each payment over
    the term of the loan.  The list would scroll off the screen, but use
    loops to display a partial list, hesitate, and then display more of
    the list.  Do not use a graphical user interface.
    Insert comments in the program to document the program.

*/

import java.util.Date;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Mortgage2
{
    //Format Currency
    static DecimalFormat currency = new DecimalFormat("$#,000.00");

    //Declare Variables
    
    static int iTerm = 360; //hardcoded mortage term in months
    static double dAPR = 5.75; //hardcoded Annual Percentage Rate (APR)

    //Date Constructor
    static Date currentDate = new Date();

    public static void main (String[] args) throws IOException
    {
        Scanner input = new Scanner(System.in);
        
        double dBalance = 200000; //hardcoded mortage principal
        double newBalance = 200000;
        String str = "";
        char loop = 'y';
        // Program Header
        System.out.println();
        System.out.println("\t\t\tMortage Calculator");
        System.out.println("\t\t\tBy Jim Blum");
        System.out.println("\t\t\tJava Programming");
        System.out.println("\t\t\t" + currentDate);
        System.out.println();
        while(loop == 'y')
        {
            dBalance = newBalance;
            double dMonthlyInterest = getMonthlyInterest(dAPR);//Monthly interest rate
            double dPayment = getPayment(dMonthlyInterest, dBalance);//Monthly payment
            newBalance = getNewBalance(dMonthlyInterest,dPayment, dBalance);//Monthly balance after principal payment applied
            double dPrincipalPymnt = dBalance - newBalance;//Amount applied monthly to principal
            double dInterestPymnt = dPayment - dPrincipalPymnt; //Amount applied monthly to interest
    
    
            //Output Total Payment
            System.out.println("The monthly payment on " + dBalance + "at 5.75% APR is "+ currency.format(dPayment));
            System.out.println();
            System.out.println("New Balance "+ currency.format(newBalance) + "\t\tPrincipal = "+ currency.format(dPrincipalPymnt) +"\tInterest = "+ currency.format(dInterestPymnt));
            System.out.println();
            System.out.println();
            System.out.print("continue(y/n): ");
            str = input.nextLine();
            loop = str.charAt(0);
        }
    } //end of main

        public static double getMonthlyInterest(double dAPR)//calculates monthly interest based on APR

        {

            double dMonthlyInterest = dAPR / (12 * 100);
            return dMonthlyInterest;

        }//end of dMonthlyInterest method

        public static double getPayment(double dMonthlyInterest, double dBalance )//calculates monthly mortgage payment

        {

            double dPayment = (dBalance * getMonthlyInterest(dAPR)) / (1 - Math.pow(1 + getMonthlyInterest(dAPR), - iTerm));
            return dPayment;

        }//end of getPayment method

        public static double getNewBalance(double dMonthlyInterest, double dPayment,double dBalance)//calculates new mortgage balance

        {

            double newBalance = dBalance * (1 + dMonthlyInterest) - dPayment;
            return newBalance;

        }//end of getNewBalance method

} //end of class

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month