I am having an issue with my program. If I select one of the three calc buttons it works fine and shows the amort table correctly. If I put information in the text fields for amount, interest, and years and hit calculate button I get little boxes in the amort table. I have been looking at it for a while and changing things to see if it looks different but it just won't work. Here is my code:
/*
Week 3 Assignment: McBride Financial Services Mortgage Calculator
Programmer: Robin Spencer
Filename: RSpencerMortgageCalcV2.java
Date: August 31, 2009
Purpose: This program is for change request 5 to take the amount from user input and then have the user select from three different loan payments.
Cited Source: http://www.dreamincode.net/forums/showforum32.htm
*/
// import of the packages
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RSpencerMortgageCalculatorV2
{
public static void main (String[] args)
{
JFrame window = new MortCalculation();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
} // end of class
class MortCalculation extends JFrame implements ActionListener
{
//delcare function variables
protected JButton button7, button15, button30, buttonClear, buttonQuit, buttonCalc;
protected JLabel labelMonthlyPay, labelAmount, labelTitle, labelAmortTable, labelTerm, labelRate;
protected JTextField fieldPayment, fieldamount, fieldyears, fieldinterest;
protected JTextArea areaAmortTable;
// declare variables and array and initialize the values
double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount, interest;
int[] loanYears = {7,15,30};
double[] intRate = {5.35, 5.50, 5.75};
double loanAmt = loanAmount;
int years;
public void actionPerformed (ActionEvent e)
{
// perform based on which button was pressed
if ("Calculate7".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment7();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
// need to set loan amount to the value from the field first which will need to be done for each loan calculations
loanAmt = Double.parseDouble(fieldamount.getText());
for(int x = 1; x <=(loanYears[0]*12); x++) // loop through all monthly payment for each loan
{
Balance=loanAmt; // set balance to loan amount
// calculations on monthly payment
InterestPaid = (((intRate[0]/100.0)/12.0)*Balance); // monthly interest paid
PrinciplePaid = (paymentAmount-InterestPaid); // applied towards principal
loanAmt=(Balance-PrinciplePaid); // loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
} // end for
} // end first if for button pressed
else if ("Calculate15".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment15();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldamount.getText());
for (int x = 1; x <=(loanYears[1]*12); x++) // loop through all monthly payment for each loan
{
Balance = loanAmt; // set balance to loan amount
// calculations for monthly payment
InterestPaid=((intRate[1]/100/12)*Balance);
PrinciplePaid=(paymentAmount-InterestPaid);
loanAmt=((Balance-PrinciplePaid));
areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
} // end for loop
} // end else if
else if ("Calculate30".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment30();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldamount.getText());
for(int x = 1; x <=(loanYears[2]*12); x++) // loop through all monthly payment for each loan
{
Balance = loanAmt; // set balance to loan amt
// calculations for monthly payment
InterestPaid = ((intRate[2]/100/12)*Balance);
PrinciplePaid = (paymentAmount-InterestPaid);
loanAmt=((Balance-PrinciplePaid));
areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
} // end for loop
} // end else if
else if("Calc".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
loanAmt = Double.parseDouble(fieldamount.getText());
int years = Integer.parseInt(fieldyears.getText());
double interest = Double.parseDouble(fieldinterest.getText());
double paymentAmount = Calc();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
for(int x = 1; x <=(years*12); x++)
{
Balance=loanAmt;
InterestPaid = (((interest/100.00)/12.0)*Balance);
PrinciplePaid = (paymentAmount-InterestPaid);
loanAmt = (Balance-PrinciplePaid);
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n");
}
}
else if ("Clear".equals(e.getActionCommand()))
{
ClearFields(); // action command for reset button
}
else if ("Quit".equals(e.getActionCommand()))
{
System.exit(0); // action command for quit button
}
}
public double CalculatePayment7()
{
// check for valid numbers
try
{
// perform calculations if input is valid
fieldPayment.setText("");
double Payment = (loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)), loanYears[0]*12));
return Payment;
}
catch(NumberFormatException event)
{
// display error message if not numeric input
JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double CalculatePayment15()
{
try
{
fieldPayment.setText("");
double Payment=(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanYears[1]*12));
return Payment;
}
catch (NumberFormatException event)
{
JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double CalculatePayment30()
{
try
{
fieldPayment.setText("");
double Payment = (loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanYears[2]*12));
return Payment;
}
catch (NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double Calc()
{
try
{
fieldPayment.setText("");
fieldyears.setText("");
fieldinterest.setText("");
double Payment = (loanAmount() * (interest/100/12)) / (1 - Math.pow(1/(1 + (interest/100/12)), years*12));
return Payment;
}
catch(NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public void ClearFields()
{
// set all text fields to blank
fieldPayment.setText ("");
fieldamount.setText ("");
areaAmortTable.setText("");
fieldyears.setText("");
fieldinterest.setText("");
}
public MortCalculation()
{
// set up and initialize buttons
button7 = new JButton("7 years at 5.35%");
button7.setActionCommand("Calculate7");
button15 = new JButton("15 years at 5.5%");
button15.setActionCommand("Calculate15");
button30 = new JButton("30 years at 5.75%");
button30.setActionCommand ("Calculate30");
buttonCalc = new JButton("Calculate");
buttonCalc.setActionCommand("Calc");
buttonClear = new JButton("Reset");
buttonClear.setActionCommand("Clear");
buttonQuit = new JButton("Quit");
buttonQuit.setActionCommand("Quit");
// set up labels and text field sizes
labelTitle = new JLabel ("Mortgage Calculator");
labelMonthlyPay = new JLabel("Monthly Payment");
labelAmount = new JLabel("Amount");
fieldPayment = new JTextField("", 12);
fieldamount = new JTextField("", 10);
labelTerm = new JLabel("Years");
labelRate = new JLabel("Interest Rate");
fieldyears = new JTextField("", 12);
fieldinterest = new JTextField("", 12);
labelAmortTable = new JLabel("Amortization Table");
areaAmortTable = new JTextArea(10, 300);
JScrollPane scrollPane = new JScrollPane(areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// set up actionlisteners for each button
button7.addActionListener(this);
button15.addActionListener(this);
button30.addActionListener(this);
buttonCalc.addActionListener(this);
buttonClear.addActionListener(this);
buttonQuit.addActionListener(this);
// construct gui and layout
JPanel mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.add(labelTitle);
labelTitle.setBounds(150, 20, 500, 15);
mypanel.add(labelAmount);
labelAmount.setBounds(130, 60, 80, 25);
mypanel.add(fieldamount);
fieldamount.setBounds(240, 60, 100, 25);
mypanel.add(labelTerm);
labelTerm.setBounds(130, 85, 80, 40);
mypanel.add(fieldyears);
fieldyears.setBounds(240, 90, 100, 25);
mypanel.add(labelRate);
labelRate.setBounds(130, 125, 80, 25);
mypanel.add(fieldinterest);
fieldinterest.setBounds(240, 125, 100, 25);
mypanel.add(button7);
button7.setBounds(35, 170, 130, 30);
mypanel.add(button15);
button15.setBounds(180, 170, 130, 30);
mypanel.add(button30);
button30.setBounds(320, 170, 135, 30);
mypanel.add(labelMonthlyPay);
labelMonthlyPay.setBounds(130, 220, 100, 25);
mypanel.add(fieldPayment);
fieldPayment.setBounds(240, 220, 100, 25);
fieldPayment.setEditable(false);
mypanel.add(labelAmortTable);
labelAmortTable.setBounds(180, 250, 300, 25);
// add scrollpane, set bounds and set table as not editable
// table is part of scrollpane now, so add and place only the scrollpane
mypanel.add(scrollPane);
scrollPane.setBounds(50, 280, 400, 270);
areaAmortTable.setEditable(false);
mypanel.add(buttonCalc);
buttonCalc.setBounds(50, 570, 100, 30);
mypanel.add(buttonClear);
buttonClear.setBounds(170, 570, 95, 30);
mypanel.add(buttonQuit);
buttonQuit.setBounds(290, 570, 95, 30);
this.setContentPane(mypanel);
this.pack();
this.setTitle("Mortgage Calculator");
// set window size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(500, 700);
}
public double loanAmount()
{
double loanAmount = Double.parseDouble(fieldamount.getText());
return loanAmount;
}
}