package mortgagecalculatorWeek5; /*

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

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.WindowConstants;

import javax.swing.table.DefaultTableModel;

public class MortgageCalculatorWeek5 extends JFrame implements ActionListener {

// Mortgage amount Text Field

private JTextField mortgageAmtText = new JTextField(8);

// Loan option combo box

private JComboBox loanOptionsCombo = new JComboBox();

// Monthly payment Text Field

private JTextField monthlyPaymentText = new JTextField(10);

// Button to display results

private JButton calculateBtn = new JButton("Calculate");

// Button to clear input data

private JButton clearBtn = new JButton("Clear");

// Button to exit

private JButton exitBtn = new JButton("Exit");

// Table model to store result

private DefaultTableModel tableModel;//

public MortgageCalculatorWeek5() {

super("Mortgage Calculator");

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//

setVisible(true);

setResizable(false);

// Input components Panel

JPanel inputSectionPanel = new JPanel();

JLabel mortgageAmtLabel = new JLabel("Mortgage Amount");

JLabel termAndRateLabel = new JLabel("Term and Rate");

JLabel monthlyPaymentLabel = new JLabel("Monthly Payment");

// bttPanel to hold buttons

JPanel buttonPanel = new JPanel();

// JTable to hold amortization data table

JTable paymentTable = new JTable();

inputSectionPanel.add(mortgageAmtLabel);

inputSectionPanel.add(mortgageAmtText);

inputSectionPanel.add(termAndRateLabel);

// Create list of loan options string

String[] loanOptions = new String[3];

loanOptions[0] = "7 years at 5.35%";

loanOptions[1] = "15 years at 5.5%";

loanOptions[2] = "30 years at 5.75%";

// Populate combo box items from list of loan options

for (int i = 0; i < loanOptions.length; i++) {

loanOptionsCombo.addItem(loanOptions[i]);

}

inputSectionPanel.add(loanOptionsCombo);

// Monthly Payment text field

inputSectionPanel.add(monthlyPaymentLabel);

monthlyPaymentText.setEditable(false);

inputSectionPanel.add(monthlyPaymentText);

calculateBtn.addActionListener(this);

clearBtn.addActionListener(this);

exitBtn.addActionListener(this);

buttonPanel.add(calculateBtn);

buttonPanel.add(clearBtn);

buttonPanel.add(exitBtn);

//Thanks to L. Mitchell for her input

// Create Container to hold table and buttons

Container tableButtonContainer = getContentPane();

tableButtonContainer.setLayout(new BorderLayout());

tableButtonContainer.setLayout(new BoxLayout(tableButtonContainer, BoxLayout.Y_AXIS));

tableButtonContainer.add(inputSectionPanel, BorderLayout.WEST);

// Create table model having 4 columns

tableModel = new DefaultTableModel(0, 4);

// Set column headers

String col[] = { "Payment #", "Mortgage Payment Amt", "Loan Balance", "Interest Paid" };

tableModel.setColumnIdentifiers(col);

paymentTable.setModel(tableModel);

// Create JScrollPane to hold table

JScrollPane tableScroller = new JScrollPane(paymentTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

tableScroller.setViewportView(paymentTable);

tableButtonContainer.add(paymentTable, BorderLayout.CENTER);

tableButtonContainer.add(buttonPanel, BorderLayout.SOUTH);

tableButtonContainer.add(tableScroller);

tableScroller.setViewportView(paymentTable);

pack();

}

/**

* This method is invoked when any of the buttons are clicked. Appropriate

* methods are called based on the source of the event

*/

public void actionPerformed(ActionEvent event) {

JButton source = (JButton) event.getSource();

if (source == calculateBtn) {

displayAmortizationChart();

} else if (source == clearBtn) {

clearValues();

} else if (source == exitBtn) {

quit();

}

}

/**

* Calculate the monthly payment and display the amortization for complete

* loan term.

*/

void displayAmortizationChart() {//Assistance provided by J. Bingham

NumberFormat currency = NumberFormat.getCurrencyInstance();

double mortgageAmt = 0;

// Validate entered Mortgage amount, if not a double value, display message to user

try {

mortgageAmt = Double.parseDouble(mortgageAmtText.getText());

} catch (NumberFormatException e) {

JOptionPane.showMessageDialog(null, "Entered Amount is not a valid number put $$$$$.Please try again!",

"Error", JOptionPane.ERROR_MESSAGE);

;

mortgageAmtText.setText(null);

// Clear any existing table data

tableModel.setRowCount(0);

return;

}

// Validate entered Mortgage amount, if not greater than 0, display message to user

if (mortgageAmt <= 0) {

JOptionPane.showMessageDialog(null, "Please enter correct Mortgage amount. Please try again!", "Error",

JOptionPane.ERROR_MESSAGE);

mortgageAmtText.setText(null);

// Clear any existing table data

tableModel.setRowCount(0);

return;

}

int numberOfMonths = 0;

double monthlyInterest = 0;

// Get number of months and monthly interest rate based on selected option in combo box

if (loanOptionsCombo.getSelectedIndex() == 0) {

numberOfMonths = 7 * 12;

monthlyInterest = (5.35 / 12) / 100.0;

} else if (loanOptionsCombo.getSelectedIndex() == 1) {

numberOfMonths = 15 * 12;

monthlyInterest = (5.5 / 12) / 100.0;

} else {

numberOfMonths = 30 * 12;

monthlyInterest = (5.75 / 12) / 100.0;

}

double monthlyPayment = 0;

// Calculate and set monthly payment

monthlyPayment = mortgageAmt

* ((monthlyInterest * Math.pow(1.0 + monthlyInterest, numberOfMonths)) / (Math.pow(

1.0 + monthlyInterest, numberOfMonths) - 1.0));

monthlyPaymentText.setText(currency.format(monthlyPayment));

// Calculate values for each month

double beginningBalance = mortgageAmt;

double endingBalance = mortgageAmt;

tableModel.setRowCount(numberOfMonths);

double interestThisPeriod = 0;

// Iterate through all months and add values to table model.

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

// Set payment number

tableModel.setValueAt(i + 1, i, 0);

// Set Mortgage Payment Amount

tableModel.setValueAt(currency.format(monthlyPayment), i, 1);

// Set Beginning Balance

tableModel.setValueAt(currency.format(beginningBalance), i, 2);

interestThisPeriod = beginningBalance * monthlyInterest;

// Set Mortgage Payment Amount

tableModel.setValueAt(currency.format(interestThisPeriod), i, 3);

endingBalance = beginningBalance - monthlyPayment + interestThisPeriod;

beginningBalance = endingBalance;

}

}

/**

* This method clears all values

*/

void clearValues() {

tableModel.setRowCount(0);

mortgageAmtText.setText(null);

monthlyPaymentText.setText(null);

loanOptionsCombo.setSelectedIndex(0);

}

/**

* Exit from program

*/

void quit() {

System.exit(0);

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MortgageCalculatorWeek5().setVisible(true);//

}

});

}

}