Assistance Request - GUI - Amoritization Table

I am taking an on-line programming class. One of our assignments is to develop an amortization table using a GUI. I have developed most of the program but I am stuck on a section of the code. I am able to calculate the first set of payments which are: Monthly Payment, Loan Balance and Interest Paid. I am able to write the results of the calculations to a table and populate the table with the appropriate number of line of results (term of the loan in months). The problem I am having is that the results from the first calculation are written to the entire table. I am unable to get the program into a loop where it will make multiple calculations and then write the results of those calculations to the table.

Specifically the assignment is as follows: Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:

?7 years at 5.35%

?15 years at 5.5%

?30 years at 5.75%

Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit.

If any of you have a spare few moments, I would value you direction on where I might be going wrong. I am sure that the problem has a simple solution but the solution is eluding me. I am not very good at programming and have been struggling for hours on this problem.

The code that I have developed is below. Please note that I need to clean the program up a bit before submitting it to my instructor.

Thanks for any advice that you might have

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import java.text.ParseException;

import java.util.Vector;

import java.text.DecimalFormat;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

import java.io.*;

public class WiverWeek3A extends JPanel implements ActionListener {

// Default values

private static String DEFAULT_ENTRY_TEXT = "Please Enter the Mortgage Amount";

private static String[] COMBO_TEXT = { "7 Years at 5.35% Interest",

"15 Years at 5.5% Interest", "30 Years at 5.75% Interest" };

private static double[][] DEFAULT_DATA = { { 7, .0535 }, { 15, .055 }, { 30, .0575 } };

private static String[] COLUMN_NAMES = { "Monthly Payment", "Loan Balance", "Interest Paid" };

// Strings for the objects

private static String MortgageAmountTextString = "Mortgage Amount: ";

private static String button1String = "Clear";

private static String button2String = "Calculate";

private static String button3String = "Exit";

// Text field for data entry

// This is declared as static because it is

// refered to in the createAndShowGUI() method

private static JTextField entry1MortgageField;

// Labels to identify the text fields

private JLabel MortgageAmountLabel;

private JLabel YearsRatesLabel;

// Buttons

private JButton clearButton;

private JButton calcButton;

private JButton exitButton;

// Option objects for data entry

private JComboBox combo1Box;

// Formats to format and parse numbers

private NumberFormat numberFormat;

// Table for output

private JTable table;

private DefaultTableModel tmod;

// Action listener(s) for the button(s)

// Create action listeners

// In this example, we are creating a separate ActionListener

// for each button. This provides a modular approach that is

// easier to maintain than putting all event logic into

// the main actionPerformed method.

private ClearAction clearaction = new ClearAction();

private CalcAction calcaction = new CalcAction();

private ExitAction exitaction = new ExitAction();

// Inner class to implement an action listener for the clear button

class ClearAction implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

// Put default value back into the field

entry1MortgageField.setText("" + DEFAULT_ENTRY_TEXT);

// Set the table to the default, or empty, model

table.setModel(tmod);

// Set the combo to the first item

combo1Box.setSelectedIndex(0);

// Select the entry field

entry1MortgageField.requestFocus();

entry1MortgageField.selectAll();

}

}

// Inner class to implement an action listener for the calc button

class CalcAction implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

// Create the working variables

int IndexValue = 0;

int Months_In_Year = 12;

double CounterValue = 0;

double LoanTerm = 0;

double PrincipalBalance = 0;

double PrincipalAmount = 0;

double MonthlyPayment = 0;

double AnnualInterestRate = 0;

double MonthlyInterestRate = 0;

double InterestDuePerMonth= 0;

double LoanBalance = 0;

double PrincipalPayment = 0;

double MonthlyInterestPaid = 0;

double InterestPaid = 0;

// Create a new table model that will take the generated data

DefaultTableModel working_tmod = new DefaultTableModel(null,COLUMN_NAMES);

//Set format to parse the numeric entry data

numberFormat = NumberFormat.getNumberInstance();

// Formats variables into a whole number format

// Perform the data collection and call a calcuate method

try {

// Get the value from the entry field.

PrincipalAmount = numberFormat.parse(entry1MortgageField.getText()).doubleValue();

// Get the selected Index value from the combo box

IndexValue = combo1Box.getSelectedIndex();

// Use the index value to pull the appropriate default data

// from the array

entry2Field.setText(money.format(MonthlyPayment));

LoanTerm = DEFAULT_DATA[IndexValue][0] * Months_In_Year;

AnnualInterestRate = DEFAULT_DATA[IndexValue][1];

MonthlyInterestRate = AnnualInterestRate / Months_In_Year;

InterestDuePerMonth = PrincipalBalance * MonthlyInterestRate;

for( int i = 0; i < LoanTerm; i++ ){

MonthlyPayment = (PrincipalAmount * MonthlyInterestRate)/(1 - Math.pow(1/(1 + MonthlyInterestRate),LoanTerm));

InterestPaid = PrincipalAmount * MonthlyInterestRate;

// LoanBalance = (LoanTerm * MonthlyPayment) - MonthlyPayment;

LoanBalance = PrincipalAmount - (MonthlyPayment - InterestPaid);

//PrincipalPayment = MonthlyPayment - InterestPaid;

//LoanBalance -= MonthlyPayment;

// PrincipalPayment = (MonthlyPayment - (PrincipalAmount * MonthlyInterestRate));

//LoanBalance -= PrincipalPayment;

//InterestPaid += (PrincipalAmount * MonthlyInterestRate);

// PrincipalAmount++;

// Create a vector that will hold the row of data for the table

Vector rowData = new Vector();

rowData.add(new Double(MonthlyPayment));

rowData.add(new Double(LoanBalance));

rowData.add(new Double(InterestPaid));

// Add the vector to the table model

working_tmod.addRow(rowData);

}

// With the table model complete, set the new model

// as the current table model to be displayed

table.setModel(working_tmod);

} catch (ParseException e) {

// Any parse error will lead here.

JOptionPane.showMessageDialog(calcButton,"Please Enter a Numeric Value.","The Data Entered is in Error", JOptionPane.ERROR_MESSAGE);

// Select the entry field once the pop-up has closed

entry1MortgageField.requestFocus();

entry1MortgageField.selectAll();

}

}

}

// Inner class to implement an action listener for the exit button

class ExitAction implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

System.exit(0);

}

}

public WiverWeek3A() {

// Instantiate the BorderLayout to be used later

super(new BorderLayout());

// Create screen objects

MortgageAmountLabel = new JLabel(MortgageAmountTextString);

YearsRatesLabel = new JLabel();

entry1MortgageField = new JTextField();

entry1MortgageField.setText(DEFAULT_ENTRY_TEXT);

combo1Box = new JComboBox(COMBO_TEXT);

clearButton = new JButton(button1String);

calcButton = new JButton(button2String);

exitButton = new JButton(button3String);

//Creation of a table the allows edits

//table = new JTable();

//Creation of a table that disallows edits

table = new JTable() {

public boolean isCellEditable(int rowIndex, int vColIndex) {

return false;

}

};

tmod = new DefaultTableModel(null, COLUMN_NAMES);

table.setModel(tmod);

// Create entry screen pane and add objects

Box entryPane = new Box(BoxLayout.X_AXIS);

entryPane.add(MortgageAmountLabel);

entryPane.add(Box.createHorizontalStrut(10));

entryPane.add(entry1MortgageField);

entryPane.add(Box.createHorizontalStrut(10));

entryPane.add(combo1Box);

// Create table screen pane and add objects

JPanel tablePane = new JPanel(new GridLayout(1, 1));

JScrollPane table_pane = new JScrollPane(table,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

tablePane.add(table_pane);

// Create button screen pane and add objects

JPanel buttonPane = new JPanel(new GridLayout(7, 1));

buttonPane.add(YearsRatesLabel);

buttonPane.add(calcButton);

buttonPane.add(clearButton);

buttonPane.add(exitButton);

// Put panes onto the panel

setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

add(entryPane, BorderLayout.NORTH);

add(buttonPane, BorderLayout.WEST);

add(tablePane, BorderLayout.CENTER);

// Set event handlers for the buttons

clearButton.addActionListener(clearaction);

calcButton.addActionListener(calcaction);

exitButton.addActionListener(exitaction);

}

public void actionPerformed(ActionEvent e) {

// Here is where we would process menu options.

// In this example, we are not using it.

}

private static void createAndShowGUI() {

// Set the Swing default look and feel

JFrame.setDefaultLookAndFeelDecorated(true);

// Create window and default close action

JFrame frame = new JFrame("Mortgage Caculator - POS 407 Week 3 IA Wiver");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.

JComponent newContentPane = new WiverWeek3A();

newContentPane.setOpaque(true); // content panes must be opaque

frame.setContentPane(newContentPane);

// Display the window.

frame.pack();

frame.setSize(400, 300);

frame.setLocation(300, 200);

frame.setVisible(true);

// Select and highlight the entry field

entry1MortgageField.requestFocus();

entry1MortgageField.selectAll();

}

public static void main(String[] args) {

// Use invokeLater to ensure thread safety for the Swing events

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

[12207 byte] By [WiverWombata] at [2007-11-26 14:12:15]
# 1
kaoa at 2007-7-8 2:00:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...