We help IT Professionals succeed at work.
Get Started

Mortgage Calculator

Rebnick
Rebnick asked
on
865 Views
Last Modified: 2013-11-23
I can not figure out how to get my combo box and arrays working any help would be grealy appreciated. This is what I have so for.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.swing.text.*;
import java.util.Locale;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;



public class POS407_W3_RMR extends JPanel
                          implements ActionListener {
    JLabel picture;

    //creating variables

     public double monthly_interest;
     public double monthly_payment;
       public double number_payments;
       public double new_principle;
       public double m_principle;
       public double cm_interest;
       public double mortint;
       double new_balance;

     //Create Labels
       private JLabel ownerLabel;
       private JLabel assignmentLabel;
       private JLabel separatorLabel;
       private JLabel termLabel;
       private JLabel principleLabel;
       private JLabel yearLabel;
       private JLabel monthlyLabel;
       private JLabel fillerLabel;


       //Value for Fields
       double principal;
       double rate;
       int term;
       int pos;


       //Create the text fields and set them up.
       JTextField principalField = new JTextField("100000");
       JTextField termField = new JTextField("7");
       JTextField rateField = new JTextField(".0535");
       JTextField paymentField = new JTextField(10);


     //Create Buttons
       private JButton calculateButton;
       private JButton exitButton;



       //Strings for the labels
       private static String ownerString = "Name : Rebecca Ragucci";
       private static String assignmentString = "Assignment : POS407 Workshop 3";
       private static String separatorString  = "--------------------------------------------------";


     //Create JTextArea and JScrollPane
       JTextArea displayArea = new JTextArea(10,55);
       JScrollPane scroll = new JScrollPane(displayArea);


    public POS407_W3_RMR() {
        super(new BorderLayout());

        String[] petStrings = { "7 years at 5.35", "4years", "30years", "40years", "50years" };
        String termArray [] = {"7","4","30","40","50"};
        String rateArray [] = {"5.35","7.57","5.75","5.65","5.45"};




        //Define Panels
            JPanel headingPane = new JPanel(new GridLayout(0,1));
            JPanel termPane = new JPanel(new GridLayout(0,1));
            JPanel principlePane = new JPanel(new GridLayout(0,1));
            JPanel textPane = new JPanel(new GridLayout(0,2));
            JPanel comboPane = new JPanel(new GridLayout(0,1));

            //Define Labels
            ownerLabel = new JLabel(ownerString);
            assignmentLabel = new JLabel(assignmentString);
            separatorLabel = new JLabel(separatorString);
            principleLabel = new JLabel("Principle: ");
            monthlyLabel = new JLabel("Monthly Payment:$ ");
            fillerLabel = new JLabel("Year / APR%");

            //Define Heading
          headingPane.add(ownerLabel);
        headingPane.add(assignmentLabel);
            headingPane.add(separatorLabel);


        //New JPanel
            JPanel actionPane = new JPanel(new GridLayout(0,1));

        //Define Buttons
          calculateButton = new JButton("Calculate");
            exitButton = new JButton("Exit");


        //Create the combo box, select the item at index 4.

        JComboBox comboBox = new JComboBox();


        JComboBox petList = new JComboBox(petStrings);
        petList.setSelectedIndex(4);
        petList.addActionListener(this);

        //Define Text Pane
        textPane.add(principleLabel);
            textPane.add(principalField);
            textPane.add(fillerLabel);
            textPane.add(petList);
            textPane.add(monthlyLabel);
            textPane.add(paymentField);
            textPane.add(calculateButton);
            textPane.add(exitButton);


            calculateButton.addActionListener(this);
            exitButton.addActionListener(this);




        //Lay out

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

             add(headingPane, BorderLayout.PAGE_START);
             add(actionPane,BorderLayout.PAGE_END);
             add(textPane,BorderLayout.CENTER);

         //setting up scroll bars
             scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
             actionPane.add(scroll);




    }



    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e)

    {

             DecimalFormat pTwo = new DecimalFormat("0.00");

             Object source = e.getSource();
             if (source == calculateButton)
             {
          //int index = petList.getSelectedIndex();
          //term = termArray[index];
          //rate = rateArray[index];

                //term = Integer.parseInt(termField.getText());
                //principal = Double.parseDouble(principalField.getText());
                  //rate = Double.parseDouble(rateField.getText());
                //principal = 100000.00;
                //term = 7;
                  //rate = .0535;
                        number_payments = (term * 12);

                        rate = (rate / 100);

                        monthly_interest = (rate  / 12);

                        monthly_payment = (principal * monthly_interest) / (1 - Math.pow(1/ (1 + monthly_interest), number_payments));

                        paymentField.setText(pTwo.format(monthly_payment));



            new_principle = principal;
            String titles = "Month\t Principal\t\tInterest\t\t\tBalance\n";
                  displayArea.setText(titles);

            for(int counter = 0; counter <number_payments; counter++)
                  {
                        //Scroll Area


                        cm_interest = new_principle * monthly_interest;    //current monthly interest

                    m_principle= monthly_payment - cm_interest ;
                                                                  /* this is your monthly payment minus your monthly interest,
                                                                                       so it is the amount of principal you pay for that month */

                        new_balance = new_principle - m_principle;
                                                                  /*this is the new balance of your principal of your loan*/

                        new_principle = new_balance;

                        mortint = cm_interest * 100;

                //displayArea.setCaretPosition(0);
                        displayArea.append((counter + 1) + ")\t" + pTwo.format(m_principle) + "\t\t" + pTwo.format(mortint)+ "\t\t" + pTwo.format(new_balance) + "\n");

                  }



            }
                  else if (source == exitButton)
                  {
                     System.exit(0);
            }

              JComboBox cb = (JComboBox)e.getSource();
              String petName = (String)cb.getSelectedItem();

    }


    private static void RShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Rebecca'a Mortgage Calc");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new POS407_W3_RMR();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                RShowGUI();
            }
        });
    }
}

Comment
Watch Question
Principal Technologist
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE