Hello, i need assistance with a mortgage calculator program.
the requirements are :
Program designed with GUI to Write the program in Java (with a graphical user interface)
and have it calculate then display the mortgage payment amount from user input of the amount
of the mortgage and the user's selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans.
Display the mortgage payment amount followed by the loan balance and
interest paid for each payment over the term of the loan.
Here is what i have. It compiles and runs, but then when you click on the drop down menu, for the rate/term, it doesn't coordinate with the proper fields. EXCEPTION errors come up. Help would be appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
class mortgageGUI3 extends JFrame implements ActionListener // frame class and inheritance
{
static DecimalFormat decmat = new DecimalFormat("$#,##0.00");
FlowLayout flow = new FlowLayout();
JLabel amount1 = new JLabel("Amount: ");
JTextField amount = new JTextField(10);
JLabel rate1 = new JLabel("Rate: ");
JTextField rate = new JTextField(4);
JLabel term1 = new JLabel("Term: ");
JTextField term = new JTextField(4);
JComboBox mortgageBox = new JComboBox();
JLabel mortgageList = new JLabel("Choose the Mortgage Loans");
JButton calcButton = new JButton("Calculate Total");
JButton exitButton = new JButton("Exit");
JButton reset = new JButton("Reset");
JLabel blankSpaces1 = new JLabel(" ");
JLabel blankSpaces2 = new JLabel(" ");
JLabel result = new JLabel("The total is:");
JLabel sum = new JLabel("");
public mortgageGUI3()
{
Container con = getContentPane();
con.setLayout(flow); // places components in a row
con.add(amount1);
con.add(amount);
con.add(rate1);
con.add(rate);
con.add(term1);
con.add(term);
con.add(calcButton);
con.add(blankSpaces1);
con.add(result);
con.add(sum);
mortgageBox.addItem("The Mortgage Inputs Above");
mortgageBox.addItem(" 7-year Mortgage at 5.35%"); //terms
mortgageBox.addItem("15-year Mortgage at 5.5%");
mortgageBox.addItem("30-year Mortgage at 5.75%");
con.add(blankSpaces2);
con.add(exitButton);
con.add(reset);
con.add(mortgageList);
con.add(mortgageBox);
amount.setBackground(Color.yellow); // setting the background to make it attractive
rate.setBackground(Color.yellow);
term.setBackground(Color.yellow);
calcButton.setForeground(Color.black);
calcButton.setBackground(Color.yellow);
exitButton.setForeground(Color.black);
exitButton.setBackground(Color.yellow);
reset.setForeground(Color.black);
reset.setBackground(Color.yellow);
mortgageBox.addActionListener(this);
mortgageBox.setForeground(Color.white);
mortgageBox.setBackground(Color.black);
con.setBackground(Color.white); //sets the background color
calcButton.addActionListener(this);
exitButton.addActionListener(this);
reset.addActionListener(this);
}
public static void main(String[] args) // main program
{
mortgageGUI3 cFrame = new mortgageGUI3();
cFrame.setTitle("McBride's Mortgage Calculator");
cFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cFrame.setSize(300,300);
cFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
Object resetButton = null;
if(source == calcButton)
{
try // exception handling
{
String amt = amount.getText();
String trm = term.getText();
String rte = rate.getText();
double a1 = Double.parseDouble(amt);
double r1 = Double.parseDouble(rte);
if(r1 == 0) getCleanScreen();
double t1 = Double.parseDouble(trm);
if(t1 == 0) getCleanScreen();
java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");
double mortgagefigure = getPayment(a1, r1, t1, 12);
double total = getFigure(mortgagefigure);
String output = "" + doldec.format(total);
sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
}
catch (NumberFormatException error)
{
getCleanScreen();
}
}
else if (source == mortgageBox)
{
int sizeNum = mortgageBox.getSelectedIndex();
if (sizeNum == 0)
try
{
getCleanScreen();
}
catch (NumberFormatException error)
{
getCleanScreen();
}
else if (sizeNum == 1)
{
try
{
String amt = amount.getText();
double a1 = Double.parseDouble(amt);
double r1 = 7;
double t1 = 5.35;
java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");
double mortgagefigure = getPayment(a1, r1, t1, 12);
double total = getFigure(mortgagefigure);
String output = "" + doldec.format(total);
rate.setText("");
term.setText("");
sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
}
catch (NumberFormatException error)
{
getCleanScreen();
}
}
else if (sizeNum == 2)
{
try
{
String amt = amount.getText();
double a1 = Double.parseDouble(amt);
double r1 = 15;
double t1 = 5.5;
java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");
double mortgagefigure = getPayment(a1, r1, t1, 12);
double total = getFigure(mortgagefigure);
String output = "" + doldec.format(total);
rate.setText("");
term.setText("");
sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
}
catch (NumberFormatException error)
{
getCleanScreen();
}
}
else
{
try
{
String amt = amount.getText();
double a1 = Double.parseDouble(amt);
double r1 = 30;
double t1 = 5.75;
java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");
double mortgagefigure = getPayment(a1, r1, t1, 12);
double total = getFigure(mortgagefigure);
String output = "" + doldec.format(total);
rate.setText("");
term.setText("");
sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
}
catch (NumberFormatException error)
{
getCleanScreen();
}
}
}
else if(source == resetButton)
{
getCleanScreen();
}
else
{
System.exit(0);
}
}// end actionPerformed
private void getCleanScreen() {
throw new UnsupportedOperationException("Not yet implemented");
}
private double getFigure(double mortgagefigure) {
throw new UnsupportedOperationException("Not yet implemented");
}
private double getPayment(double a1, double r1, double t1, int i) {
throw new UnsupportedOperationException("Not yet implemented");
}
}