Java Program ll

March 29, 2010 at 15:01:18
Specs: Linux i686
/*
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.

Designed by Jason Vaillancourt
3/25/2010
*/

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 // 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(""+output+"");
}
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(""+output+"");
}
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(""+output+"");
}
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(""+output+"");
}
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");
}
}

It runs but there are errors. I think I need a formula to cal the payment and there are exception handling errors that come up that prevents program from running properly. Can Any HELP PLEASE!


See More: Java Program ll

Report •


#1
March 29, 2010 at 15:52:09
when i run the program in command line the GUI pops up. I am allowed to enter a mortgage amount, but when i click on the drop down list for a rate/term it doesn't coordinate properly with the appropriate field. I know after i get this to work i'm going to need to have to implement a formula to calculate the payment along with interest for each payment. However, i need to work on one problem at a time. Any assistance would be appreciated. Thanks.

Report •
Related Solutions


Ask Question