Ok...The assignment is to create a mortgage calculator with a GUI that asks for the principle amount, has a drop down box that allows the user to select an interest rate and term (ie 7-yr at 5.35%) and displays the informat, payment, and an amortized payment schedule...I beat my head into a wall on this and I can't seem to get anywhere. I keep throwing allot of errors. This is the fourth varient I have done trying to find a way to make this work...any help would be greatly appreciated.
/**
*This JFrame is used to gather mortgage data from the user and display the monthly
*payment amount of the mortgage based on the input.
*
* @Title MortCalc2
* @author William Cook
* @Date 4/19/2009
* @version 1.1
*/
import java.util.*;
import java.text.NumberFormat;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MortCalc2r1
extends JFrame
implements ActionListener {
//create labels and fields
JLabel nameLabel = new JLabel("Hello. Please enter your name here.");
JLabel principleLabel = new JLabel("Please enter your desired loan amount here.");
JLabel ratestringLabel = new JLabel("Please select your rate and term.");
JButton calcButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear Form");
JButton exitButton = new JButton("Exit");
JTextField nameTField = new JTextField ();
JTextField principleTField = new JTextField ();
JComboBox ratestring = newJComboBox(rateterm);
//declare variables
String [] rateterm={"7-year at 5.35%","15-year at 5.50%","30-year at 5.755"};
String name = "unknown";
String display = "0";
double principle = 0;
double payment;
int month = 1;
double pbalabce = 0;
double ppaid = 0;
double ipaid = 0;
boolean fielderror = false;
JTextArea displayArea = new JTextArea();
private Container con = getContentPane();
public static void main(String[] args) {
MortCalc2r1 rta = new MortCalc2r1();
}
public MortCalc2r1() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setSize(600,600);
setTitle("William Cook's Personal Mortgage Calculator");
con.setLayout(null);
nameLabel.setLocation(20,20);
nameLabel.setSize(400,20);
nameTField.setLocation(450,20);
nameTField.setSize(80,20);
principleLabel.setLocation(20,60);
principleLabel.setSize(400,20);
principleTField.setLocation(450,60);
principleTField.setSize(80,20);
ratestringLabel.setLocation(20,100);
ratestringLabel.setSize(400,20);
calcButton.setLocation(20,200);
calcButton.setSize(100,20);
clearButton.setLocation(150,200);
clearButton.setSize(100,20);
exitButton.setLocation(280,200);
exitButton.setSize(100,20);
con.add(nameLabel, null);
con.add(nameTField, null);
con.add(principleLabel, null);
con.add(principleTField, null);
con.add(ratestringLabel, null);
con.add(ratestring, null);
con.add(calcButton, null);
con.add(clearButton, null);
con.add(exitButton, null);
con.add(displayArea, null);
ratestring.addActionListener(this);
calcButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent thisEvent) {
Object source = thisEvent.getSource();
if (thisEvent.getSource() instanceof JComboBox) {
JComboBox ratestring = (JComboBox) thisEvent.getSource();
}
if (source == calcButton) {
displaydata();
}
if (source == clearButton) {
nameTField.setText("");
principleTField.setText("");
interestTField.setText("");
termTField.setText("");
displayArea.setText("");
}
if(source == exitButton) {
System.exit(0);
}
}
public void displaydata() {
name = nameTField.getText();
try {
principle = Double.parseDouble(principleTField.getText());
fielderror = false
pblance = principle;
if (ratestring == "7-year at 5.35%")
{
double interest = 0.0535;
int term = 7;
};
else if(ratestring == "15-year at 5.50%")
{
double interest = 0.055;
int term = 15;
};
else (display == "30-year at 5.755")
{
double interest = 0.0535;
int term = 7;
};
payment = ((interest/12) * principle)/(1-(Math.pow(1+(interest/12), (term * -12))));
pbalance = pbalance - ppaid;
ppaid = payment - ipaid;
ipaid = ((interest/12)*pbalance);
month++;
catch (Exception ex){
fielderror = true;
}
if (!fielderror){
while (pbalance > 0)
String display = "Thank you for using the Personal Mortgage Calculator " + name + ", \n";
display = display + "Your Principle balance is: "+ NumberFormat.getCurrencyInstance().format(principle) +", \n";
display = display + "Your interest is: " + interest*100 + ", \n";
display = display + "Your Loan term is: " + term + " years. \n\n\n";
display = display + "Your monthly payment will be: "+ NumberFormat.getCurrencyInstance().format(payment);
display = display + "Below is your Amortized Payment Schedule: \n\n\n";
display = display + "Month "+ month +"\tPayment: "+ NumberFormat.getCurrencyInstance().format(payment) +
"\tBalance: "+ NumberFormat.getCurrencyInstance().format(pbalance) +
"\tPrincipal Paid: "+ NumberFormat.getCurrencyInstance().format(ppaid) +
"\tInterest Paid: "+ NumberFormat.getCurrencyInstance().format(ipaid);
displayArea.setText(display);
}
else {
displayArea.setText("Please enter the correct information.");
}
}
}
}
These are the errors that TextPad gave me when I tried to compile
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:147: 'else' without 'if'
else if(ratestring == "15-year at 5.50%")
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:153: 'else' without 'if'
else (display == "30-year at 5.755")
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:166: 'catch' without 'try'
catch (Exception ex){
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:137: 'try' without 'catch' or 'finally'
try {
^
4 errors
Tool completed with exit code 1