import java.text.DecimalFormat;
import javax.swing.JComboBox;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* GUIForm.java
*
* Created on Sep 14, 2010, 7:16:57 AM
*/
/**
*
* @author Connie's Desktop
*/
public class GUIForm extends javax.swing.JFrame {
private static class RateTerm {
public RateTerm() {
}
}
/** Creates new form GUIForm */
public GUIForm() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPrincipleLabel = new javax.swing.JLabel();
RateTerm = new javax.swing.JComboBox();
jTxtPrinciple = new javax.swing.JTextField();
jPaymentLabel = new javax.swing.JLabel();
jCalculateButton = new javax.swing.JButton();
jClearButton = new javax.swing.JButton();
jExitButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTxtMonthlyPayment = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Mortgage Calculator");
jPrincipleLabel.setText("Principle");
RateTerm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" }));
RateTerm.setBorder(new javax.swing.border.MatteBorder(null));
RateTerm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RateTermActionPerformed(evt);
}
});
jTxtPrinciple.setBorder(null);
jTxtPrinciple.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTxtPrincipleActionPerformed(evt);
}
});
jPaymentLabel.setText("Monthly Payment");
jCalculateButton.setText("Calculate");
jCalculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCalculateButtonActionPerformed(evt);
}
});
jClearButton.setText("Clear");
jClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jClearButtonActionPerformed(evt);
}
});
jExitButton.setText("Exit");
jExitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jExitButtonActionPerformed(evt);
}
});
jTxtMonthlyPayment.setColumns(20);
jTxtMonthlyPayment.setRows(5);
jTxtMonthlyPayment.setBorder(null);
jScrollPane1.setViewportView(jTxtMonthlyPayment);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jPrincipleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPaymentLabel)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(RateTerm, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTxtPrinciple, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)))
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jExitButton)
.addComponent(jClearButton)
.addComponent(jCalculateButton))))
.addContainerGap(54, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jPrincipleLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTxtPrinciple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addComponent(RateTerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jPaymentLabel)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jCalculateButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addComponent(jClearButton)
.addGap(28, 28, 28)
.addComponent(jExitButton))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
.addGap(26, 26, 26))
);
pack();
}// </editor-fold>
private void RateTermActionPerformed(java.awt.event.ActionEvent evt) {
//Declare variables
String firstString = "7 Years at 5.35%";
String secondString = "15 Years at 5.5%";
String thirdString = "30 Years at 5.75%";
}
private void jCalculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Create an array for the interest rate
double[] interestRate = new double[3];
interestRate[0] = 5.35;
interestRate[1] = 5.5;
interestRate[2] = 5.75;
// Create an array for the number of years
int[] numberOfYears = new int[3];
numberOfYears[0] = 7;
numberOfYears[1] = 15;
numberOfYears[2] = 30;
{
// Define varibles
int X = RateTerm.getSelectedIndex();
while (X<3)
{
double P = 200000; //Principle
double R = 0.0; //monthly interest rate
double M = 0.0; // Number of months for the loan
double N = 0.0; //Monthly Payment
//create object to convert values to dollars and cents
DecimalFormat cash = new DecimalFormat("$0.00");
//calculate monthly interest rate
R = interestRate[X] / (12 * 100);
//calculate total number of months for loan
M = numberOfYears[X] * 12;
//Calculate Monthly Payment
N = (P * (R * (Math.pow((1 + R), M)) / (Math.pow((1 + R), M) - 1)));
this.jTxtMonthlyPayment.setText(+ interestRate[X] + "% for " + numberOfYears[X] + " years is "+ cash.format(N));
X = X + 1;
}
}
}
private void jClearButtonActionPerformed(java.awt.event.ActionEvent evt) {
// making the clear button function
jTxtPrinciple.setText("");
jTxtMonthlyPayment.setText("");
}
public JComboBox getRateTerm() {
return RateTerm;
}
public void setRateTerm(JComboBox RateTerm) {
this.RateTerm = RateTerm;}
private void jExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jTxtPrincipleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUIForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox RateTerm;
private javax.swing.JButton jCalculateButton;
private javax.swing.JButton jClearButton;
private javax.swing.JButton jExitButton;
private javax.swing.JLabel jPaymentLabel;
private javax.swing.JLabel jPrincipleLabel;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTxtMonthlyPayment;
private javax.swing.JTextField jTxtPrinciple;
// End of variables declaration
}