import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.lang.*;
import java.text.*;
import java.util.*;
public class Mortgage extends javax.swing.JApplet implements ActionListener {
/*Create Arrays*/
double[] annualInterests = {5.35,5.5,5.75}; //Interest Array
int[] numOfYears = {7,15,30}; //Term Array
// Declare variables
double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount;
double principal;
double interest;
int term, i;
float paymentdue; /*The variable used for user output for exact payment due information*/
//Constructing Labels for GUI
JLabel title = new JLabel(" Mortgage Calculator ");
JLabel title = new JLabel(" Please enter Principal amount and select term with rate ");
JTextField appPrincipal=new JTextField(10); // Principle Loan Amount
JTextField appTerm=new JTextField(2); // Term in Years "00"
JTextField appInterest=new JTextField(5); // Interest %
JTextField appMonthlyPayment=new JTextField(10);
JLabel ErrorMsg=new JLabel("");
Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
//* Constructing Applet Container
public void init()
{
Container con = getContentPane();
con.setLayout(new BorderLayout());
JPanel TopLabel=new JPanel();
TopLabel.setLayout(new FlowLayout());
title.setFont(bigFont);
con.add("North",title);
// Initialize the applet by setting it to use blue
// and yellow as background and foreground colors.
setBackground(Color.blue);
setForeground(Color.yellow);
JPanel MainForm=new JPanel();
MainForm.setLayout(new GridLayout(0,3));
JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT);
MainForm.add(label1);
JButton bttn = new JButton(" 7 Years at 5.35% ");
bttn.addActionListener( new ActionListener()) { // Created an "action listener" for the button which is defined by this anonymous class.
}
public void actionPerformed(ActionEvent evt) {
// This method will respond to the user's click entry for the mortgage term & interest
i = 0;
MainForm.add(i);
}
JButton bttn = new JButton(" 15 Years at 5.5% ");
bttn.addActionListener( new ActionListener()) {
}
public void actionPerformed(ActionEvent evt) {
i = 1;
MainForm.add(i);
}
JButton bttn = new JButton(" 30 Years at 5.75% ");
bttn.addActionListener( new ActionListener()) {
}
public void actionPerformed(ActionEvent evt) {
i = 2;
MainForm.add(i);
}
JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT);
MainForm.add(label2);
MainForm.add(appPrincipal);
JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT);
MainForm.add(label3);
MainForm.add(appInterest);
JLabel label4=new JLabel("Result ",SwingConstants.RIGHT);
MainForm.add(label4);
MainForm.add(appMonthlyPayment);
con.add("West",MainForm);
JPanel ButtonPanel=new JPanel();
ButtonPanel.setLayout(new BorderLayout());
ButtonPanel.add("North",ErrorMsg);
JButton Calculate=new JButton("Calculate");
//add an action listener
Calculate.addActionListener(this);
ButtonPanel.add("Center",Calculate);
con.add("South",ButtonPanel);
appMonthlyPayment.setEditable(false);
}
public void start()
{
repaint();
}
//Ask For User Input And Divide Input
public void actionPerformed(ActionEvent divide)
{
DecimalFormat decimalPlaces=new DecimalFormat("0.00");
appMonthlyPayment.setText("");
// The next 3 lines get the values from the text boxes.
term = Integer.parseInt(appTerm.getText());
interest = Double.parseDouble(appTerm.getText());
principal = Double.parseDouble(appPrincipal.getText());
//Calculate Monthly Interest
monthlyInterest = annualInterests[i]/(12*100);
int totalNumOfMonths = numOfYears[i]*12;
int months= term*12; //The variable used for user input for exact month information
monthlyInterest = interest/(12 * 100); //calculate the monthly interst using simple interest.
denominator = Math.pow(1 + monthlyInterest, -(term*12));
denominator = 1 - denominator;
monthlyPayment = principal * (monthlyInterest / denominator);
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;
//calculate the monthly interst using simple interest.
double getMonthlyPrincipal, monthlyPayment, remainingPrincipal;
return monthlyPayment - (remainingPrincipal * monthlyInterest);
// Finally, set the text field to show the result
appMonthlyPayment.setText("Payment = " + monthlyPayment);
public void actionPerformed(ActionEvent e)
{
try
{ // Capture the Info the User Entered, remove 's and make it a double
MortgageInfo.setInrestRatePercent(Double.parseDouble(IntrestBox.getText().replaceAll(",","")));
MortgageInfo.setloanAmount(Double.parseDouble(MortgageBox.getText().replaceAll(",","")));
MortgageInfo.setloanYears(Double.parseDouble(TermBox.getText().replaceAll(",",""))); // Get & Format the Results
double test=MortgageInfo.getpaymentAmount();
DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
MonthlyPayment.setText("<html><font color=blue>"+myFormatter.format(test)+"</font></html>");
}
catch(Exception except)
{
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true); // Make it pretty
JFrame frame=new briano_week2(); // Create a new Frame
frame.setSize(250,180); // Set the Size of the Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the User to Quit
frame.setVisible(true);
}
} //End Program