Gui Mortgage Calculator - Basic

Only available on StudyMode
  • Download(s) : 100
  • Published : September 7, 2009
Open Document
Text Preview
/*
* Author:
* Class:
* File Name:
* Assignment:
*
* This program allows a user to calculate a mortgage payment based on their input. * The input consists of the loan amount, length of the loan, and the interest rate. */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;

// Sets the Mortgage1 class which allows a user to calculate a mortgage loan

public class Mortgage1 extends JFrame {

// Creates the buttons that will be used
private JButton calcButton = new JButton("Calculate");
private JButton resetButton = new JButton("Reset");

// Creates the labels for the fields to be used
private final JLabel amountLabel = new JLabel("Loan Amount:");
private final JLabel termLabel = new JLabel("Term (in years):");
private final JLabel rateLabel = new JLabel("Rate (actual %):");
private final JLabel mpayLabel = new JLabel("Monthly Payment:");

// Creates the text fields for the user input
private JTextField amountField;
private JTextField termField;
private JTextField rateField;
private JTextField mpayField;

// Adds the title to the window and defines window diminsions
public Mortgage1() {
super("PRG/421 Mortgage Calculator");
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(275, 175);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);

MenuBar mnuBar = new MenuBar();
setMenuBar(mnuBar);

Menu mnuFile = new Menu("File", true);
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
mnuFile.add(mnuFileExit);

Menu mnuHelp = new Menu("Help", true);
mnuBar.add(mnuHelp);
MenuItem mnuAbout = new MenuItem("About");
mnuHelp.add(mnuAbout);

mnuFileExit.addActionListener(new ExitMenuHandler());

//mnuFileExit.setActionCommand("Exit");

}

// Creates the actual buttons and fields in a usable way
public void initComponents() {
JPanel jp;

jp = new JPanel(new GridLayout(4, 4));

jp.add(amountLabel);
amountField = new JTextField("");
amountField.setEditable(true);
jp.add(amountField);

jp.add(termLabel);
termField = new JTextField("");
termField.setEditable(true);
jp.add(termField);

jp.add(rateLabel);
rateField = new JTextField("");
rateField.setEditable(true);
jp.add(rateField);

jp.add(mpayLabel);
mpayField = new JTextField("");
mpayField.setEditable(false);
jp.add(mpayField);

getContentPane().add(jp, BorderLayout.CENTER);

jp = new JPanel(new GridLayout());

jp.add(calcButton);
calcButton.addActionListener(new CalcButtonHandler());

jp.add(resetButton);
resetButton.addActionListener(new ResetButtonHandler());

getContentPane().add(jp, BorderLayout.SOUTH);

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

}

public static void main(String[] args) {
Mortgage1 frame = new Mortgage1();
}

// Defines what the Calculate button should do for its action
class CalcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
double amount = Double.parseDouble(amountField.getText());
double term = Integer.parseInt(termField.getText())*12;
double rate = Double.parseDouble(rateField.getText()) /

100 / 12.;
double mpay = amount * ( rate * Math.pow ( ( 1 + rate ),

term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
DecimalFormat df = new DecimalFormat("$###,###.00");
mpayField.setText(df.format(mpay));
}
catch (Exception ex) {
System.out.println(ex);
}
}
} // Ends the CalcButtonHandler class

// Defines what the Reset button should do for its action.
class ResetButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
amountField.setText("");
termField.setText("");...
tracking img