import java.text.NumberFormat;
/*Robin Williams
* PRG/421
* Week 3, Change Request 5
* GUI for Mortgage Calculator with Array/Menu
*/
public class MortgageCalculatorJFrame extends javax.swing.JFrame {
/** Creates new form MortgageCalculatorJFrame */
public MortgageCalculatorJFrame() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
loanAmountLbl = new javax.swing.JLabel();
loanChoiceLbl = new javax.swing.JLabel();
loanAmountTxt = new javax.swing.JTextField();
loanChoiceCmb = new javax.swing.JComboBox();
calculateButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
quitButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
resultsTxtArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
loanAmountLbl.setText("Loan Amount");
loanChoiceLbl.setText("Loan Choice");
loanChoiceCmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" }));
loanChoiceCmb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loanChoiceCmbActionPerformed(evt);
}
});
calculateButton.setText("Calculate");
calculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButtonActionPerformed(evt);
}
});
clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
quitButton.setText("Quit");
quitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quitButtonActionPerformed(evt);
}
});
resultsTxtArea.setColumns(20);
resultsTxtArea.setRows(5);
jScrollPane1.setViewportView(resultsTxtArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(loanAmountLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(loanChoiceLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loanAmountTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
.addComponent(loanChoiceCmb, javax.swing.GroupLayout.Alignment.TRAILING, 0, 230, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(100, 100, 100)
.addComponent(quitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(54, 54, 54))
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(calculateButton)
.addContainerGap(377, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(50, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loanAmountLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(loanAmountTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loanChoiceLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(loanChoiceCmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calculateButton)
.addComponent(quitButton)
.addComponent(clearButton))
.addGap(34, 34, 34)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(23, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void loanChoiceCmbActionPerformed(java.awt.event.ActionEvent evt) {
}
@SuppressWarnings("empty-statement")
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
double dblPrincipal = (Double.parseDouble (loanAmountTxt.getText())); //principal amount
double [] dblInterestRate = {5.35,5.5,5.75}; //intialized the array with values(interest rate)
int []intLoanTermYear = {7,15,30}; //intialized the array with values(loan term)
double [] dblMonthlyPayment = {0,0,0}; //intialized the array with zero values(monthly payment)
double [] dblMonthlyInterestRate= {0,0,0}; //intialized the array with zero values(monthly interest rate)
int [] intLoanTermMonth = {0,0,0}; //intialized the array with zero values(loan term in months)
int box = loanChoiceCmb.getSelectedIndex();
double dblcurrentbalance = 0;
double dblcurrentmonthlyPrincipalpayment = 0;
double dblcurrentmonthlyInterestpaid = 0;
double dblcurrentmonthlyprincipal = 0;
double dblpreviousmonthlybalance = 0;
{
dblMonthlyInterestRate[box] = dblInterestRate[box]/(12 * 100); //calculate monthly interest rate per array
intLoanTermMonth[box] = intLoanTermYear[box] * 12; //calculate number of months of loan
/* Monthlypayment formula using array index values */
dblMonthlyPayment[box] = dblPrincipal * ( dblMonthlyInterestRate[box] / (1 - (Math.pow((1 + dblMonthlyInterestRate[box]),-(intLoanTermMonth[box])))));
/* Calculation formulas*/
dblcurrentmonthlyInterestpaid = (dblPrincipal * dblMonthlyInterestRate[box]); //current monthly interest
dblcurrentmonthlyPrincipalpayment = (dblMonthlyPayment[box] - dblMonthlyInterestRate[box]); //current monthlt principal payment
dblcurrentmonthlyprincipal = (dblPrincipal - dblcurrentbalance); //calculate the new balance
dblpreviousmonthlybalance = dblcurrentmonthlyprincipal;
int intlinecounter=1;
for(intlinecounter=1; intlinecounter<=intLoanTermMonth[box]; intlinecounter++)
{
// Formulas to perform calculations
dblMonthlyInterestRate[box] = dblpreviousmonthlybalance * dblMonthlyInterestRate[box];
dblcurrentbalance = dblMonthlyPayment[box] - dblcurrentmonthlyInterestpaid;
dblcurrentmonthlyprincipal = dblpreviousmonthlybalance - dblcurrentbalance;
dblpreviousmonthlybalance = dblcurrentmonthlyprincipal;
//Print out Monthly Payment Amount
NumberFormat CurrencyFormat = NumberFormat.getCurrencyInstance();
resultsTxtArea.append("The Loan Principal is " +(CurrencyFormat.format (dblPrincipal))+ "." + "\n"+"The loan's interest rate is" +""+"" + dblInterestRate[box] +"%." + "\n"+"The loan term is"+""+intLoanTermYear[box]+""+ "years."+"\n"+"The monthly payment will be"+""+(CurrencyFormat.format (dblMonthlyPayment[box])));
if(intlinecounter % 20 == 0)
{ intlinecounter=1;
System.out.println("Press any key to continue");
try
{ System.in.read();
}catch (Exception e){;
}
}
}}
}
private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
loanAmountTxt.setText(null);
loanChoiceCmb.setSelectedIndex(-1);
resultsTxtArea.setText(null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MortgageCalculatorJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calculateButton;
private javax.swing.JButton clearButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel loanAmountLbl;
private javax.swing.JTextField loanAmountTxt;
private javax.swing.JComboBox loanChoiceCmb;
private javax.swing.JLabel loanChoiceLbl;
private javax.swing.JButton quitButton;
private javax.swing.JTextArea resultsTxtArea;
// End of variables declaration
}