Mortgage calculator with GUI

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 pcornacchione  Icon User is offline

  • New D.I.C Head

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

Mortgage calculator with GUI

Posted 25 September 2010 - 06:09 PM

Okay folks, i'm back again. **sigh** Trying to get this thing to work with user input for the loan amount and three choices for the term/interest rate. Then it needs the full amortization for each of the three. This is the disaster i have so far. I'm hopelessly lost and confused. I'd appreciate help, pointed to a tutorial, or anything to make this clear!!

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 **not sure how to implement here
    DecimalFormat percent = new DecimalFormat("0.00%");     						//formats 2 decimal places and % sign **not sure how to implement here
    DecimalFormat whole = new DecimalFormat("0");           						//formats to whole number **not sure how to implement here

    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("Please enter desired Mortgage Amount (do not enter $ or cents)",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);

    JPanel row3 = new JPanel(new GridLayout(1,2));
	JRadioButton rbOne = new JRadioButton("7 Years at 5.35%",true);
	JRadioButton rbTwo = new JRadioButton("15 Years at 5.5%",true);
	JRadioButton rbThree = new JRadioButton("30 Years at 5.75%",true);

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

    JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20));				//centers buttons and set spacing between them
    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
    JScrollPane scroll = new JScrollPane(displayArea);

// Menu Selection for Term & Rate
	JMenuItem menuOne = new JMenuItem ("7 Years at 5.35%");
	JMenuItem menuTwo = new JMenuItem ("15 Years at 5.5%");
	JMenuItem menuThree = new JMenuItem ("30 Years at 5.75%");
	JMenu menu = new JMenu ("Please select one:");

	public MortgageTest() {

          	super ("Mortgage Calculator");
          	setSize(600, 250);															//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);				//**should this be blank?
          	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);				//**i think this needs to be the menu but how to set here?
          	row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
        	row3.setBorder( rowborder);

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

          	button.add(radioButtonOne);
			button.add(radioButtonTwo);
			button.add(radioButtonThree);
          	button.add(resetButton);
          	button.add(exitButton);
          	button.add(calculateButton);
          	button.add(amortizationButton);
          	pane.add(button);
          	button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height));

          	menu.add(menuOne);
			menu.addSeparator();
			menu.add(menuTwo);
			menu.addSeparator();
			menu.add(menuThree);

          	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);

          	resetButton.addActionListener(this);  										//reset listener
          	exitButton.addActionListener(this);											//exit listener
          	calculateButton.addActionListener(this);									//calculate listener
          	amortizationButton.addActionListener(this);									//amortization listener
          	chiocesJComboBox.addActionListener(this);									//jcombobox listener
          	menuOne.addActionListener(this);											//Listeners for the menu
			menuTwo.addActionListener(this);											//Listeners for the menu
			menuThree.addActionListener(this);											//Listeners for the menu

        }

        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) {

  				    	if (event.getSource() == radioButtonOne) {
       						length = 7;
       						interestRate = 5.35;
       						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 (event.getSource() == radioButtonTwo) {
        					length = 15;
        					interestRate = 5.5;
        					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 (event.getSource() == radioButtonThree) {
      						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) {

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

							if (event.getSource() == radioButtonOne) {
	       						length = 7;
	       						interestRate = 5.35;
	       						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
							}

	       					if (event.getSource() == radioButtonTwo) {
	        					length = 15;
	        					interestRate = 5.5;
	        					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
	       					}

	        				if (event.getSource() == radioButtonThree) {
	      						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";
						displayTextArea.setText(titles);
						displayTextArea.setText(number+"\t\t"+money.format(monthlyPayment)+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
						String s = Double.toString(number);
						numberTextArea.setText(s);
						String s = Double.toString(monthlyPayment);
						monthlyPaymentTextArea.setText(s);
						String s = Double.toString(balance);
						balanceTextArea.setText(s);
						String s = Double.toString(interestPaid);
						interestPaidTextArea.setText(s);
						}
					}
}
	public static void main(String[] args) {
				Frame f = new MortgageTest();
					f.show();

	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage calculator with GUI

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 25 September 2010 - 10:30 PM

So what specifically are your problems or errors?
Was This Post Helpful? 0
  • +
  • -

#3 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 25 September 2010 - 11:03 PM

View Postmacosxnerd101, on 25 September 2010 - 09:30 PM, said:

So what specifically are your problems or errors?


For starters nothing works! LOL

--------------------Configuration: <Default>--------------------
C:\MortgageTest.java:121: cannot find symbol
symbol : variable row4
location: class MortgageTest
pane.add(row4);
^
C:\MortgageTest.java:122: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.add(monthlyPayment_label);
^
C:\MortgageTest.java:123: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.add(monthlyPayment_txt);
^
C:\MortgageTest.java:125: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
^
C:\MortgageTest.java:125: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
^
C:\MortgageTest.java:126: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setBorder( rowborder);
^
C:\MortgageTest.java:128: cannot find symbol
symbol : variable radioButtonOne
location: class MortgageTest
button.add(radioButtonOne);
^
C:\MortgageTest.java:129: cannot find symbol
symbol : variable radioButtonTwo
location: class MortgageTest
button.add(radioButtonTwo);
^
C:\MortgageTest.java:130: cannot find symbol
symbol : variable radioButtonThree
location: class MortgageTest
button.add(radioButtonThree);
^
C:\MortgageTest.java:156: cannot find symbol
symbol : variable chiocesJComboBox
location: class MortgageTest
chiocesJComboBox.addActionListener(this); //jcombobox listener
^
C:\MortgageTest.java:195: cannot find symbol
symbol : variable radioButtonOne
location: class MortgageTest
if (event.getSource() == radioButtonOne) {
^
C:\MortgageTest.java:204: cannot find symbol
symbol : variable radioButtonTwo
location: class MortgageTest
if (event.getSource() == radioButtonTwo) {
^
C:\MortgageTest.java:213: cannot find symbol
symbol : variable radioButtonThree
location: class MortgageTest
if (event.getSource() == radioButtonThree) {
^
C:\MortgageTest.java:228: cannot find symbol
symbol : variable radioButtonOne
location: class MortgageTest
if (event.getSource() == radioButtonOne) {
^
C:\MortgageTest.java:239: cannot find symbol
symbol : variable radioButtonTwo
location: class MortgageTest
if (event.getSource() == radioButtonTwo) {
^
C:\MortgageTest.java:250: cannot find symbol
symbol : variable radioButtonThree
location: class MortgageTest
if (event.getSource() == radioButtonThree) {
^
C:\MortgageTest.java:262: cannot find symbol
symbol : variable displayTextArea
location: class MortgageTest
displayTextArea.setText(titles);
^
C:\MortgageTest.java:263: cannot find symbol
symbol : variable displayTextArea
location: class MortgageTest
displayTextArea.setText(number+"\t\t"+money.format(monthlyPayment)+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
^
C:\MortgageTest.java:265: cannot find symbol
symbol : variable numberTextArea
location: class MortgageTest
numberTextArea.setText(s);
^
C:\MortgageTest.java:266: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(monthlyPayment);
^
C:\MortgageTest.java:267: cannot find symbol
symbol : variable monthlyPaymentTextArea
location: class MortgageTest
monthlyPaymentTextArea.setText(s);
^
C:\MortgageTest.java:268: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(balance);
^
C:\MortgageTest.java:269: cannot find symbol
symbol : variable balanceTextArea
location: class MortgageTest
balanceTextArea.setText(s);
^
C:\MortgageTest.java:270: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(interestPaid);
^
C:\MortgageTest.java:271: cannot find symbol
symbol : variable interestPaidTextArea
location: class MortgageTest
interestPaidTextArea.setText(s);
^
Note: C:\MortgageTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
25 errors

Process completed.

--------------------Configuration: <Default>--------------------
java.lang.NoClassDefFoundError: MortgageTest
Caused by: java.lang.ClassNotFoundException: MortgageTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MortgageTest. Program will exit.
Exception in thread "main"
Process completed.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 25 September 2010 - 11:11 PM

After glancing over the errors, you didn't declare some of those variables in your class, so they cannot be found b/c they don't exist.
Was This Post Helpful? 0
  • +
  • -

#5 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 26 September 2010 - 10:12 AM

View Postmacosxnerd101, on 25 September 2010 - 10:11 PM, said:

After glancing over the errors, you didn't declare some of those variables in your class, so they cannot be found b/c they don't exist.


I thought i had everything declared? Can you be more specific please? I'm so braindead from fooling with this that i cannot see clearly. I know its prolly obvious to you but not to me!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 26 September 2010 - 11:26 AM

Four such variables are row4, radioButtonOne, radioButtonTwo, and radioButtonThree. If you use a crtl+f for these variables, you will see the first occurrences of these in use, but you will never find them declared anywhere in your code. This is an easy way to help you debug for the other occurrences of this error.
Was This Post Helpful? 0
  • +
  • -

#7 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 26 September 2010 - 11:53 AM

Okay, i'm confused now. How do you declare them? I don't remember ever having to declare things like that in previous programs. And then my next question is why only row4? Why not the other rows? I'm hopelessly confused now! :helpsmilie:

Me --> :stupid:
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 26 September 2010 - 12:03 PM

The row4 variable is the only one you didn't declare and initialize. Take a look at row1 JPanel row1 = new JPanel(); . There is no such declaration and initialization for row4, or the other variables I mentioned.

Below I explain declaration and initialization with a String as an example. See my comments for explanation.
//declare x; note that x is null
//so that means you have a pointer
//that doesn't reference an Object in memory.
//This means you cannot use this variable
//besides comparing it to null
String x; 

//initialize x to some value
//now you can use this String object
//and it's methods b/c x references
//"123" in memory
x = "123"; 


Was This Post Helpful? 1
  • +
  • -

#9 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 26 September 2010 - 01:09 PM

View Postmacosxnerd101, on 26 September 2010 - 11:03 AM, said:

The row4 variable is the only one you didn't declare and initialize. Take a look at row1 JPanel row1 = new JPanel(); . There is no such declaration and initialization for row4, or the other variables I mentioned.

Below I explain declaration and initialization with a String as an example. See my comments for explanation.
//declare x; note that x is null
//so that means you have a pointer
//that doesn't reference an Object in memory.
//This means you cannot use this variable
//besides comparing it to null
String x; 

//initialize x to some value
//now you can use this String object
//and it's methods b/c x references
//"123" in memory
x = "123"; 



Aahhh, it is slowly starting to make sense. I see now where i was not consistent with my naming and that threw me off with the declarations!!

I'm down to the following errors now....suggestions on how to fix them?

--------------------Configuration: <Default>--------------------
C:\MortgageTest.java:274: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(monthlyPayment);
^
C:\MortgageTest.java:276: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(balance);
^
C:\MortgageTest.java:278: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(interestPaid);
^
Was This Post Helpful? 0
  • +
  • -

#10 k1ngcor3y  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 27-August 10

Re: Mortgage calculator with GUI

Posted 26 September 2010 - 08:31 PM

Those errors are because you are declaring a variable more than once
int x = 1;
int x = 2;



^^That is an error.^^
vv This is correct vv

int x = 1;
x = 2;



You only need to declare a variable once

This post has been edited by k1ngcor3y: 26 September 2010 - 08:33 PM

Was This Post Helpful? 1
  • +
  • -

#11 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 26 September 2010 - 09:30 PM

Okay thank you. Got that much cleared up. But things still aren't working right but i'm gonna work on it some more and if i'm still screwed up i'll be back! LOL

Thank you to everyone for the help so far!! Y
Was This Post Helpful? 0
  • +
  • -

#12 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 - 12:25 AM

**sigh** Yet again i'm here seeking wisdom....GUI looks great now. However, my radio buttons are not working properly which makes my math (assuming it will work) not work.
OH! And currently no errors...that's the only good thing.

:helpsmilie: HELP!!!!!!!!!!! :helpsmilie:

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 radioButtonOne = new JRadioButton("7 Years at 5.35%",true);
	JRadioButton radioButtonTwo = new JRadioButton("15 Years at 5.5%",false);
	JRadioButton radioButtonThree = new JRadioButton("30 Years at 5.75%",false);

	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);
			row4.add(radioButtonOne);
			row4.add(radioButtonTwo);
			row4.add(radioButtonThree);
          	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);

			radioButtonOne.addActionListener(this);										//button one listener
			radioButtonTwo.addActionListener(this);										//button two listener
			radioButtonThree.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) {

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

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

    					    	if (event.getSource() == radioButtonThree) {
      								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) {

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

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

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

	        						if (event.getSource() == radioButtonThree) {
	      								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 MortgageTest();
					f.show();

	}
}

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 04:33 AM

You need to use a ButtonGroup:
http://download.orac...uttongroup.html

It will group the buttons together so that you can only select one RadioButton in the group.
Was This Post Helpful? 0
  • +
  • -

#14 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:52 AM

Okay, i created the button group. Double checked that i had all my names the same throughout the program. And now i get more errors. I'm doing the work. I'm following what everyone says to do. BUT i'm getting more and more confused because each time i make the change that is supposed to fix my problem i get more errors that send me back to square one! I'm not asking for anyone to do my work but can someone please tell me specifically what line the *bleep* problem is on? I'm frustrated and confused. Errors follow coding.

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%");
	ButtonGroup pickOneGroup = new ButtonGroup();										//creates group for buttons
    pickOneGroup.add(oneButton);
	pickOneGroup.add(twoButton);
	pickOneGroup.add(threeButton);

	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);
			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) {

  				    	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) {

						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 MortgageTest();
					f.show();

	}
}


--------------------Configuration: <Default>--------------------
C:\Users\Pam\Documents\MortgageTest.java:60: <identifier> expected
pickOneGroup.add(oneButton);
^
C:\Users\Pam\Documents\MortgageTest.java:60: <identifier> expected
pickOneGroup.add(oneButton);
^
C:\Users\Pam\Documents\MortgageTest.java:61: <identifier> expected
pickOneGroup.add(twoButton);
^
C:\Users\Pam\Documents\MortgageTest.java:61: <identifier> expected
pickOneGroup.add(twoButton);
^
C:\Users\Pam\Documents\MortgageTest.java:62: <identifier> expected
pickOneGroup.add(threeButton);
^
C:\Users\Pam\Documents\MortgageTest.java:62: <identifier> expected
pickOneGroup.add(threeButton);
^
6 errors

Process completed.
--------------------Configuration: <Default>--------------------
java.lang.NoClassDefFoundError: MortgageTest
Caused by: java.lang.ClassNotFoundException: MortgageTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MortgageTest. Program will exit.
Exception in thread "main"
Process completed.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:59 AM

You can't add the JRadioButtons to the ButtonGroup outside of a method. You can initialize variables when you declare them outside of the constructor, but all other method calls must be in other methods.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2