# Service Request SR-mf-003 Change Request5

on
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, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.

I just can't figure out to use the array to calculate the Principal Paid, Interest Paid, and Balance that changes from payment 001 to XXX. (for example, 30 years loan is 360 times)

How can we loop the calculation to be from pay cycle 001 to 360 for example?
Use "for" statement, but this is too diffciult.
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010

Commented:
for (payment=1; payment<=360; payment++)
{
System.out.println("Payment: "+payment);
.....
}

Commented:
Thank you super black belt!
Since there are 3 different loan options, how can I apply it to the for( ) statement?
If I do not know well, I may need to use the for() statement 3 times to output the uniquie calculation.
From you code, I can understand that calculation will repert 360 times. If the diffierent option is selected, then another for() statement should be used. For example, for(pay=1; pay=<180;pay++).
How can I apply the array to this?
I think I need to use like : int[] term={7,15,30}; double[] interest ={5.35;5.50;5.75};
Java Developer
Top Expert 2010
Commented:
use nested loops

for (int i=0; i<3; i++) {
int t = term[i];
double int = interest[i];
...

for (payment=1; payment<=360; payment++)
{
System.out.println("Payment: "+payment);
.....
}
}

Commented:
Hello!
When I look at your codes, I think that we can repeat the calculation for 3 options without writing the same kind of codes for 3 time. Am I right? The next step is to figure out the way to output the data as Month,-Principal paid,-Interest paid, and-Balance. Is there a simple way to format the data?
I would like to use the JTextArea using the NetBeans. In fact that I built a GUI part without any calculation part. Am I right that above calculation part of coding should be written under the
calculate action perfromed() section? It seems to me that calculation was made when I clicked the calculation button when I wrote some calculation codes under this statement.
Please help. I am sorry that I need more basics to deal with this, but I have to complete somehow. Allow me to excuse!
Java Developer
Top Expert 2010

Commented:
> I think that we can repeat the calculation for 3 options without writing the same kind of codes for 3 time. Am I right?

correct

> The next step is to figure out the way to output the data as Month,-Principal paid,-Interest paid, and-Balance. Is there a simple way to format the data?

first do it unformatted, then post a new question and I'll look at the formatting for you there

> Am I right that above calculation part of coding should be written under the
calculate action perfromed() section?

correct

Commented:
Great instructor!
I will write some code tonight, and post it. It will be hard, but I will try my best so that you will know where I am at.
Thanks again for the support.

Commented:
Hello,
I have to use a combobox, and I learned that I use the "getSelectedIntex() method to get the selected item. Can I use the switch statement to do the calculation?
Example:
// create the combo box
String[] options = new String[] {"Option 1", "Option 2", "Option 3"};
myComboBox = new JComboBox(options)

...

// get the selected option
int selectedOption = myComboBox.getSelectedIndex();
switch (selectedOption) {
case 0: // Option 1
// do your stuff with option 1
break;

case 1: // Option 2
// do your stuff with option 2
break;

case 2: // Option 3
// do your stuff with option 3
break;
}

Question: I do not understand where I can put this. When I made a GUI using the NetBeans, event lisenter stuff for the combobox is made already, and there is a method for this. Do I need to write those code there?
If so, I do not think the calculation(when I press the calculation butoon) button which is another method will never know this. Last week, I could write every calculation codes under the actionbuttonperformed method to make it work. This time, there are multiple choices due to combobox use. I need to know how to tell the calculationbuttonactionperform to know which option is selected.

Here is the codes:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* MortgageGUI2.java
*
* Created on Jan 26, 2010, 9:32:59 AM
*/

package mortgage2;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.text.DecimalFormat;

/**
*

*/
public class MortgageGUI2 extends javax.swing.JFrame {
DecimalFormat c = new DecimalFormat("\$,###,###,###.00");

/** Creates new form MortgageGUI2 */
public MortgageGUI2() {
initComponents();
loanamtfield.setText(null);

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
titlelabel = new javax.swing.JLabel();
loanamountlabel = new javax.swing.JLabel();
loanoptionslsabel = new javax.swing.JLabel();
calculate = new javax.swing.JButton();
clear = new javax.swing.JButton();
endprogram = new javax.swing.JButton();
monthpaylabel = new javax.swing.JLabel();
monthpay = new javax.swing.JLabel();
loanamtfield = new javax.swing.JTextField();
loanOption = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();

jLabel1.setText("jLabel1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

titlelabel.setFont(new java.awt.Font("Tahoma", 1, 14));
titlelabel.setText("Mortgage Calculator V2");

loanamountlabel.setFont(new java.awt.Font("Tahoma", 1, 12));
loanamountlabel.setText("Loan Total Amount: \$");

loanoptionslsabel.setFont(new java.awt.Font("Tahoma", 1, 12));
loanoptionslsabel.setText("Loan Option(select from available choice) :");

calculate.setText("Calculate");
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateActionPerformed(evt);
}
});

clear.setText("Clear");
clear.setActionCommand("clear");
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});

endprogram.setText("End Program");
public void actionPerformed(java.awt.event.ActionEvent evt) {
endprogramActionPerformed(evt);
}
});

monthpaylabel.setText("Your Monthly  Payment Under This Loan is:  \$");

monthpay.setText("monthlypay");

loanamtfield.setText("loanamount");

loanOption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%", " " }));
public void actionPerformed(java.awt.event.ActionEvent evt) {
loanOptionActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);

layout.setVerticalGroup(
);

pack();
}// </editor-fold>

private void calculateActionPerformed(java.awt.event.ActionEvent evt) {

}

private void clearActionPerformed(java.awt.event.ActionEvent evt) {
loanamtfield.setText(null);
monthpay.setText(null);

}

private void endprogramActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void loanOptionActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MortgageGUI2().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton calculate;
private javax.swing.JButton clear;
private javax.swing.JButton endprogram;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JComboBox loanOption;
private javax.swing.JLabel loanamountlabel;
private javax.swing.JTextField loanamtfield;
private javax.swing.JLabel loanoptionslsabel;
private javax.swing.JLabel monthpay;
private javax.swing.JLabel monthpaylabel;
private javax.swing.JLabel titlelabel;
// End of variables declaration

}
Java Developer
Top Expert 2010

Commented:
one question at a time :)

please close this question, and open a new one with your new question
Chief Technology Officer
Most Valuable Expert 2011

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Do more with