This assignment requires that I allow for user input in a GUI for principal, interest rate, and term in years. I also have to provide a pulldown with 3 preset rates that will also load a cooresponding term length. All of that plus an amoritization table as well.
I have a good start just can't get it to call my user entry now.
/*
Mortgage Calculator Application
joeweek4_IA.java
Programer: Joe Klink
PRG/421 University of Phonenix
Purpose:This project calculates the monthly payment of a mortgage and displays a amortization table.
The user can select a loan from a drop down menu.
version: 1.00 2008/12/13
*/
//imports required packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class joeweek4 extends JPanel implements ActionListener {
//global variable
private JComboBox IntRates;
private JLabel prinlb, rateLb, termLb, paymtLb;
private JButton Calc, Reset;
private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
private TextArea PayAmort;
String currentInt;
public joeweek4()
{
String[] comboItems={"5.35","5.50","5.75"};
currentInt = comboItems [0];
//remove later IntRates = new JComboBox(comboItems);
//creates labels
prinlb = new JLabel("Principal Amount:");
rateLb = new JLabel("Interest Rate:");
termLb = new JLabel("Term:");
paymtLb = new JLabel("Monthly Payment:");
//creates textfields
prinTxFld = new JTextField("",10);
rateTxFld = new JTextField("",5);
termTxFld = new JTextField("",5);
monthlyPayment = new JTextField("",10);
//combobox pulldown
JComboBox IntRates = new JComboBox(comboItems);
IntRates.setEditable(true);
IntRates.addActionListener(this);
PayAmort = new TextArea(20,70);
//create buttons
Calc = new JButton("Calculate");
Calc.setActionCommand ("GO");
Reset = new JButton("Reset");
Reset.setActionCommand("Reset");
//add action to button
Calc.addActionListener(this);
Reset.addActionListener(this);
//add labels and fields
add(prinlb);
add(prinTxFld);
add(rateLb);
add(IntRates);
add(rateTxFld);
add(termLb);
add(termTxFld);
add(paymtLb);
add(monthlyPayment);
add(PayAmort);
add(Calc);
add(Reset);
}
public void actionPerformed(ActionEvent e)
{
if ("GO".equals(e.getActionCommand()))
{
CalculateMortgage();
}
else
{
prinTxFld.setText("");
rateTxFld.setText("");
termTxFld.setText("");
monthlyPayment.setText("");
PayAmort.setText ("");
}
}
public void CalculateMortgage()
{
//string array to accept which terms to use
rateTxFld.setText("" + (String)IntRates.getSelectedItem());
double principle=Double.parseDouble (prinTxFld.getText());
double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
if (dblrateTxFld == 5.35)
termTxFld.setText("7");
else if (dblrateTxFld == 5.50)
termTxFld.setText("15");
else if (dblrateTxFld == 5.75)
termTxFld.setText("30");
int inttermTxFld=Integer.parseInt(termTxFld.getText());
// Formats the output of dollar figures
DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
double Payment;
double InterestPaid;
double prinTxFldBal;
double temp;
int Months;
double intMonthlyRate = dblrateTxFld/100;
int MonthTerm = inttermTxFld * 12;
Months=MonthTerm;
//formulas
Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
//converts monthly payment to decimal format
monthlyPayment.setText("" + (two_decimal.format(Payment)));
PayAmort.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
for (int counter=0; counter < MonthTerm; counter++)
{
temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
InterestPaid = Payment * temp;
principle = (principle - Payment + InterestPaid);
MonthTerm = MonthTerm --;
Months--;
PayAmort.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
}
}
private static void createAndShowGui()
{
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
joeweek4 myCalculator = new joeweek4();
myCalculator.setOpaque(true);
frame.setContentPane(myCalculator);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
createAndShowGui();
}
}
);
}
}