Mortgage calculator with GUI

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2387 Views - Last Post: 27 September 2010 - 10:34 PM Rate Topic: -----

#16 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:24 AM

Excellent! Moved the button group to pane row4 and now they work. However my math is screwed. Help with that now? I get NaN when i try to calculate the monthly payment and nothing at all for the amortization.

import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.text.*;
import java.lang.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;

public class MortgageTest extends JFrame implements ActionListener {

	double principal = 0;															//principal
	int length = 0;																	//years
	int months = 0;																	//months
	double interestRate = 0;  														//interest rate
	double monthlyInterest = 0;														//monthly interest rate
	double monthlyPayment = 0;														//monthly payment
	double premium = 0;																//premium
    double interestPaid = 0;  														//interest paid
    double balance = principal;														//balance

	DecimalFormat money = new DecimalFormat("$0.00");       						//formats to 2 decimal places

    JPanel row1 = new JPanel();
    JLabel mortgage_label = new JLabel("Pam's Mortgage Calculator", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principal_label = new JLabel("Enter Mortgage Amount (do not enter $ or cents):",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);

    JPanel row3 = new JPanel(new GridLayout(1, 2));
    JLabel button_label = new JLabel("Please select one of the following options:", JLabel.CENTER);

	JPanel row4 = new JPanel(new GridLayout(1, 3));
	JRadioButton oneButton = new JRadioButton("7 years at 5.35%",true);					//creates buttons
	JRadioButton twoButton = new JRadioButton("15 years at 5.5%");
	JRadioButton threeButton = new JRadioButton("30 years at 5.75%");

	JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel monthlyPayment_label = new JLabel("Your monthly payment will be:", JLabel.LEFT);
    JTextField monthlyPayment_txt = new JTextField(10);

	JPanel row6 = new JPanel(new GridLayout(1, 2));
    JButton resetButton = new JButton("Reset");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate Monthly Payment");
    JButton amortizationButton = new JButton("Show Amortization");

    JTextArea displayArea = new JTextArea(10, 45);										//for amortization display
    JTextField number_txt = new JTextField(4);
    JTextField balance_txt = new JTextField(12);
    JTextField interestPaid_txt = new JTextField(12);
    JScrollPane scroll = new JScrollPane(displayArea);

	public MortgageTest() {

          	super ("Pam's Mortgage Calculator");
          	setSize(750, 600);															//sets overall GUI size
          	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	Container pane = getContentPane();

        	Border rowborder = new EmptyBorder( 10, 15, 5, 15 );						//sets spacing inside frame

          	pane.add(row1);
          	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(button_label);
          	row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
        	row3.setBorder( rowborder);

          	pane.add(row4);
          	ButtonGroup pickOneGroup = new ButtonGroup();										//creates group for buttons
    		pickOneGroup.add(oneButton);
			pickOneGroup.add(twoButton);
			pickOneGroup.add(threeButton);
			row4.add(oneButton);
			row4.add(twoButton);
			row4.add(threeButton);
          	row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
        	row4.setBorder( rowborder);

        	pane.add(row5);
          	row5.add(monthlyPayment_label);
          	row5.add(monthlyPayment_txt);
          	monthlyPayment_txt.setEditable(false);										//user cannot alter this text
          	row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
          	row5.setBorder( rowborder);

			pane.add(row6);
          	row6.add(resetButton);
         	row6.add(exitButton);
         	row6.add(calculateButton);
         	row6.add(amortizationButton);
          	row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
          	row6.setBorder( rowborder);

          	scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        		//for amortization display
        	pane.add(scroll);

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

			oneButton.addActionListener(this);										//button one listener
			twoButton.addActionListener(this);										//button two listener
			threeButton.addActionListener(this);									//button three listener
          	resetButton.addActionListener(this);  										//reset listener
          	exitButton.addActionListener(this);											//exit listener
          	calculateButton.addActionListener(this);									//calculate listener
          	amortizationButton.addActionListener(this);									//amortization listener
        }

        public void actionPerformed(ActionEvent event) {

                	Object command = event.getSource();
//begins error message checking
                    if(command == calculateButton) {
                    	try {
                        	principal = Double.parseDouble(principal_txt.getText());
                        }
                            catch(NumberFormatException e) {

//error message for principal amount
                           JOptionPane.showMessageDialog(null, "Dollar amounts only please! (ex. 300000)", "ERROR", JOptionPane.ERROR_MESSAGE);
                           }
                    }

//resets all amounts to blank
                    if(command == resetButton)  {
						principal_txt.setText(null);
                        monthlyPayment_txt.setText(null);
                        displayArea.setText(null);
                    }

//exits the program
                    if(command == exitButton) {
                        System.exit(0);
                    }

					if(command == calculateButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

  				    	if (event.getSource() == oneButton) {
       						length = 7;
       						interestRate = 5.35;
  		  				}

    						if (event.getSource() == twoButton) {
        						length = 15;
        						interestRate = 5.5;
    						}

    					    	if (event.getSource() == threeButton) {
      								length = 30;
      								interestRate = 5.75;
      							}

						monthlyInterest = (interestRate/12)/100;						//Determines monthly interest rate
						months = length * 12;											//Changes years to months
						monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						String s = Double.toString(monthlyPayment);
						monthlyPayment_txt.setText(s);
					}

					if(command == amortizationButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

						for(int number = 0; number <= length - 1; number++) {

							if (event.getSource() == oneButton) {
	       						length = 7;
	       						interestRate = 5.35;
							}

	       						if (event.getSource() == twoButton) {
	        						length = 15;
	        						interestRate = 5.5;
	       						}

	        						if (event.getSource() == threeButton) {
	      								length = 30;
	      								interestRate = 5.75;
	        						}


						monthlyInterest = (interestRate/12)/100;
	                    months = length * 12;											//Changes years to months
						monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						interestPaid = balance * monthlyInterest + .005;				//Determines interest paid amount
	                    premium = monthlyPayment - interestPaid;						//Determines premium paid
	                    balance = balance - premium;									//Determines loan balance

						String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
						displayArea.setText(titles);

							while (number <= months)
							{
								interestPaid = balance * monthlyInterest + .005;				//Determines interest paid amount
	                    		premium = monthlyPayment - interestPaid;						//Determines premium paid
	                    		balance = balance - premium;									//Determines loan balance
								displayArea.setText(number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
								String s = Double.toString(number);
								number_txt.setText(s);
								balance_txt.setText(s);
								interestPaid_txt.setText(s);
								number++;
							}
						}
					}
}
	public static void main(String[] args) {
				Frame f = new MortgageTest();
					f.show();

	}
}

This post has been edited by pcornacchione: 27 September 2010 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#17 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 05:47 PM

I think i screwed up the looping on my math calculations for this mortgage calculator. I cannot figure out where i did it at. Can someone please take a look and give me a hand? Its supposed to display the monthly payment at the top of the GUI but i only get "NaN" for the results and then the amortization schedule at the bottom which does not display at all. All you java gurus have been super for helping me thus far but now i'm desperate because this program is due in a few hours and i still don't have it working correctly!! HELP!!!!!!!!!!!!!

import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.text.*;
import java.lang.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;


public class Mortgage3 extends JFrame implements ActionListener {

	double principal = 0;															//principal
	int length = 0;																	//years
	int months = 0;																	//months
	double interestRate = 0;  														//interest rate
	double monthlyInterest = 0;														//monthly interest rate
	double monthlyPayment = 0;														//monthly payment
	double premium = 0;																//premium
    double interestPaid = 0;  														//interest paid
    double balance = principal;														//balance

	DecimalFormat money = new DecimalFormat("$0.00");       						//formats to 2 decimal places

    JPanel row1 = new JPanel();
    JLabel mortgage_label = new JLabel("Pam's Mortgage Calculator", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principal_label = new JLabel("Enter Mortgage Amount (do not enter $ or cents):",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);

    JPanel row3 = new JPanel(new GridLayout(1, 2));
    JLabel button_label = new JLabel("Please select one of the following options:", JLabel.CENTER);

	JPanel row4 = new JPanel(new GridLayout(1, 3));
	JRadioButton oneButton = new JRadioButton("7 years at 5.35%",true);				//creates buttons
	JRadioButton twoButton = new JRadioButton("15 years at 5.5%");
	JRadioButton threeButton = new JRadioButton("30 years at 5.75%");

	JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel monthlyPayment_label = new JLabel("Your monthly payment will be:", JLabel.LEFT);
    JTextField monthlyPayment_txt = new JTextField(10);

	JPanel row6 = new JPanel(new GridLayout(1, 2));
    JButton resetButton = new JButton("Reset");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate Monthly Payment");
    JButton amortizationButton = new JButton("Show Amortization");

    JTextArea displayArea = new JTextArea(10, 45);									//for amortization display
    JTextField number_txt = new JTextField(4);
    JTextField balance_txt = new JTextField(12);
    JTextField interestPaid_txt = new JTextField(12);
    JScrollPane scroll = new JScrollPane(displayArea);

	public Mortgage3() {

          	super ("Pam's Mortgage Calculator");
          	setSize(750, 600);														//sets overall GUI size
          	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	Container pane = getContentPane();

        	Border rowborder = new EmptyBorder( 10, 15, 5, 15 );					//sets spacing inside frame

          	pane.add(row1);
          	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(button_label);
          	row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
        	row3.setBorder( rowborder);

          	pane.add(row4);
          	ButtonGroup pickOneGroup = new ButtonGroup();									//creates group for buttons
    		pickOneGroup.add(oneButton);
			pickOneGroup.add(twoButton);
			pickOneGroup.add(threeButton);
			row4.add(oneButton);
			row4.add(twoButton);
			row4.add(threeButton);
          	row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
        	row4.setBorder( rowborder);

        	pane.add(row5);
          	row5.add(monthlyPayment_label);
          	row5.add(monthlyPayment_txt);
          	monthlyPayment_txt.setEditable(false);									//user cannot alter this text
          	row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
          	row5.setBorder( rowborder);

			pane.add(row6);
          	row6.add(resetButton);
         	row6.add(exitButton);
         	row6.add(calculateButton);
         	row6.add(amortizationButton);
          	row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
          	row6.setBorder( rowborder);

          	scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        	//for amortization display
        	pane.add(scroll);

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

			oneButton.addActionListener(this);										//button one listener
			twoButton.addActionListener(this);										//button two listener
			threeButton.addActionListener(this);									//button three listener
          	resetButton.addActionListener(this);  									//reset listener
          	exitButton.addActionListener(this);										//exit listener
          	calculateButton.addActionListener(this);								//calculate listener
          	amortizationButton.addActionListener(this);								//amortization listener
        }

        public void actionPerformed(ActionEvent event) {

                	Object command = event.getSource();
//begins error message checking
                    if(command == calculateButton) {
                    	try {
                        	principal = Double.parseDouble(principal_txt.getText());
                        }
                            catch(NumberFormatException e) {

//error message for principal amount
                           JOptionPane.showMessageDialog(null, "Dollar amounts only please! (ex. 300000)", "ERROR", JOptionPane.ERROR_MESSAGE);
                           }
                    }

//resets all amounts to blank
                    if(command == resetButton)  {
						principal_txt.setText(null);
                        monthlyPayment_txt.setText(null);
                        displayArea.setText(null);
                    }

//exits the program
                    if(command == exitButton) {
                        System.exit(0);
                    }

					if(command == calculateButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

  				    	if (event.getSource() == oneButton) {
       						length = 7;
       						interestRate = 5.35;
  		  				}

    						if (event.getSource() == twoButton) {
        						length = 15;
        						interestRate = 5.5;
    						}

    					    	if (event.getSource() == threeButton) {
      								length = 30;
      								interestRate = 5.75;
      							}

    					monthlyInterest = (interestRate/12)/100;					//Determines monthly interest rate
						months = length * 12;										//Changes years to months
						monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						String s = Double.toString(monthlyPayment);
						monthlyPayment_txt.setText(s);
					}

					if(command == amortizationButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

						for(int number = 0; number <= length - 1; number++) {

							if (event.getSource() == oneButton) {
	       						length = 7;
	       						interestRate = 5.35;
							}

	       						if (event.getSource() == twoButton) {
	        						length = 15;
	        						interestRate = 5.5;
	       						}

	        						if (event.getSource() == threeButton) {
	      								length = 30;
	      								interestRate = 5.75;
	        						}

						monthlyInterest = (interestRate/12)/100;
	                    months = length * 12;										//Changes years to months
						monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						interestPaid = balance * monthlyInterest + .005;			//Determines interest paid amount
	                    premium = monthlyPayment - interestPaid;					//Determines premium paid
	                    balance = balance - premium;								//Determines loan balance

						String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
						displayArea.setText(titles);
						displayArea.setText(number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
						String s = Double.toString(number);
						number_txt.setText(s);
						balance_txt.setText(s);
						interestPaid_txt.setText(s);
						}
					}
}
	public static void main(String[] args) {
				Frame f = new Mortgage3();
					f.show();

	}
}

Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 05:55 PM

Duplicate topics merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#19 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 06:37 PM

If you have a Not A Number it is you screw up with 0 values as this println() shows

    			monthlyInterest = (interestRate/12)/100;					//Determines monthly interest rate
			months = length * 12;										//Changes years to months
			System.out.println("principal " + principal + " interest " + monthlyInterest + " months " + months);
			monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment



principal 12000.0 interest 0.0 months 0
Was This Post Helpful? 1
  • +
  • -

#20 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 06:44 PM

pbl--I'm confused. Do you mean i have the print line in the wrong location? I looked at my coding and i'm sorry but i don't understand what you mean.
Was This Post Helpful? 0
  • +
  • -

#21 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 07:53 PM

View Postpcornacchione, on 27 September 2010 - 07:44 PM, said:

pbl--I'm confused. Do you mean i have the print line in the wrong location? I looked at my coding and i'm sorry but i don't understand what you mean.

I just added a System.out.prinltn() statement to show you that when you calculate monthlyPayment

monthlyInterest and months are 0
Was This Post Helpful? 1
  • +
  • -

#22 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:02 PM

pbl--i really do thank you for replying. But i'm sorry i simply don't understand. I don't know how to fix this because i don't see where you get the fields are empty. I guess i'm just beyond stupid. Two more weeks of Java hell...i don't think i will survive.
Was This Post Helpful? 0
  • +
  • -

#23 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:12 PM

Here is your error... I think


   if(command == calculateButton) {
	double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users 

  	if (event.getSource() == oneButton) {
       	   length = 7;
       	   interestRate = 5.35;
  	}

    	if (event.getSource() == twoButton) {
           length = 15;
           interestRate = 5.5;
    	}

    	if (event.getSource() == threeButton) {
      	   length = 30;
      	   interestRate = 5.75;
      	}


event.getSource() will never be one of your radio button it is actally calculateButton
You have to do
   if(command == calculateButton) {
	double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users 

  	if (oneButton.isSelected()) {
       	   length = 7;
       	   interestRate = 5.35;
  	}
    	else if (twoButton.isSelected()) {
           length = 15;
           interestRate = 5.5;
    	}
    	else {
      	   length = 30;
      	   interestRate = 5.75;
      	}
        // now you can calculate


Was This Post Helpful? 1
  • +
  • -

#24 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:22 PM

pbl--bless you!! I figured it was gonna be something fairly simple but i'm just so stressed right now i couldn't see it. Now on to fixing the amortization portion. Will this nightmare ever end? **sigh**
Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:48 PM

View Postpcornacchione, on 27 September 2010 - 09:22 PM, said:

pbl--bless you!! I figured it was gonna be something fairly simple but i'm just so stressed right now i couldn't see it. Now on to fixing the amortization portion. Will this nightmare ever end? **sigh**

First you have to do the same thing about which radio button is selecteded
then you have to append() to the textArea. Doing setText() erase what was there before

		if(command == amortizationButton) {
			double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input
			if (oneButton.isSelected()) {
				length = 7;
				interestRate = 5.35;
			}

			else if (twoButton.isSelected()) {
				length = 15;
				interestRate = 5.5;
			}

			else {
				length = 30;
				interestRate = 5.75;
			}

			String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
			displayArea.setText(titles);
			for(int number = 0; number <= length - 1; number++) {

				monthlyInterest = (interestRate/12)/100;
				months = length * 12;										//Changes years to months
				monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
				interestPaid = balance * monthlyInterest + .005;			//Determines interest paid amount
				premium = monthlyPayment - interestPaid;					//Determines premium paid
				balance = balance - premium;								//Determines loan balance

				displayArea.append("\n" + number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
				String s = Double.toString(number);
				number_txt.setText(s);
				balance_txt.setText(s);
				interestPaid_txt.setText(s);
			}


Was This Post Helpful? 1
  • +
  • -

#26 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:04 PM

pbl--thank you so very much for working with me!! I still have a problem with the looping. It isn't printing the full amortization...only one line. But it looks great!! You are my Java Guru!! Here's the updated program...

import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.text.*;
import java.lang.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;


public class Mortgage3 extends JFrame implements ActionListener {

	double principal = 0;															//principal
	int length = 0;																	//years
	int months = 0;																	//months
	double interestRate = 0;  														//interest rate
	double monthlyInterest = 0;														//monthly interest rate
	double monthlyPayment = 0;														//monthly payment
	double premium = 0;																//premium
    double interestPaid = 0;  														//interest paid
    double balance = principal;														//balance

	DecimalFormat money = new DecimalFormat("$0.00");       						//formats to 2 decimal places

    JPanel row1 = new JPanel();
    JLabel mortgage_label = new JLabel("Pam's Mortgage Calculator", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principal_label = new JLabel("Enter Mortgage Amount (do not enter $ or cents):",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);

    JPanel row3 = new JPanel(new GridLayout(1, 2));
    JLabel button_label = new JLabel("Please select one of the following options:", JLabel.CENTER);

	JPanel row4 = new JPanel(new GridLayout(1, 3));
	JRadioButton oneButton = new JRadioButton("7 years at 5.35%",true);				//creates buttons
	JRadioButton twoButton = new JRadioButton("15 years at 5.5%");
	JRadioButton threeButton = new JRadioButton("30 years at 5.75%");

	JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel monthlyPayment_label = new JLabel("Your monthly payment will be:", JLabel.LEFT);
    JTextField monthlyPayment_txt = new JTextField(10);

	JPanel row6 = new JPanel(new GridLayout(1, 2));
    JButton resetButton = new JButton("Reset");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate Monthly Payment");
    JButton amortizationButton = new JButton("Show Amortization");

    JTextArea displayArea = new JTextArea(10, 45);									//for amortization display
    JTextField number_txt = new JTextField(4);
    JTextField balance_txt = new JTextField(12);
    JTextField interestPaid_txt = new JTextField(12);
    JScrollPane scroll = new JScrollPane(displayArea);

	public Mortgage3() {

          	super ("Pam's Mortgage Calculator");
          	setSize(750, 600);														//sets overall GUI size
          	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	Container pane = getContentPane();

        	Border rowborder = new EmptyBorder( 10, 15, 5, 15 );					//sets spacing inside frame

          	pane.add(row1);
          	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(button_label);
          	row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
        	row3.setBorder( rowborder);

          	pane.add(row4);
          	ButtonGroup pickOneGroup = new ButtonGroup();									//creates group for buttons
    		pickOneGroup.add(oneButton);
			pickOneGroup.add(twoButton);
			pickOneGroup.add(threeButton);
			row4.add(oneButton);
			row4.add(twoButton);
			row4.add(threeButton);
          	row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
        	row4.setBorder( rowborder);

        	pane.add(row5);
          	row5.add(monthlyPayment_label);
          	row5.add(monthlyPayment_txt);
          	monthlyPayment_txt.setEditable(false);									//user cannot alter this text
          	row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
          	row5.setBorder( rowborder);

			pane.add(row6);
          	row6.add(resetButton);
         	row6.add(exitButton);
         	row6.add(calculateButton);
         	row6.add(amortizationButton);
          	row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
          	row6.setBorder( rowborder);

          	scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        	//for amortization display
        	pane.add(scroll);

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

			oneButton.addActionListener(this);										//button one listener
			twoButton.addActionListener(this);										//button two listener
			threeButton.addActionListener(this);									//button three listener
          	resetButton.addActionListener(this);  									//reset listener
          	exitButton.addActionListener(this);										//exit listener
          	calculateButton.addActionListener(this);								//calculate listener
          	amortizationButton.addActionListener(this);								//amortization listener
        }

        public void actionPerformed(ActionEvent event) {

                	Object command = event.getSource();
//begins error message checking
                    if(command == calculateButton) {
                    	try {
                        	principal = Double.parseDouble(principal_txt.getText());
                        }
                            catch(NumberFormatException e) {

//error message for principal amount
                           JOptionPane.showMessageDialog(null, "Dollar amounts only please! (ex. 300000)", "ERROR", JOptionPane.ERROR_MESSAGE);
                           }
                    }

//resets all amounts to blank
                    if(command == resetButton)  {
						principal_txt.setText(null);
                        monthlyPayment_txt.setText(null);
                        displayArea.setText(null);
                    }

//exits the program
                    if(command == exitButton) {
                        System.exit(0);
                    }

					if(command == calculateButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

  				    	if (event.getSource() == oneButton) {
       						length = 7;
       						interestRate = 5.35;
  		  				}

    						else if (event.getSource() == twoButton) {
        						length = 15;
        						interestRate = 5.5;
    						}

    					    	else  {         //(event.getSource() == threeButton)
      								length = 30;
      								interestRate = 5.75;
      							}

    					monthlyInterest = (interestRate/12)/100;					//Determines monthly interest rate
						months = length * 12;										//Changes years to months
						monthlyPayment = .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						String s = Double.toString(monthlyPayment);
						monthlyPayment_txt.setText(s);
					}

					if(command == amortizationButton) {
						double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input

						for(int number = 0; number <= length - 1; number++) {

							if (event.getSource() == oneButton) {
	       						length = 7;
	       						interestRate = 5.35;
							}

	       						else if (event.getSource() == twoButton) {
	        						length = 15;
	        						interestRate = 5.5;
	       						}

	        						else  {          //(event.getSource() == threeButton)
	      								length = 30;
	      								interestRate = 5.75;
	        						}

						monthlyInterest = (interestRate/12)/100;
	                    months = length * 12;										//Changes years to months
						monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
						interestPaid = balance * monthlyInterest + .005;			//Determines interest paid amount
	                    premium = monthlyPayment - interestPaid;					//Determines premium paid
	                    balance = balance - premium;								//Determines loan balance

						String titles = "\t Payment # \t\t Loan Balance \t\t Interest Paid \n";
						displayArea.setText(titles);
						displayArea.append("\t"+number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
						String s = Double.toString(number);
						number_txt.setText(s);
						balance_txt.setText(s);
						interestPaid_txt.setText(s);
						}
					}
}
	public static void main(String[] args) {
				Frame f = new Mortgage3();
					f.show();

	}
}

Was This Post Helpful? 0
  • +
  • -

#27 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:26 PM

You have to set length BEFORE entering the loop as I did
without that length will be == 0 and the loop never performed

Also use the same technique
if(oneButton.isSelected())
not
if(event.getSource() == oneButton)

at least cut & paste correctly :)

if(command == amortizationButton) {
	double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input
	if (oneButton.isSelected()) {
		length = 7;
		interestRate = 5.35;
	}

	else if (twoButton.isSelected()) {
		length = 15;
		interestRate = 5.5;
	}

	else {
		length = 30;
		interestRate = 5.75;
	}

	String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
	displayArea.setText(titles);
	for(int number = 0; number <= length - 1; number++) {

		monthlyInterest = (interestRate/12)/100;
		months = length * 12;										//Changes years to months
		monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment
		interestPaid = balance * monthlyInterest + .005;			//Determines interest paid amount
		premium = monthlyPayment - interestPaid;					//Determines premium paid
		balance = balance - premium;								//Determines loan balance

		displayArea.append("\n" + number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
		String s = Double.toString(number);
		number_txt.setText(s);
		balance_txt.setText(s);
		interestPaid_txt.setText(s);
	}


Was This Post Helpful? 1
  • +
  • -

#28 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:49 PM

pbl--you are my savior!! Still have math errors but it works! :bananaman: The amortization only runs to month 29 and the balance and interestPaid are off. I used the exact same math calculations that worked fine for the last assignment so i don't get it. But i'm so happy now!! THANK YOU THANK YOU THANK YOU!! You have made a stupid newbie very happy tonight!! :wacko:

UPDATE--got the math almost worked out but i still don't see the error for the loop to get it past 29??

UPDATE--math corrected!! Just need to fix the looping error so more than 29 payments will show!! Suggestions??

This post has been edited by pcornacchione: 27 September 2010 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#29 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:25 PM

Length is years, not months:

...
for(int number = 0; number <= length - 1; number++) {
...


Was This Post Helpful? 1
  • +
  • -

#30 pcornacchione  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 10

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:34 PM

View Postn8wxs, on 27 September 2010 - 09:25 PM, said:

Length is years, not months:

...
for(int number = 0; number <= length - 1; number++) {
...



Geez, all my problems have been so simple and i've been so blind! LOL Thank you so much for pointing that out!! Everyone here is so great!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2