/* Dwight Welsh
GUI Java Mortgage Calculation Program
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
public class Mortgage extends JFrame
{
// define window width and height
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 800;
// define text field for amount of the mortgage
private JTextField amountText;
// define text area to display mortgage payment amount followed
// by the loan balance and interest paid for each payment over
// the term of the loan
private JTextArea outputTextArea;
// term (in years) array
private int[] terms = {7, 15, 30};
// annual interest rate (%) array
private double[] rates = {5.35, 5.50, 5.75};
// constructor
public Mortgage()
{
super("Mortgage Payment Calculator");
Container container = getContentPane();
// set the Border Layout
container.setLayout(new BorderLayout());
// create label
JLabel label = new JLabel("Amount of the mortgage: ");
// create text field
amountText = new JTextField(10);
// create menu
JMenuBar menuBar = new JMenuBar();
// Mortgage Loans menu
JMenu loanMenu = new JMenu("Loan Terms Menu");
menuBar.add(loanMenu);
// add menu items
JMenuItem term7MenuItem = new JMenuItem("7 year at 5.35%");
JMenuItem term15MenuItem = new JMenuItem("15 year at 5.5%");
JMenuItem term30MenuItem = new JMenuItem("30 year at 5.75%");
JMenuItem quitMenuItem = new JMenuItem("Quit");
loanMenu.add(term7MenuItem);
loanMenu.add(term15MenuItem);
loanMenu.add(term30MenuItem);
loanMenu.addSeparator();
loanMenu.add(quitMenuItem);
setJMenuBar(menuBar);
// create panel
JPanel panel = new JPanel(new FlowLayout());
// add label and text field
panel.add(label);
panel.add(amountText);
// add panel at north
container.add(panel, BorderLayout.NORTH);
// create output text area and make it not editable
// and set font as "Courier New"
outputTextArea = new JTextArea();
outputTextArea.setEditable(false);
outputTextArea.setFont(new Font("Courier New", Font.PLAIN, 12));
// and output text area at the center
container.add(new JScrollPane(outputTextArea), BorderLayout.CENTER);
// 7 years at 5.35% loan menu item click event listener
term7MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayLoan(terms[0], rates[0]);
}
});
// 15 years at 5.5% loan menu item click event listener
term15MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayLoan(terms[1], rates[1]);
}
});
// 30 years at 5.75% loan menu item click event listener
term30MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayLoan(terms[2], rates[2]);
}
});
// quit menu item click event listener
quitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
}
// Display the mortgage payment amount followed by the loan balance and
// interest paid for each payment over the term of the loan.
public void displayLoan(int term, double rate)
{
double amount; // loan amount
int years; // number of years
int months; // total months
double monthlyRate; // monthly interest rate
double monthlyPayment; // monthly mortgage payment amount
NumberFormat nf = NumberFormat.getCurrencyInstance();
double loanBalance;
double interestPaid;
try
{
amount = Double.parseDouble(amountText.getText());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Amount must be numeric", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (amount <= 0)
{
JOptionPane.showMessageDialog(null, "Amount must be positive", "Amount", JOptionPane.ERROR_MESSAGE);
return;
}
// calculate total months
months = term * 12;
// calculate monthly interest rate
monthlyRate = rate / 1200;
// calculate monthly mortgate payment amount
monthlyPayment = (amount * monthlyRate) / (1.0 - (Math.pow(1.0 + monthlyRate, -months)));
// set mortgage payment amount in output text area
outputTextArea.setText(String.format("Monthly payment amount: %s\n\n", nf.format(monthlyPayment)));
// append header for Month, Loan Balance, and Interest Paid
outputTextArea.append(String.format("%10s %20s %20s\n", "Month", "Loan Balance", "Interest Paid"));
outputTextArea.append(String.format("%10s %20s %20s\n", "-----", "------------", "-------------"));
// for each month, display Month, Loan Balance, and Interest Paid
loanBalance = amount;
for (int month = 1; month <= months; month++)
{
// calculate monthly interest paid
interestPaid = loanBalance * monthlyRate;
// calculate loan Balance
loanBalance = loanBalance - monthlyPayment + interestPaid;
outputTextArea.append(String.format("%10d %20s %20s\n", month,
nf.format(loanBalance), nf.format(interestPaid)));
}
}
public static void main(String args[])
{
// create Mortgage class object
Mortgage mortgage = new Mortgage();
// set size of window
mortgage.setSize(FRAME_WIDTH, FRAME_HEIGHT);
// make window exit when closed
mortgage.setDefaultCloseOperation(EXIT_ON_CLOSE);
// make location of window at center of screen
mortgage.setLocationRelativeTo(null);
// make window not resizable
mortgage.setResizable(false);
// make window visible
mortgage.setVisible(true);
}
}