import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
import java.util.logging.ConsoleHandler;
public class MortgageCalculator{
public static Container container;
public static class week2MortCal extends JPanel
implements PropertyChangeListener {
private static final long serialVersionUID = 1L;
//*input for each field
private double amount = 200000;
private double rate = .0575; //5.75%
private int numPeriods = 30;
//*Lets you know what the fields are
private JLabel amountLabel;
private JLabel rateLabel;
private JLabel numPeriodsLabel;
private JLabel paymentLabel;
//*Strings for each input
private static String amountString = "Loan Amount: ";
private static String rateString = "APR (%): ";
private static String numPeriodsString = "Years: ";
private static String paymentString = "Monthly Payment: ";
//*input fields
private JFormattedTextField amountField;
private JFormattedTextField rateField;
private JFormattedTextField numPeriodsField;
private JFormattedTextField paymentField;
//*Formats for the calculation
private NumberFormat amountFormat;
private NumberFormat percentFormat;
private NumberFormat paymentFormat;
private double temp1;
//*Adds buttons to the field
public static JButton CalculateButton;
public static JButton ClearButton;
public static JButton ExitButton;
private Container content;
public JLabel temp;
public week2MortCal() {
super(new BorderLayout());
setUpFormats();
double payment = calculatePayment(amount, rate, numPeriods);
//*Makes the labels.
amountLabel = new JLabel(amountString);
rateLabel = new JLabel(rateString);
numPeriodsLabel = new JLabel(numPeriodsString);
paymentLabel = new JLabel(paymentString);
//*Makes text fields.
amountField = new JFormattedTextField(amountFormat);
amountField.setValue(new Double(amount));
amountField.setColumns(10);
amountField.addPropertyChangeListener("value", this);
rateField = new JFormattedTextField(percentFormat);
rateField.setValue(new Double(rate));
rateField.setColumns(10);
rateField.addPropertyChangeListener("value", this);
numPeriodsField = new JFormattedTextField();
numPeriodsField.setValue(new Integer(numPeriods));
numPeriodsField.setColumns(10);
numPeriodsField.addPropertyChangeListener("value", this);
paymentField = new JFormattedTextField(paymentFormat);
paymentField.setValue(new Double(payment));
paymentField.setColumns(10);
paymentField.setEditable(false);
paymentField.setForeground(Color.blue);
//*Information on the labels
amountLabel.setLabelFor(amountField);
rateLabel.setLabelFor(rateField);
numPeriodsLabel.setLabelFor(numPeriodsField);
paymentLabel.setLabelFor(paymentField);
//*arranges labels in the panel.
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(amountLabel);
labelPane.add(rateLabel);
labelPane.add(numPeriodsLabel);
labelPane.add(paymentLabel);
//*arranges the text labels in the panel .
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(amountField);
fieldPane.add(rateField);
fieldPane.add(numPeriodsField);
fieldPane.add(paymentField);
//*Put the labels on left,
//*text fields on right.
setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
//* places content in frame.
content.add(CalculateButton);
content.add(ClearButton);
content.add(ExitButton);
//* controls the calculation button actions
ConsoleHandler handlerC = new ConsoleHandler();
CalculateButton.addActionListener((ActionListener) handlerC);
//*controls the clear button actions
ClearHandler handlerD = new ClearHandler();
ClearButton.addActionListener((ActionListener) handlerD);
}
/** Used when a field's "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == amountField) {
amount = ((Number)amountField.getValue()).doubleValue();
} else if (source == rateField) {
rate = ((Number)rateField.getValue()).doubleValue();
} else if (source == numPeriodsField) {
numPeriods = ((Number)numPeriodsField.getValue()).intValue();
}
double payment = calculatePayment(amount, rate, numPeriods);
paymentField.setValue(new Double(payment));
}
private static void createAndShowGUI() {
//*Makes the window.
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//*Allows you to add input in window
frame.add(new week2MortCal());
CalculateButton = new JButton("Calculate");
ClearButton = new JButton("Clear");
ExitButton = new JButton("Exit");
CalculateButton.addActionListener(null);
ClearButton.addActionListener(null);
ExitButton.addActionListener(null);
//*Shows the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
//*Calculates the monthly payment based on the loan amount,
//*Interest rate, and length of loan.
public static double calculatePayment(double principle, double annRate, int years){
double monthlyInt = annRate / 12;
double monthlyPayment = (principle * monthlyInt)
/ (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Calculates 1 monthly payment times by 12 for yearly cost.
return format(monthlyPayment, 2);
}
public static double format(double amount, int mortgage) {
double temp = amount;
temp = temp * Math.pow(10, mortgage);
temp = Math.round(temp);
temp = temp/Math.pow(10, mortgage);
return temp;
}
private void setUpFormats() {
amountFormat = NumberFormat.getNumberInstance();
percentFormat = NumberFormat.getNumberInstance();
percentFormat.setMinimumFractionDigits(3);
paymentFormat = NumberFormat.getCurrencyInstance();
}
public double getTemp() {
return temp1;
}
public void setTemp(double temp) {
this.temp1 = temp;
}
}
};