import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class JamesProctorProgramGUIWk3 extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//Loan Values
double principalPaid;
double currentInter;
double balanceAmount;
double monthPrincipal;
double interestRate;
double paymentField;
double monthInterest;
double[] interRate = {0.0535, 0.0550, 0.0575};
int[] yearRate = {7, 15, 30};
int termYears, termMonths, i = 0, m = 0, done;
int monthAmount;
String[] rate = {"7 year at 5.35%", "15 year at 5.5%", "30 year at 5.75%"};
String principalAmount;
//text fields
JTextField loanField = new JTextField("", 7);
JTextField monthField = new JTextField("", 7);
//creates the panel
JPanel contentPanel;
//creates the reset and calculate buttons
JButton calculateButton, resetButton;
//creates labels
JLabel loanAmountlbl, monthlyPaymentlbl;
//Sets Decimal Format
DecimalFormat paymentFormat = new DecimalFormat ("$###,##0.00");;
//create combo box
JComboBox percentBox = new JComboBox();
//creates table format
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//creates GUI
public JamesProctorProgramGUIWk3()
{
super();
createGUI();
}
public void createGUI()
{
this.setSize(700, 300); //length, height
this.setLocation(0, 0); //this specifies where the frame position will be on the screen
this.setContentPane(contentPanel());
this.setTitle("James Proctor Program GUI Wk3");
}
public JPanel contentPanel()
{
contentPanel = new JPanel();
contentPanel.setLayout(null);
//adds labels to the panel
loanAmountlbl = new JLabel("Mortgage:");
loanAmountlbl.setLocation(200, 30);
loanAmountlbl.setSize(100, 25);
contentPanel.add(loanAmountlbl);
monthlyPaymentlbl = new JLabel("Monthly Payment:");
monthlyPaymentlbl.setLocation(158, 85);
monthlyPaymentlbl.setSize(100, 30);
contentPanel.add(monthlyPaymentlbl);
//adds text fields
loanField = new JTextField("", 10);
loanField.setLocation(280, 30);
loanField.setSize(150, 25);
contentPanel.add(loanField);
monthField = new JTextField("", 10);
monthField.setLocation(280, 85);
monthField.setSize(150, 25);
contentPanel.add(monthField);
//this adds the combo box and action listener
percentBox.addItem(rate[0]);
percentBox.addItem(rate[1]);
percentBox.addItem(rate[2]);
percentBox.setLocation(280, 55);
percentBox.setSize(150, 25);
percentBox.addActionListener(this);
contentPanel.add(percentBox);
//adds a scrollbar
JScrollPane scroller = new JScrollPane(table);
contentPanel.add(scroller);
scroller.setSize(650,300);
scroller.setLocation(20, 150);
//adds column names to the table
model.addColumn("Payment Number");
model.addColumn("Current Interest");
model.addColumn("Principal Paid");
model.addColumn("New Balance");
//setup up buttons
resetButton = new JButton("Reset");
resetButton.setLocation(450, 55);
resetButton.setSize(100, 25);
contentPanel.add(resetButton);
calculateButton = new JButton("Calculate");
calculateButton.setLocation(450, 85);
calculateButton.setSize(100, 25);
contentPanel.add(calculateButton);
calculateButton.addActionListener(this);
resetButton.addActionListener(this);
return contentPanel;
}
//action event for the calculate button
public void actionPerformed(ActionEvent e)
{
String calculate = e.getActionCommand();
if (e.getSource() == percentBox) {
switch (percentBox.getSelectedIndex()){
case 0:
i = 0;
break;
case 1:
i = 1;
break;
case 2:
i = 2;
break;
}
}
if (calculate == "Calculate")
{
loanField.setText("");
principalAmount = monthField.getText();
//cannot get the calculate button to function
//error has to do with the below line of code
monthPrincipal = Double.parseDouble(principalAmount);
}
if(done == 1)
done = 0;
else {
interestRate = interRate[i];
termYears = yearRate[i];
monthInterest = interestRate/(12*100); //finds the monthly interest
termMonths = termYears*12; //calculates term length in months
paymentField = monthInterest*monthPrincipal/(1-Math.pow((1+monthInterest), -termMonths)); //calculates monthly payment
loanField.setText(" " + paymentFormat.format(paymentField));
//loop for the table to display each payment
for (m = 0; m <= monthAmount; m ++) {
monthAmount = yearRate[i]*12;
currentInter = monthPrincipal * monthInterest;
principalPaid = paymentField - currentInter;
balanceAmount = monthPrincipal - principalPaid;
monthPrincipal = balanceAmount;
if(monthPrincipal <= 1){ break;}
model.addRow(new Object[]{m+1, paymentFormat.format(currentInter), paymentFormat.format(principalPaid), paymentFormat.format(balanceAmount)});
if(monthPrincipal <= 1){ break;}
}
}
//action event for the reset button
//this button does work
if (e.getSource() == resetButton)
{
loanField.setText("");
monthField.setText("");
loanField.requestFocusInWindow();
percentBox.setSelectedIndex(0);
model.setRowCount(0);
}
}
//shows the GUI
public static void main(String[] args){
new JamesProctorProgramGUIWk3().setVisible(true);
}
}