Solved

GUI Java Mortgage Calculator

Posted on 2007-04-04
4
647 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
Comment Utility
The event handler for the calculateButton does nothing other than parse the input fields
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now