[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

Problem with Amortizing in Mortgage Calculator

Asked by rhivka in New to Java Programming

Tags: mortgage, jradiobutton

Hey guys! I have been working out the kinks in this thing all day and I can't seem to get my Amortization to work. I got everything else to work how I want it. I've double checked all my formulas, and I think it's in the looping. But I'm not sure what to change from here. Could you take a look at it and tell me what you think? Thanks in advance!

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class MortgageCalculator extends JFrame implements ActionListener {

      int term = 0;
      double principal = 0;
      double rate = 0;
      double monthlyPayment = 0;
      double interest = 0;
      int notePeriod = 0;

      String mTerm[] = {"7", "15", "30"};
      String mInterst[] = {"5.35", "5.50", "5.75"};

      JPanel row1 = new JPanel();
      JLabel mortgageLabel = new JLabel("MORTGAGE PAYMENT CALCULATOR", JLabel.CENTER);

      JPanel row2 = new JPanel(new GridLayout(1, 2));
      JLabel principalLabel = new JLabel("Mortgage Principal $",JLabel.LEFT);
      JTextField principalTxt = new JTextField(10);

      JPanel row3 = new JPanel(new GridLayout(1, 2));
      JLabel termLabel = new JLabel("Mortgage Term (Yrs)",JLabel.LEFT);
      JTextField termTxt = new JTextField(10);

      JPanel row4 = new JPanel(new GridLayout(1, 2));
      JLabel rateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
      JTextField rateTxt = new JTextField(10);

      JPanel radioPanel = new JPanel();
      JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
      JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
      JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);

      JPanel row5 = new JPanel(new GridLayout(1, 2));
      JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
      JTextField monthlyPaymentTxt = new JTextField(10);

      //create buttons
      JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
      JButton amortizeButton = new JButton("Amortize Payments");
      JButton clearButton = new JButton("Clear");
      JButton exitButton = new JButton("Exit");
      JButton calculateButton = new JButton("Calculate");

      //create textarea to diplay payments
      JTextArea displayArea = new JTextArea(10, 45);
      JScrollPane scroll = new JScrollPane(displayArea);

      public MortgageCalculator()
      {
            super ("Mortgage Payment Calculator by S Kemen");
            setSize(550, 500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container pane = getContentPane();

            Border rowborder = new EmptyBorder( 3, 10, 3, 10 );

            pane.add(row1);
            row1.add(mortgageLabel);
            row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
            row1.setBorder( rowborder);

            pane.add(row2);
            row2.add(principalLabel);
            row2.add(principalTxt);
            row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
            row2.setBorder( rowborder);

            pane.add(row3);
            row3.add(termLabel);
            row3.add(termTxt);
            row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
            row3.setBorder( rowborder);

            pane.add(row4);
            row4.add(rateLabel);
            row4.add(rateTxt);
            row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
            row4.setBorder( rowborder);

            ButtonGroup bgroup = new ButtonGroup();
            bgroup.add(buttonA);
            bgroup.add(buttonB);
            bgroup.add(buttonC);

            radioPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4 ));
            radioPanel.add(buttonA);
            radioPanel.add(buttonB);
            radioPanel.add(buttonC);
            pane.add(radioPanel);
            radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height));
            radioPanel.setBorder( rowborder);

            pane.add(row5);
            row5.add(monthlyPaymentLabel);
            row5.add(monthlyPaymentTxt);
            monthlyPaymentTxt.setEnabled(false);  //set payment amount uneditable
            row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
            row5.setBorder( rowborder);

            button.add(calculateButton);
            button.add(clearButton);
            button.add(exitButton);
            button.add(amortizeButton);
            pane.add(button);
            button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height));

            scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            pane.add(scroll);

            pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
            setVisible(true);
            setContentPane(pane);

            //add listeners
            clearButton.addActionListener(this);
            exitButton.addActionListener(this);
            calculateButton.addActionListener(this);
            amortizeButton.addActionListener(this);
            buttonA.addActionListener(this);
            buttonB.addActionListener(this);
            buttonC.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event)
    {
        Object command = event.getSource();
           if(command == calculateButton)
      {
      try
                       {
            principal = Double.parseDouble(principalTxt.getText());
        }
      catch(NumberFormatException e)
        {
            //catch null pointer exception if Principal is null
            JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
      try
         {
            term = Integer.parseInt(termTxt.getText());
             rate = Double.parseDouble(rateTxt.getText());
         }
            catch(NumberFormatException e)
            {
       //Set rate and term based on which item in the combobox is selected
       if(buttonA.isSelected() == true)
       {
            rate = 5.35;
            term = 7;
      }
      else if(buttonB.isSelected() == true)
      {
            rate = 5.5;
            term = 15;
      }
      else if (buttonC.isSelected() == true)
      {
            rate = 5.75;
            term = 30;
      }
      else
      {
      //If no button is selected, this is an actual error.  Throw an exception
      JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR",      JOptionPane.ERROR_MESSAGE);
      }
               }
              double interest = rate / 100 / 12;               //Monthly interst rate
              double notePeriod= term * 12;                    //Number of months over which loan is amortized

              //calculation formula
              double monthlyPayment = (principal * interest) / (1 - Math.pow(1 + interest, -notePeriod));

              //formatting variables
              DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
              DecimalFormat pf = new DecimalFormat("#,##0.00%");      //percentages
              DecimalFormat mi = new DecimalFormat("#,##0.000%");     //percentages
              monthlyPaymentTxt.setText("" + df.format(monthlyPayment));
        }
        if(command == clearButton)
        {
             principalTxt.setText(null);
             monthlyPaymentTxt.setText(null);
             displayArea.setText(null);
        }
        if(command == exitButton)
        {
             System.exit(0);
        }
        if (command == amortizeButton)
         {
      //formatting variables
      DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
      DecimalFormat pf = new DecimalFormat("#,##0.00%");      //percentages
      DecimalFormat mi = new DecimalFormat("#,##0.000%");     //percentages

      //Amoritization variables
      double loanBalance = notePeriod * monthlyPayment;
      double interestPaid = 0;                                       //Amount of interest paid on the loan
      double monthlyPrincipal = 0;                                //Amount of principal in each monthly payment
      double principalBalance = principal;                    //runing total of principal after payment
      int y = 0;                                                 //Counter

       //This loop is used to calculate and display the payment schedule information
      for(y = 1; y <= notePeriod; y++)
      {                                                                                         //start loop
                            displayArea.append("");                             //Inserts a blank line

            //start inner loop
            interestPaid = principalBalance * interest;
            monthlyPrincipal = monthlyPayment - interestPaid;
            loanBalance = loanBalance - monthlyPayment;
            principalBalance = principalBalance - monthlyPrincipal;

            displayArea.append("Month "+y+"\t\t"+df.format(monthlyPrincipal)+"\t\t"
                  +df.format(interestPaid)+"\t\t"+df.format(principalBalance)+"\n");
                displayArea.setCaretPosition(0);
            }
      }
    }
    public static void main (String[] arguments) //Main Method
    {
        MortgageCalculator smc = new MortgageCalculator();
        smc.setVisible(true);
        smc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 } //End of program
 
Loading Advertisement...
 
[+][-]07/17/07 12:23 AM, ID: 19502748Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: New to Java Programming
Tags: mortgage, jradiobutton
Sign Up Now!
Solution Provided By: objects
Participating Experts: 1
Solution Grade: A
 
[+][-]07/16/07 06:56 PM, ID: 19501648Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/17/07 12:29 AM, ID: 19502767Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92