class MortCalcGUI extends JFrame implements ActionListener{
/*declare variables for GUI functions*/
protected JButton bCal7,bCal15,bCal30, bClear, bExit;
protected JLabel labelPayment, labelLoanAmount, labelTitle, labelInstructions, labelAmTable;
protected JTextField fieldPayment, fieldLoanAmount;
protected JTextArea areaAmTable;
private static final long serialVersionUID = 1L;
/*declare loan array data and variables and initialize*/
double intPaid, prinPaid,balance, mthlyPayment, loanAmount;
int[]loanTerm = {7,15,30};
double[] intRate = {5.35,5.50,5.75};
double loanAmt = loanAmount;
public void actionPerformed (ActionEvent e){
/*action taken depending on what button is selected*/
if ("Calculate7".equals(e.getActionCommand())){
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment7();
fieldPayment.setText(""+ (currency.format(paymentAmount)));
areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
/*set loan amount */
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <= (loanTerm[0]*12); x++){
/*this loops monthly payments for loans*/
balance=loanAmount;
/* sets Balance to the amount of the loan*/
/*monthly payment calculations*/
intPaid = (((intRate[0]/100.0)/12.0)*balance);
prinPaid = (paymentAmount -intPaid);
loanAmt =(balance-prinPaid);
areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
}
if (balance <= 0){ /*Until Balance is zero*/
areaAmTable.append ("\tRemaining balance =$0.00");
}
}
else if ("Calculate15".equals(e.getActionCommand())){
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment15();
fieldPayment.setText(""+ (currency.format(paymentAmount)));
areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
/*set loan amount */
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <= (loanTerm[1]*12); x++){
/*this loops monthly payments for loans*/
balance=loanAmount;
/* sets Balance to the amount of the loan*/
/*montlhy payment calculations*/
intPaid = (((intRate[1]/100.0)/12.0)*balance);
prinPaid = (paymentAmount -intPaid);
loanAmt =(balance-prinPaid);
areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
}
if (balance <= 0){ /*Until Balance is zero*/
areaAmTable.append ("\tRemaining balance =$0.00");
}
}
else if ("Calculate30".equals(e.getActionCommand())){
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment30();
fieldPayment.setText(""+ (currency.format(paymentAmount)));
areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <= (loanTerm[2]*12); x++){
balance=loanAmount;
intPaid = (((intRate[2]/100.0)/12.0)*balance);
prinPaid = (paymentAmount -intPaid);
loanAmt =(balance-prinPaid);
areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
}
if (balance <= 0){ /*Until Balance is zero*/
areaAmTable.append ("\tRemaining balance =$0.00");
}
}
else if ("Clear".equals(e.getActionCommand())){
ClearFields ();
}
else {
System.exit (0);
}
}
public double CalculatePayment7 (){
/*checks input for incorrect input type*/
try {
/*If input is corect,then perform calculation*/
fieldPayment.setText ("");
double Payment = (loanAmount() * (intRate[0]/100/12))/1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12);
return Payment;
}
catch (NumberFormatException event){
/*error displayed for incorrect input type*/
JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double CalculatePayment15 (){
/*checks input for incorrect input type*/
try {
/*If input is coorect,then perform calculation*/
fieldPayment.setText ("");
double Payment = (loanAmount() * (intRate [1]/100/12))/1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanTerm[1]*12);
return Payment;
}
catch (NumberFormatException event){
/*error displayed for incorrect input type*/
JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double CalculatePayment30 (){
/*checks input for incorrect input type*/
try {
/*If input is coorect,then perform calculation*/
fieldPayment.setText ("");
double Payment = (loanAmount() * (intRate[2]/100/12))/1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanTerm[2]*12);
return Payment;
}
catch (NumberFormatException event){
/*error displayed for incorrect input type*/
JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public void ClearFields (){
/*make all fields blank*/
fieldPayment.setText ("");
fieldLoanAmount.setText ("");
areaAmTable.setText ("");
}
public MortCalcGUI (){
/*sets and initialize buttons*/
bCal7 = new JButton ("7 Year, 5.35%");
bCal7.setActionCommand ("Calculate7");
bCal15 = new JButton ("15 Year, 5.50%");
bCal15.setActionCommand ("Calculate15");
bCal30 = new JButton ("30 Year, 5.75%");
bCal30.setActionCommand ("Calculate30");
bClear = new JButton ("Reset all Fields");
bClear.setActionCommand ("Reset");
bExit = new JButton ("Exit Calculator");
bExit.setActionCommand ("Exit");
/*sets labels and field sizes for GUI*/
labelTitle = new JLabel ("Mortgage Calculator");
labelInstructions = new JLabel ("Enter loan amount and choose rate and term");
labelPayment = new JLabel ("Monthly Payment");
labelLoanAmount = new JLabel ("Amount of Loan");
labelAmTable = new JLabel ("Amortization Table");
areaAmTable = new JTextArea (10, 30);
JScrollPane scrollPane = new JScrollPane (areaAmTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
/*set listeners for buttons*/
bCal7.addActionListener(this);
bCal15.addActionListener(this);
bCal30.addActionListener(this);
bClear.addActionListener(this);
bExit.addActionListener(this);
/*GUI constructor and set layout*/
JPanel mine = new JPanel ();
mine.setLayout (null);
mine.add (labelTitle);
labelTitle.setBounds (110, 30, 700, 15);
mine.add (labelInstructions);
labelInstructions.setBounds ( 30, 70, 450, 15);
mine.add (labelLoanAmount);
labelLoanAmount.setBounds (130, 110, 100, 25);
mine.add (fieldLoanAmount);
fieldLoanAmount.setBounds (240, 110, 100, 25);
mine.add (bCal7);
bCal7.setBounds (40, 150, 125, 30);
mine.add (bCal15);
bCal15.setBounds (180, 150, 125, 30);
mine.add (bCal30);
bCal30.setBounds (320, 200, 100, 25);
mine.add (labelPayment);
labelPayment.setBounds (130, 200, 100, 25);
mine.add (fieldPayment);
fieldPayment.setBounds (240, 200, 100, 25);
fieldPayment.setEditable (false);
mine.add(labelAmTable);
labelAmTable.setBounds (180, 250, 300, 25);
/*add scrollpane and set bounds of scrollpane*/
mine.add(scrollPane);
scrollPane.setBounds (50, 280, 400, 270);
areaAmTable.setEditable (false);
mine.add(bClear);
bClear.setBounds (250, 570, 125, 25);
mine.add (bExit);
bExit.setBounds (250, 570, 125, 30);
this.setContentPane(mine);
this.pack();
this.setTitle ("Mortgage Calculator");
/*set calculator window size*/
}
public double loanAmount (){
double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
return loanAmount;
}
}