Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

GUI Java Mortgage Calculator

Posted on 2007-04-04
4
654 Views
Last Modified: 2012-05-05
Ok Java Guru's....I have a GUI Java Mortgage Program now and can't get it to calculate and display the payment. I am at a loss and could really need an eye opener on this one. I have looked at the code too long and am lost at to what I forgot. It compiles and runs with the exception of not computing.....so here it is, enjoy....
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class Mortgage5 extends JFrame implements ActionListener {   //Class Header


             double rate = 0;  //Initialize variables
             double monthlyPayment = 0;
             double principal = 0;
             int term = 0;
             double interest = 0;


          JPanel row1 = new JPanel(); // Set up boxes to label Items
          JLabel mortgage_label = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);

          JPanel row2 = new JPanel(new GridLayout(1, 2));
          JLabel principal_label = new JLabel("Mortgage Principal $",JLabel.LEFT);
          JTextField principal_txt = new JTextField(10);

          JPanel row3 = new JPanel(new GridLayout(1, 2));
          JLabel term_label = new JLabel("Mortgage Term (Yrs)",JLabel.LEFT);
          JTextField term_txt = new JTextField(10);

          JPanel row4 = new JPanel(new GridLayout(1, 2));
          JLabel rate_label = new JLabel("Interest Rate (%)", JLabel.LEFT);
          JTextField rate_txt = new JTextField(10);

          JPanel row5 = new JPanel(new GridLayout(1, 2));
          JLabel payment_label = new JLabel("Monthly Payment $", JLabel.LEFT);
          JTextField payment_txt = new JTextField(10);

          JPanel button = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10)); //Create Buttons
          JButton clearButton = new JButton("Clear");
          JButton exitButton = new JButton("Exit");
          JButton calculateButton = new JButton("Calculate");
          JButton amortizeButton = new JButton("Amortize Payments");

          JTextArea displayArea = new JTextArea(10, 45);  //Payment Text Area
          JScrollPane scroll = new JScrollPane(displayArea);


                        public Mortgage5() {


                        //super ("Mortgage Payment Calculator");
                        setSize(550, 500);
                        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          Container pane = getContentPane();

          Border rowborder = new EmptyBorder( 3, 10, 3, 10 );

          pane.add(row1);
          row1.add(mortgage_label);
          row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
          row1.setBorder( rowborder);

          pane.add(row2);
          row2.add(principal_label);
          row2.add(principal_txt);
          row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
          row2.setBorder( rowborder);

          pane.add(row3);
          row3.add(term_label);
          row3.add(term_txt);
          row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
          row3.setBorder( rowborder);

          pane.add(row4);
          row4.add(rate_label);
          row4.add(rate_txt);
          row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
          row4.setBorder( rowborder);

          pane.add(row5);
          row5.add(payment_label);
          row5.add(payment_txt);
          payment_txt.setEnabled(false);                          //set payment amount uneditable
          row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
          row5.setBorder( rowborder);

          button.add(calculateButton);
          button.add(clearButton);
          button.add(exitButton);
          button.add(amortizeButton);
          pane.add(button);
          amortizeButton.setEnabled(false);
          button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height));


          scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
          pane.add(scroll);

          pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
          setVisible(true);
          setContentPane(pane);


          clearButton.addActionListener(this);  //Add Listeners
          exitButton.addActionListener(this);
          calculateButton.addActionListener(this);
          amortizeButton.addActionListener(this);

        }
               public void actionPerformed(ActionEvent event) {

                    Object command = event.getSource();


                    if(command == calculateButton) {

                              try {

                              principal = Double.parseDouble(principal_txt.getText());
                              }
                              catch(NumberFormatException e) {

                           //catch null pointer exception if Principal is null
                           JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
                             }

                             try {

                                 term = Integer.parseInt(term_txt.getText());
                              }
                              catch(NumberFormatException e) {

                         //catch null pointer exception if Term is null
                            JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
                              }

                                  try {

                              rate = Double.parseDouble(rate_txt.getText());
                              }
                              catch(NumberFormatException e) {
                         //catch null pointer exception if Rate is null
                              JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
                             }


          }

                    if(command == clearButton)  {

                         principal_txt.setText(null);
                         term_txt.setText(null);
                         rate_txt.setText(null);
                         payment_txt.setText(null);
                         displayArea.setText(null);
                    }

                    if(command == exitButton) {

                         System.exit(0);
                    }

                    if(command == amortizeButton) {

                         double year = 0;
                         double interest = 0;
                         double balance = 0;
                         double monthlyInterest = 0;
                         String titles = "Month \t Principal \t Interest \t Balance\n";

                         monthlyInterest = (rate / 100) /12;
                         balance = principal;

                         displayArea.setText(titles);


                         for(int counter = 0; counter <= term - 1; counter++) {


                              interest = balance * monthlyInterest;

                              if(interest <= 0)
                                   interest = 0;

                              year = monthlyPayment - interest;
                              balance = balance - year;


                              DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
                              DecimalFormat pf = new DecimalFormat("#,##0.00%");      //percentages
                              DecimalFormat mi = new DecimalFormat("#,##0.000%");     //percentages

                              //Positions the cursor to the top of the textArea
                              displayArea.setCaretPosition(0);
                         }
                    }
                    }


                    public static void main (String[] arguments) {   //Main Method

                    Mortgage5 mtg = new Mortgage5();

      }
     }
0
Comment
Question by:drisden1
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18856196
The event handler for the calculateButton does nothing other than parse the input fields
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18860940
After:

>> principal = Double.parseDouble(principal_txt.getText());
>> term = Integer.parseInt(term_txt.getText());
>> rate = Double.parseDouble(rate_txt.getText());

You probably need to calculate and display the results using a JOptionPane?
0
 
LVL 5

Accepted Solution

by:
tbboyett earned 500 total points
ID: 18861483
As CEHJ & mayankeagle mentioned already you will need to calculate the payment and display it.  You are only pulling in the values that are entered and doing nothing with them.  I have marked where you should do this in your code.  If you are having problems with the calculation, here is a link describing how to calculate the payments http://www.financialone.com/mortgages/mortcalcs

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class Mortgage5 extends JFrame implements ActionListener {   //Class Header
    double rate = 0;  //Initialize variables
    double monthlyPayment = 0;
    double principal = 0;
    int term = 0;
    double interest = 0;

    JPanel row1 = new JPanel(); // Set up boxes to label Items
    JLabel mortgage_label = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principal_label = new JLabel("Mortgage Principal $",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);

    JPanel row3 = new JPanel(new GridLayout(1, 2));
    JLabel term_label = new JLabel("Mortgage Term (Yrs)",JLabel.LEFT);
    JTextField term_txt = new JTextField(10);

    JPanel row4 = new JPanel(new GridLayout(1, 2));
    JLabel rate_label = new JLabel("Interest Rate (%)", JLabel.LEFT);
    JTextField rate_txt = new JTextField(10);

    JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel payment_label = new JLabel("Monthly Payment $", JLabel.LEFT);
    JTextField payment_txt = new JTextField(10);

    JPanel button = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10)); //Create Buttons
    JButton clearButton = new JButton("Clear");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate");
    JButton amortizeButton = new JButton("Amortize Payments");

    JTextArea displayArea = new JTextArea(10, 45);  //Payment Text Area
    JScrollPane scroll = new JScrollPane(displayArea);


    public Mortgage5() {
        //super ("Mortgage Payment Calculator");
        setSize(550, 500);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container pane = getContentPane();

        Border rowborder = new EmptyBorder( 3, 10, 3, 10 );

        pane.add(row1);
        row1.add(mortgage_label);
        row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
        row1.setBorder( rowborder);

        pane.add(row2);
        row2.add(principal_label);
        row2.add(principal_txt);
        row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
        row2.setBorder( rowborder);

        pane.add(row3);
        row3.add(term_label);
        row3.add(term_txt);
        row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
        row3.setBorder( rowborder);

        pane.add(row4);
        row4.add(rate_label);
        row4.add(rate_txt);
        row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
        row4.setBorder( rowborder);

        pane.add(row5);
        row5.add(payment_label);
        row5.add(payment_txt);
        payment_txt.setEnabled(false);                          //set payment amount uneditable
        row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
        row5.setBorder( rowborder);

        button.add(calculateButton);
        button.add(clearButton);
        button.add(exitButton);
        button.add(amortizeButton);
        pane.add(button);
        amortizeButton.setEnabled(false);
        button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height));

        scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pane.add(scroll);

        pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
        setVisible(true);
        setContentPane(pane);


        clearButton.addActionListener(this);  //Add Listeners
        exitButton.addActionListener(this);
        calculateButton.addActionListener(this);
        amortizeButton.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event) {

        Object command = event.getSource();

        if(command == calculateButton) {
            try {

                principal = Double.parseDouble(principal_txt.getText());
                term = Integer.parseInt(term_txt.getText());
                rate = Double.parseDouble(rate_txt.getText());
               
                /**
                 * Calculate your payment here and display it.
                 * For calculation help look at http://www.financialone.com/mortgages/mortcalcs
                 */
            }
            catch(NumberFormatException e) {

                //catch null pointer exception if Principal is null
                JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }

        if(command == clearButton)  {

             principal_txt.setText(null);
             term_txt.setText(null);
             rate_txt.setText(null);
             payment_txt.setText(null);
             displayArea.setText(null);
        }

        if(command == exitButton) {

             System.exit(0);
        }

        if(command == amortizeButton) {

             double year = 0;
             double interest = 0;
             double balance = 0;
             double monthlyInterest = 0;
             String titles = "Month \t Principal \t Interest \t Balance\n";

             monthlyInterest = (rate / 100) /12;
             balance = principal;

             displayArea.setText(titles);


             for(int counter = 0; counter <= term - 1; counter++) {
                  interest = balance * monthlyInterest;

                  if(interest <= 0)
                       interest = 0;

                  year = monthlyPayment - interest;
                  balance = balance - year;

                  DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
                  DecimalFormat pf = new DecimalFormat("#,##0.00%");      //percentages
                  DecimalFormat mi = new DecimalFormat("#,##0.000%");     //percentages

                  //Positions the cursor to the top of the textArea
                  displayArea.setCaretPosition(0);
             }
        }
    }
    public static void main (String[] arguments) {   //Main Method
        Mortgage5 mtg = new Mortgage5();
    }
 }
0
 

Author Comment

by:drisden1
ID: 18861942
tbboyett, CEHJ, and mayankeagl,
Thanks for the help, I knew I was missing something. I will repost my revised code once I find where I put the math part. I had one of those feelings I missed it and wasn't sure where to put it. Thanks again for the help...great job!!!!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
jboss wildfly 10.1 10 228
login form jsp example 2 37
Java syntax, or is it Selenium 6 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question