Service Request SR-mf-003 Change Request5

baka99
baka99 used Ask the Experts™
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
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

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

Author

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};
Please help.
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);
       .....
   }
}
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

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!
Mick BarryJava 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

Author

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.

Author

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");
        calculate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateActionPerformed(evt);
            }
        });

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

        endprogram.setText("End Program");
        endprogram.addActionListener(new java.awt.event.ActionListener() {
            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%", " " }));
        loanOption.addActionListener(new java.awt.event.ActionListener() {
            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.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(titlelabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(127, 127, 127))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loanamountlabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loanamtfield, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(108, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loanoptionslsabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loanOption, 0, 116, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(calculate)
                        .addGap(33, 33, 33)
                        .addComponent(clear)
                        .addGap(36, 36, 36)
                        .addComponent(endprogram))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(monthpaylabel)
                        .addGap(18, 18, 18)
                        .addComponent(monthpay)))
                .addContainerGap(74, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {calculate, clear});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titlelabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loanamountlabel)
                    .addComponent(loanamtfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loanoptionslsabel)
                    .addComponent(loanOption, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calculate)
                    .addComponent(clear)
                    .addComponent(endprogram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(monthpaylabel)
                    .addComponent(monthpay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        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) {
        // TODO add your handling code here:
    }

    /**
    * @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

}
Mick BarryJava Developer
Top Expert 2010

Commented:
one question at a time :)

please close this question, and open a new one with your new question
Kevin CrossChief 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

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial