Morgage Calculator SR 5

on
I need to write the program in Java with a GUI 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%

I have to use an array for the mortgage data for the different loans and 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. I need some help getting this started.
``````import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class MCalculatorGUI///Name of my File and Class
{
public static void main(String[] args)
{
double loanAmount;//Declaring my Variables
double loanInterest;//Declaring my Variables
double monthlyPayment;//Declaring my Variables
double interestRate;
int loanTerm;
String totalLoan, interestLoan, termLoan;
DecimalFormat decimalPlaces=new DecimalFormat("0.00"); //Format decimal point for proper display

totalLoan=JOptionPane.showInputDialog(null, "Enter the Amount of the Loan: ");
loanAmount = Double.parseDouble(totalLoan);

interestLoan=JOptionPane.showInputDialog(null, "Enter the Interest Rate of the Loan in decimal Form: ");
loanInterest = Double.parseDouble(interestLoan)/12;

termLoan=JOptionPane.showInputDialog(null, "Enter the Term of the Loan: ");
loanTerm = Integer.parseInt(termLoan)*12;

// calculations
monthlyPayment = (loanAmount * loanInterest) / (1 - Math.pow(1 + loanInterest, -loanTerm));//Formula for Monthly Payments

JOptionPane.showMessageDialog(null,"Your Monthly Payments Are" + decimalPlaces.format(monthlyPayment));
System.exit(0);
}}
``````
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004
Commented:
looks like homework
Commented:
If I understand your homework correctly, then for each period you have to keep track of the following values:

Current Loan Principal = the remainder of the loan principal at the beginning of the month
Current Interest Payment = the monthly interest paid for the remainder of the loan principal
Current Principal Payment = the monthly interest paid subtracted from the monthly payment

next month's principal remainder will be this month's principal minus the this month's principal payment.

You will have to figure out the rest for yourself :)

Commented:
My program will not run, am I missing something? Can someone help please?

import javax.swing.*;

//imported io file for the math method
import java.io.*;
//imported decimalformt for its format method
import java.text.DecimalFormat;

public class MCalculatorGUI2 extends javax.swing.JFrame
{

/** Creating a new form MortgageCalGUI **/
public MCalculatorGUI2()
{
//call to method Components() to initilize the new from
Components();

}

/** The Components() method is called within our MortgageCalGUI constructor to initilize our new MortgageCalGUI form **/
private void Components()
{
//creating text fields
txtfld_principal = new javax.swing.JTextField();

//creating labels
//principal label field
label_principal = new javax.swing.JLabel();
label_principal.setText("Principal (No comma's)");
//term label field
label_term = new javax.swing.JLabel();

//create a combo box for our terms
bx_term = new javax.swing.JComboBox(terms);
bx_term.setSelectedIndex(0);

/*
create a monthly label to be used for monthly payment output
label_monthly = new javax.swing.JLabel();
label_monthly.setText("\$0.00");
*/

//creating Button
btn_calculate = new javax.swing.JButton();
btn_calculate.setText("Calculate");

//create out event handler for our combo box
public void actionPerformed(java.awt.event.ActionEvent evt) {
HandleComboBox(evt);
}
});

//create our event handler for our calculate button
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalculationPerformed(evt);
}
});

//create our "escape" route
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

//create a new frame nameing it Mortgage Calculator
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //using our "escape" route for our new frame

//set the layout of the window
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);

/*** HORIZONTAL POSITION LAYOUT ***/

/*** THE PRINCIPAL TEXT FIELD HORIZONTAL POSITION ***/

/* THE TERM LABEL FIELD HORIZONTOAL POSITION ***/

/*** THE COMBO BOX HORIZONTAL POSITION ***/

/*** THE BUTTON HORIZONTAL POSITION ***/

);

btn_calculate,
bx_term,
txtfld_principal,
label_term }
);

/*** VERTICAL POSITION LAYOUT ***/

/*** THE PRINCIPAL TEXT FIELD VERTICAL POSITION ***/

/*** THER TERM LABEL FIELD VERTICAL POSITION ***/

/*** THE COMBO BOX VERTICAL POSITION ***/

/*** THE CALCULATE BUTTON VERTICAL POSITION ***/

);
pack();
}

//HandleComboBox is called by the event handle for our combo box
//Creates a temporary combo box to help set the arrays and to set the correct interest rates into the interest text field
private void HandleComboBox(java.awt.event.ActionEvent evt)
{
//creating a temporary combo box
//the source of our event is our combo box (bx_term)
javax.swing.JComboBox temp_cb = (JComboBox)evt.getSource();

//creating a temporary Sting to hold our selection from our combo box
String temp_term = (String)temp_cb.getSelectedItem();
txtfld_term.setText(temp_term + " years");

//setting the index to our interest array
int index = 0;
switch((int)(Integer.parseInt(temp_term)))
{
case 7 :
index = 0;
break;
case 15 :
index = 1;
break;
case 30 :
index = 2;
break;
}
//using the index for our interest array
txtfld_interest.setText(interest[index]+"");
}

//CalculationPerformed is called by the event handler for out button
//calculates the inputed values and then updates the monthly payment label
private void CalculationPerformed(java.awt.event.ActionEvent evt)
{

/*/calculate payment based on inputed values
final double temp_mthly = (temp_principal * (temp_interest/12)) / (1-Math.pow(1 + temp_interest/12,-(temp_term*12)));

label_monthly.setText(DecimalPlaces.format(temp_mthly) + " Monthly"); *
}

public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MCalculatorGUI2().setVisible(true);
}
});
}

/*** VARIABLES AND OBJECT DECLARATIONS ***/
//labels
private javax.swing.JLabel label_principal;
private javax.swing.JLabel label_term;
private javax.swing.JLabel label_interest;
private javax.swing.JLabel label_monthly;
private javax.swing.JLabel label_pymntschedule;

//text fields
private javax.swing.JTextField txtfld_principal;
private javax.swing.JTextField txtfld_term;
private javax.swing.JTextField txtfld_interest;
private javax.swing.JTextField txtfld_monthly;
private javax.swing.JTextField txtfld_pymntschedule;

//arrays
private String[] terms    = { "7", "15", "30" };
private double[] interest = { 0.0535, 0.055, 0.0575 };

//buttons
private javax.swing.JComboBox bx_term;
private javax.swing.JButton btn_calculate;

//scroll pane
private javax.swing.JScrollPane scroll_payments;

//Delcaing object
//declaring "decimalPlaces" as a new object type of DecimalFormat with the format of 0.00
//this ensures any vaiables used by decimalPlaces has a "currancy" value of two decimal places
private DecimalFormat DecimalPlaces = new DecimalFormat("\$#,###,###.##");
}
Commented:
sorry mate, I am no swing expert.
do you get any error messages? does it compile at all?

Commented:
No this is the error I get

MCalculatorGUI2.java:101: illegal start of expression
);
^
1 error
Commented:
Ok now I am really lost!

import javax.swing.*;

//imported io file for the math method
import java.io.*;
//imported decimalformt for its format method
import java.text.DecimalFormat;

public class MCalculatorGUI2 extends javax.swing.JFrame
{

/** Creating a new form MortgageCalGUI **/
public MCalculatorGUI2()
{
int term = 0;
double principal = 0;
double rate = 0;
double monthlyPayment = 0;
double interest = 0;

//call to method Components() to initilize the new from
Components();

}

/** The Components() method is called within our MortgageCalGUI constructor to initilize our new MortgageCalGUI form **/
private void Components()
{
//creating text fields
txtfld_principal = new javax.swing.JTextField();

//creating labels
//principal label field
label_principal = new javax.swing.JLabel();
label_principal.setText("Principal (No comma's)");
//term label field
label_term = new javax.swing.JLabel();

//create a combo box for our terms
bx_term = new javax.swing.JComboBox(terms);
bx_term.setSelectedIndex(0);

/*
create a monthly label to be used for monthly payment output
label_monthly = new javax.swing.JLabel();
label_monthly.setText("\$0.00");
*/

//creating Button
btn_calculate = new javax.swing.JButton();
btn_calculate.setText("Calculate");

//create out event handler for our combo box
public void actionPerformed(java.awt.event.ActionEvent evt) {
HandleComboBox(evt);
}
});

//create our event handler for our calculate button
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalculationPerformed(evt);
}
});

//create our "escape" route
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

//create a new frame nameing it Mortgage Calculator
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //using our "escape" route for our new frame

//set the layout of the window
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);

/*** HORIZONTAL POSITION LAYOUT ***/

/*** THE PRINCIPAL TEXT FIELD HORIZONTAL POSITION ***/

/* THE TERM LABEL FIELD HORIZONTOAL POSITION ***/

/*** THE COMBO BOX HORIZONTAL POSITION ***/

/*** THE BUTTON HORIZONTAL POSITION ***/

);

btn_calculate,
bx_term,
txtfld_principal,
temp_term,
label_term }
);

/*** VERTICAL POSITION LAYOUT ***/

/*** THE PRINCIPAL TEXT FIELD VERTICAL POSITION ***/

/*** THER TERM LABEL FIELD VERTICAL POSITION ***/

/*** THE COMBO BOX VERTICAL POSITION ***/

/*** THE CALCULATE BUTTON VERTICAL POSITION ***/

);
pack();
}

//HandleComboBox is called by the event handle for our combo box
//Creates a temporary combo box to help set the arrays and to set the correct interest rates into the interest text field
private void HandleComboBox(java.awt.event.ActionEvent evt)
{
//creating a temporary combo box
//the source of our event is our combo box (bx_term)
javax.swing.JComboBox temp_cb = (JComboBox)evt.getSource();

//creating a temporary Sting to hold our selection from our combo box
String temp_term = (String)temp_cb.getSelectedItem();
txtfld_term.setText(temp_term + " years");

//setting the index to our interest array
int index = 0;
switch((int)(Integer.parseInt(temp_term)))
{
case 7 :
index = 0;
break;
case 15 :
index = 1;
break;
case 30 :
index = 2;
break;
}
//using the index for our interest array
txtfld_interest.setText(interest[index]+"");
}

//CalculationPerformed is called by the event handler for out button
//calculates the inputed values and then updates the monthly payment label
private void CalculationPerformed(java.awt.event.ActionEvent evt)
{

//calculate payment based on inputed values
final double temp_mthly = (temp_principal * (temp_interest/12)) / (1-Math.pow(1 + temp_interest/12,-(temp_term*12)));

label_monthly.setText(DecimalPlaces.format(temp_mthly) + " Monthly");
}

public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MCalculatorGUI2().setVisible(true);
}
});
}

/*** VARIABLES AND OBJECT DECLARATIONS ***/
//labels
private javax.swing.JLabel label_principal;
private javax.swing.JLabel label_term;
private javax.swing.JLabel label_interest;
private javax.swing.JLabel label_monthly;
private javax.swing.JLabel label_pymntschedule;

//text fields
private javax.swing.JTextField txtfld_principal;
private javax.swing.JTextField txtfld_term;
private javax.swing.JTextField txtfld_interest;
private javax.swing.JTextField txtfld_monthly;
private javax.swing.JTextField txtfld_pymntschedule;

//arrays
private String[] terms    = { "7", "15", "30" };
private double[] interest = { 0.0535, 0.055, 0.0575 };

//buttons
private javax.swing.JComboBox bx_term;
private javax.swing.JButton btn_calculate;

//scroll pane
private javax.swing.JScrollPane scroll_payments;

//Delcaing object
//declaring "decimalPlaces" as a new object type of DecimalFormat with the format of 0.00
//this ensures any vaiables used by decimalPlaces has a "currancy" value of two decimal places
private DecimalFormat DecimalPlaces = new DecimalFormat("\$#,###,###.##");
}

Do more with