package week5;
/*
*
* @author Chad McCoy
*/
import java.awt.*; //these import everything needed for use in program
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MortgageCalc2 extends JFrame { //class definition
GridBagConstraints gbc = new GridBagConstraints();
private void set_gbc(int row, int column, int width, int height, int fill, int v, int h) {
gbc.gridy = row;
gbc.gridx = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.fill = fill;
gbc.ipady = v;
gbc.ipadx = h;
}
JLabel firstNumLabel = new JLabel("Mortgage Principal:"); //this section establishes variables
JTextField firstNumField = new JTextField("0");
JLabel secondNumLabel = new JLabel("Mortgage Terms (in years):");
JTextField secondNumField = new JTextField("0");
JLabel thirdNumLabel = new JLabel("Annual Percentage Rate (APR)");
JTextField thirdNumField = new JTextField("0");
JLabel resultLabel = new JLabel("Payment Amount:");
JTextField resultField = new JTextField("0");
JButton calcButton = new JButton("Calculate Payment");
JButton closeButton = new JButton("Close");
JButton resetButton = new JButton("Reset Calculator");
JButton startOvrButton = new JButton("Start Over");
public int pymtAmt = 0;
public double intRate = 0;
public int monMorTerms = 0;
JTextArea outputArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(outputArea);
JTextArea headingArea = new JTextArea();
JScrollPane scrollPane2 = new JScrollPane(headingArea);
private final static String newLine = "\n";
String addLine2 = ("Pmt#" + "\t" + "Pmt Amt" + "\t" + "Loan Bal" + "\t" + "Int Paid");
public MortgageCalc2() { //constructor for object of this class
super("Mortgage Calculator"); //this makes a title for the window
setLocation(500, 200); //this sets the location of the window
setSize (360,500); //sets the window size
setResizable(true); //allows user to resize window
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //the window will be disposed of, but program will still be running
setLayout(new GridBagLayout()); //defines window layout
this.addWindowListener(new WindowAdapter() { /*this allows me to provide an option if the window is closed,
serves to ensure user wants to close the window*/
public void windowClosed(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, "Are you sure you wish to close the Week 5 Mortgage Calculator?",
"Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
set_gbc(0, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10); //establishes contraints for gridbag
add(firstNumLabel,gbc); //these lines add the elements to the window
set_gbc(0, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(firstNumField,gbc);
set_gbc(1, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(secondNumLabel,gbc);
set_gbc(1, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(secondNumField,gbc);
set_gbc(2, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(thirdNumLabel,gbc);
set_gbc(2, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(thirdNumField,gbc);
set_gbc(3, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(resultLabel,gbc);
set_gbc(3, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(resultField,gbc);
set_gbc(4, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(calcButton,gbc);
set_gbc(4, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(closeButton,gbc);
set_gbc(5, 0, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(resetButton,gbc);
set_gbc(5, 1, 1, 1, GridBagConstraints.HORIZONTAL, 10, 10);
add(startOvrButton,gbc);
set_gbc(6,0,2,2, GridBagConstraints.HORIZONTAL, 10, 10);
add(scrollPane2,gbc);
set_gbc(8, 0, 2, 2, GridBagConstraints.BOTH, 10, 10);
gbc.weightx = 5.0;
gbc.weighty = 5.0;
add(scrollPane,gbc);
headingArea.append (addLine2 + newLine);
outputArea.setEditable(false); //text area for amortization table cannot be edited by user
resultField.setEditable(false); //this indicates the result field can not be edited by the user
closeButton.addActionListener( //defines what the close button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (JOptionPane.showConfirmDialog(null, "Are you sure you wish to close the Week 5 Mortgage Calculator?",
"Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
calcButton.addActionListener( //defines what the calculate button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String firstNumText = firstNumField.getText();
String secondNumText = secondNumField.getText();
String thirdNumText = thirdNumField.getText();
double firstNum = Double.parseDouble(firstNumText);
double secondNum = Double.parseDouble(secondNumText);
double thirdNum = Double.parseDouble(thirdNumText);
double intRate = thirdNum / 100 / 12;
double monMorTerms = secondNum * 12;
double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
NumberFormat nf = NumberFormat.getCurrencyInstance();
resultField.setText("" + nf.format(result));
int paymentNum = 0; //initializes variables for while loop
double princLeft = firstNum;
double totalPaid = 0;
double intPaid = 0;
double princPaid = 0;
while (princLeft > 0) { //loop to calculate amortization table
++paymentNum;
totalPaid = (totalPaid + result);
intPaid = (intRate * princLeft);
princPaid = (result - intPaid);
princLeft = (princLeft - princPaid);
String addLine = (paymentNum + "\t" + nf.format(result) + "\t" + nf.format(princLeft) + "\t" + nf.format(intPaid));
outputArea.append (addLine + newLine);
}
}
});
resetButton.addActionListener(new ActionListener() { //defines what the reset button does
public void actionPerformed(ActionEvent e) {
firstNumField.setText("0");
secondNumField.setText("0");
thirdNumField.setText("0");
resultField.setText("");
outputArea.setText("");
}
});
startOvrButton.addActionListener( //defines what the close button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setVisible(false); //cleans up the calculator windowmakes the selection window visible
new MortgageCalculator().setVisible(true); //makes the selection window visible
}
});
setVisible(true); //ensures user can see window created
}
}