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();
}
}
29 Replies - 1958 Views - Last Post: 27 September 2010 - 10:34 PM
#1
Mortgage calculator with GUI
Posted 25 September 2010 - 06:09 PM
Replies To: Mortgage calculator with GUI
#2
Re: Mortgage calculator with GUI
Posted 25 September 2010 - 10:30 PM
#3
Re: Mortgage calculator with GUI
Posted 25 September 2010 - 11:03 PM
macosxnerd101, on 25 September 2010 - 09:30 PM, said:
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.
#4
Re: Mortgage calculator with GUI
Posted 25 September 2010 - 11:11 PM
#5
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 10:12 AM
macosxnerd101, on 25 September 2010 - 10:11 PM, said:
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!
#6
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 11:26 AM
#7
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 11:53 AM
Me -->
#8
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 12:03 PM
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";
#9
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 01:09 PM
macosxnerd101, on 26 September 2010 - 11:03 AM, said:
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);
^
#10
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 08:31 PM
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
#11
Re: Mortgage calculator with GUI
Posted 26 September 2010 - 09:30 PM
Thank you to everyone for the help so far!! Y
#12
Re: Mortgage calculator with GUI
Posted 27 September 2010 - 12:25 AM
OH! And currently no errors...that's the only good thing.
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();
}
}
#13
Re: Mortgage calculator with GUI
Posted 27 September 2010 - 04:33 AM
http://download.orac...uttongroup.html
It will group the buttons together so that you can only select one RadioButton in the group.
#14
Re: Mortgage calculator with GUI
Posted 27 September 2010 - 09:52 AM
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.
#15
Re: Mortgage calculator with GUI
Posted 27 September 2010 - 09:59 AM
|
|

New Topic/Question
Reply



MultiQuote








|