Solved

GUI Java Mortgage Calculator

Posted on 2007-04-04
4
657 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 54
sql import cannot be resolved jsp 3 75
SequenceInputStream example 3 22
learn programming 8 70
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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