I have finally gotten my GUI to look like I want it to, but I am having trouble getting the calculations right. No matter what I put in there, it seems to calculate a large payment, and a very wrong amortization schedule... Here is what I have so far...
Code:
package guiweek3;
//imports necessary tools
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.text.*;
import java.text.*;
import java.util.*;
//creates the different fields, labels for the fields, and the panel and buttons
public class guiweek3 extends JFrame implements ActionListener
{
//creates gui panel and compnents including text fields and labels
private JPanel panelTop = new JPanel(new GridLayout (3,3));
private JPanel panelMid = new JPanel(new FlowLayout());
private JPanel panelBottom = new JPanel();
private JTextField amountField = new JTextField();
private JTextField paymentField = new JTextField();
private JLabel paymentLabel = new JLabel("Monthly Payment:");
private JLabel amountLabel = new JLabel("Enter Your Loan Amount: $");
private JLabel loanLabel = new JLabel("Choose Your Loan Terms:");
private JComboBox loanMenu = new JComboBox();
private JTextArea amortizationSchedule = new JTextArea(25,50);
private JButton calculateButton = new JButton("Calculate Payment");
private JButton clearButton = new JButton("Clear Fields");
private JButton quitButton = new JButton("Quit");
static DecimalFormat usDollar = new DecimalFormat("$###,##0.00");
//create arrays for the three loans
int[] yearsArray = {7, 15, 30};
double[] interestArray = {5.35, 5.50, 5.75};
//exit action
public guiweek3()
{
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
//adds the listen action to the calculate, clear, and quit buttons
calculateButton.addActionListener(this);
quitButton.addActionListener(this);
clearButton.addActionListener(this);
}
//adds the individual components (panel, fields, labels, buttons)to the
//panel, sets column sizes for the text fields. Makes payment field static.
public void initComponents()
{
//creates the object attributes
amountField.setColumns(8);
paymentField.setColumns(8);
paymentField.setEditable(false);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
JScrollPane amortizationPanel = new JScrollPane(amortizationSchedule);
amortizationSchedule.setEditable(false);
//creates the components and adds them to the panel
panelTop.add(amountLabel);
panelTop.add(amountField);
panelTop.add(loanLabel);
panelTop.add(loanMenu);
panelTop.add(paymentLabel);
panelTop.add(paymentField);
panelMid.add(calculateButton);
panelMid.add(clearButton);
panelMid.add(quitButton);
panelBottom.add(amortizationPanel);
pane.add(panelTop);
pane.add(panelMid);
pane.add(panelBottom);
for(int i=0; i<3; i++)
{
loanMenu.addItem(interestArray[i] + "% interest for " + yearsArray[i] + " years");
} //end for
}//end initComponents
//creates frame called guiweek2
public static void main(String[] args) throws IOException
{
guiweek3 frame = new guiweek3();
frame.setTitle("Calculator Your New Mortgage Payment");
frame.setBounds(200, 200, 600, 600);
}//end main
//if the calculate button is pressed, set the amount
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == calculateButton)
{
//error handling try/catch, and cases for the different loans avaialble
try
{
double term;
double rate;
switch (loanMenu.getSelectedIndex())
{
case 0:
rate = interestArray[0];
term = yearsArray[0];
break;
case 1:
rate = interestArray[1];
term = yearsArray[1];
break;
case 2:
rate = interestArray[2];
term = yearsArray[2];
break;
}//end switch
double amount = Double.parseDouble (amountField.getText());
//formatting for currencies and percentages
NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
for (int i = 0; i<3; ++i)
{
double monthlyInterest = interestArray[i] / 12;
double months = yearsArray[i] * 12;
double payment = (amount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), months * -1));
double remainingPrincipal = (amount - payment);
paymentField.setText(usDollar.format(payment));
//calculations for amortization results
while (remainingPrincipal > 0)
{
double interestPayment = (remainingPrincipal * (interestArray[i] / 1200) * 1);
//Display loan balance and interest paid
amortizationSchedule.append ("Balance:" + usDollar.format(remainingPrincipal)
+ "\tInterest Paid:" + usFormat.format(interestPayment) +"\n");
// Subtract amount paid to principal from loan balance
remainingPrincipal = remainingPrincipal - payment;
}//end while
}//end for
}//end try
//error message to user if loan is not entered as a number
catch(NumberFormatException i)
{
JOptionPane.showMessageDialog(null, "You must enter the loan " +
"amount as a number.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
}//end catch
}//end if calculate
//if the clear button is pressed, clear the fields
if (event.getSource() == clearButton)
{
amountField.setText("");
paymentField.setText("");
amortizationSchedule.setText("");
}//end if clear statement
//if the quit button is pressed, exit the application
if (event.getSource() == quitButton)
{
System.exit(0);
}//end if quit statement
}//end actionPerformed
}//end class guiweek3
Comment