Im creating a calculator that allows users to enter their own Loan Amount, then choose whether to input their own Loan Interest and Monthly Loan Terms, or to choose between a set of predefined loan terms and interest rate in a GUI.
So far I have been successful in creating the GUI and able to get the combo box option to calculate.
The issue Im now having is getting the the user input for the loan terms and loan interest to work. Im guessing that I need to somehow write and "if or else" statement in order to get the to different calculations to work, as well Im not sure how I would factor this into my Amortization output area.
Any and all help, or tips are greatly appreciated. I dont expect you to write the code for me, though an example of how to go about accomplishing this concept to just point me in the right area would also help as Im a good visual learner.
//as well I have inlcuded my requirements for this program incase my illusrtration of what is needed was not clear enough
Write the program in Java (with a graphical user interface) so that it will allow the user to select which way they want to calculate a mortgage:
1. By input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment
2. Or by input of the amount of a mortgage and then select from a menu of mortgage loans:
- 7 year at 5.35%
- 15 year at 5.5 %
- 30 year at 5.75%
public class MortgageCalculator extends javax.swing.JFrame {
private int[] terms = new int[] {7,15,30};
private double[] interestRates = new double[] {5.35, 5.5, 5.75};
private double calcMonthlyPayment (double loanAmount,
double interest, double term) {
double mir = interest / 12 / 100;
double tim = term * 12;
return loanAmount * mir / (1 - (Math.pow(( 1 + mir ), (-tim))));
}
public MortgageCalculator() {
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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
prpT = new javax.swing.JTextField();
optButt = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
calcButt = new javax.swing.JButton();
quitButt = new javax.swing.JButton();
clearButt = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
outF = new javax.swing.JTextArea();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
lIntB = new javax.swing.JTextField();
lTermB = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setLabelFor(prpT);
jLabel1.setText("Loan Principale");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setLabelFor(optButt);
jLabel2.setText("Loan Options");
prpT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
prpTActionPerformed(evt);
}
});
optButt.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 year at 5.35%", "15 years at 5.5%", "30 years at 5.75%"}));
optButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
optButtActionPerformed(evt);
}
});
jLabel3.setBackground(new java.awt.Color(255, 255, 255));
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Mortgage Calculator");
jLabel3.setBorder(new javax.swing.border.MatteBorder(null));
calcButt.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
calcButt.setText("Calculate");
calcButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calcButtActionPerformed(evt);
}
});
quitButt.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
quitButt.setText("Quit");
quitButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quitButtActionPerformed(evt);
}
});
clearButt.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
clearButt.setText("Clear");
clearButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtActionPerformed(evt);
}
});
outF.setColumns(20);
outF.setRows(5);
jScrollPane1.setViewportView(outF);
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setLabelFor(lIntB);
jLabel4.setText("Interest");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel5.setLabelFor(lTermB);
jLabel5.setText("Loan Term");
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel7.setText("Or Choose an Option Below");
jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel8.setText("Enter Loan Intrest and Loan Term ");
lIntB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lIntBActionPerformed(evt);
}
});
lTermB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lTermBActionPerformed(evt);
}
});
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(33, 33, 33)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(94, 94, 94)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(calcButt)
.addGap(76, 76, 76)
.addComponent(clearButt)
.addGap(96, 96, 96)
.addComponent(quitButt))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(127, 127, 127)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(optButt, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(prpT, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lIntB, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lTermB, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(174, 174, 174)
.addComponent(jLabel8))
.addGroup(layout.createSequentialGroup()
.addGap(94, 94, 94)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(153, 153, 153))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(prpT))
.addGap(18, 18, 18)
.addComponent(jLabel8)
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(lIntB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(lTermB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(optButt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calcButt)
.addComponent(clearButt)
.addComponent(quitButt))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void optButtActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void quitButtActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void clearButtActionPerformed(java.awt.event.ActionEvent evt) {
prpT.setText(null); //clears Loan Princaple
outF.setText(null); // Clears Output area
optButt.setSelectedIndex(-1); // Clears Loan Type Selection Field
}
private void prpTActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void calcButtActionPerformed(java.awt.event.ActionEvent evt) {
// Gather Values From User Input
double loanAmount;
try {
loanAmount = Double.parseDouble(prpT.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid Amount");
return;
}
int loanOption = optButt.getSelectedIndex();
if (loanOption == -1)
{
JOptionPane.showMessageDialog(null, "Select a Loan Option");
return;
}
int term = terms [loanOption];
double interest = interestRates[loanOption];
//Monthly Payment Method Invoke
double monthlyPayment = calcMonthlyPayment (loanAmount, interest, term);
//Amortiztion Table
StringBuilder table = new StringBuilder();
int months = term * 12;
double monthlyInterestRate = interest / 12.0 / 100.0;
double balance = loanAmount;
// Format Decimals
DecimalFormat money = new DecimalFormat("$#,##0.00");
for (int month = 1; month <= months; month ++)
{
table.append(month + "\t");
double interestPaid = balance * monthlyInterestRate;
double principalPaid = monthlyPayment - interestPaid;
balance -= principalPaid;
//Displaying Balance
table.append(money.format(balance) + "\t\t");
//interest Displayed
table.append(money.format(interestPaid) + "\t\t");
//Principal Displayed
table.append(money.format (principalPaid) + "\n");
}
StringBuilder results = new StringBuilder();
results.append ("Monthly Payment: " + money.format(monthlyPayment) + "\n\n");
results.append("month\tBalance\t\tInterest Paid\t\tPrincipal Paid\n");
results.append(table);
outF.setText(results.toString());
// TODO add your handling code here:
}
private void lIntBActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void lTermBActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MortgageCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MortgageCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MortgageCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MortgageCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MortgageCalculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calcButt;
private javax.swing.JButton clearButt;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField lIntB;
private javax.swing.JTextField lTermB;
private javax.swing.JComboBox optButt;
private javax.swing.JTextArea outF;
private javax.swing.JTextField prpT;
private javax.swing.JButton quitButt;
// End of variables declaration
}