Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    Thumbs up Help

    I cannot figure out how to get the radio buttons to work. I am trying to get the user to put in the principle balance then then can select a button for their options on rate and term. I am also having problem getting the amortization table to work. Any suggestions?
    Code:
    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 mortgageCalculatorMcCabe3 extends JFrame implements ActionListener {
    
    int term = 0;
    double principal = 0;
    double rate = 0;
    double monthlyPayment = 0;
    double interest = 0;
    String mTerm[] = {"7", "15", "30"};
    String mInterst[] = {"5.35%", "5.50%", "5.75%"};
    JPanel row1 = new JPanel();
    JLabel mortgage_label = new JLabel("Mortgage Payment Calculator", JLabel.CENTER);
    
    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principal_label = new JLabel("Loan Amount: $",JLabel.LEFT);
    JTextField principal_txt = new JTextField(10);
    
    JPanel row3 = new JPanel(new GridLayout(1, 2));
    JLabel term_label = new JLabel("Term of Loan(Years):",JLabel.LEFT);
    JTextField term_txt = new JTextField(10);
    
    JPanel row4 = new JPanel(new GridLayout(1, 2));
    JLabel rate_label = new JLabel("Interest Rate:", JLabel.LEFT);
    JTextField rate_txt = new JTextField(10);
    
    JPanel radioPanel = new JPanel();
    JRadioButton button_A = new JRadioButton("7 Years at 5.35%" , false);
    JRadioButton button_B = new JRadioButton("15 Years at 5.50%" , false);
    JRadioButton button_C = new JRadioButton("30 Years at 5.75%", false);
    JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel payment_label = new JLabel("Total Monthly Payment: $", JLabel.LEFT);
    JTextField payment_txt = new JTextField(10);
    
    //***************create buttons***************
    JPanel button = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
    JButton clearButton = new JButton("Clear");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate");
    //***************set text area to diplay payments***************
    JTextArea displayArea = new JTextArea(10, 45);
    JScrollPane scroll = new JScrollPane(displayArea);
    public mortgageCalculatorMcCabe3()
    {
    super ("Ryan's Mortgage Calculator");
    setSize(550, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    
    Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
    
    pane.add(row1);
    row1.add(mortgage_label);
    row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
    row1.setBorder( rowborder);
    
    pane.add(row2);
    row2.add(principal_label);
    row2.add(principal_txt);
    row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
    row2.setBorder( rowborder);
    
    pane.add(row3);
    row3.add(term_label);
    row3.add(term_txt);
    row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
    row3.setBorder( rowborder);
    
    pane.add(row4);
    row4.add(rate_label);
    row4.add(rate_txt);
    row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
    row4.setBorder( rowborder);
    
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(button_A);
    bgroup.add(button_B);
    bgroup.add(button_C);
    
    radioPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4 ));
    radioPanel.add(button_A);
    radioPanel.add(button_B);
    radioPanel.add(button_C);
    pane.add(radioPanel);
    radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height));
    radioPanel.setBorder( rowborder);
    
    pane.add(row5);
    row5.add(payment_label);
    row5.add(payment_txt);
    payment_txt.setEnabled(false); //set payment amount as not editable
    row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
    row5.setBorder( rowborder);
    
    button.add(calculateButton);
    button.add(clearButton);
    button.add(exitButton);
    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);
    button_A.addActionListener(this);
    button_B.addActionListener(this);
    button_C.addActionListener(this);
    
    }
    public void actionPerformed(ActionEvent event){
    
    Object command = event.getSource();
    if(command == calculateButton)
    
    {
    try
    {
    Payment();
    }
    
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Invaild Entry! Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    }
    //**********Set rate and term based on which item in the combobox is selected**********
    
    if(button_A.isSelected() == true)
    try
    {
    Payment();
    rate = 5.35;
    term = 7;
    }
    
    if(button_B.isSelected() == true)
    try
    {
    Payment();
    rate = 5.5;
    term = 15;
    }
    
    if (button_C.isSelected() == true)
    try
    {
    Payment();
    rate = 5.75;
    term = 30;
    }
    
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Invaild Entry! Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    
    if(command == clearButton)
    {
    principal_txt.setText(null);
    term_txt.setText(null);
    rate_txt.setText(null);
    payment_txt.setText(null);
    displayArea.setText(null);
    }
    
    if(command == exitButton)
    {
    System.exit(0);
    }
    }
    
    //***************payment calculation*****************
    
    public void Payment()
    {
    
    double principal = Double.parseDouble(principal_txt.getText()); //Principal amount
    float rate1 = Float.parseFloat((String)rate_txt.getText()); //Interest rate
    double rate = rate1/100;
    int term = Integer.parseInt((String)term_txt.getText()); //Length of Loan in Years
    double perMonthRate = rate/12; //Monthly interest rate
    double termMonths = term*12; //Number of months over which loan is amortized
    
    //*************calculation formula********************
    
    double monthlyPayment = principal * (perMonthRate/(1 - Math.pow((1 + perMonthRate),(-termMonths))));
    
    /*formatting variables*/
    DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
    DecimalFormat pf = new DecimalFormat("#,##0.00%"); //percentages
    DecimalFormat mi = new DecimalFormat("#,##0.000%"); //percentages
    payment_txt.setText("" + df.format(monthlyPayment));
    
    /*Amoritization variables*/
    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 paymentNumber = 1; //Variable used to set payment number.
    
    /*This loop increments the term length in years.*/
    for(int y = 1; y <= term; y++)
    { //start outer loop
    displayArea.append(""); //Inserts a blank line
    /*This loop is used to calculate and display the payment schedule
    information.*/
    for (int m = 0; m<12; m++)
    { /*start inner loop*/
    interestPaid = principalBalance*perMonthRate;
    monthlyPrincipal = monthlyPayment-interestPaid;
    principalBalance = principalBalance - monthlyPrincipal;
    displayArea.append("Month");
    df.format(monthlyPrincipal);
    df.format(interestPaid);
    df.format(principalBalance);
    paymentNumber++;
    displayArea.setCaretPosition(0);
    }
    }
    }
    public static void main(String[] arguments) {
    mortgageCalculatorMcCabe3 mortgageCalculatorMcCabe3 = new mortgageCalculatorMcCabe3();
    }
    }
    Last edited by Hack; 03-03-2009 at 08:42 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I guess I now see some of the design decisions you made which led you to choose to have special case methods to calculate the payment [reflected in the code you posted later to which I commented]. Your calculate methods in this code calls the Payment() method before you set the values to be used in the chosen term. The calculation is performed and returned before you modify value of the important data members.

    Have you learned about passing parameters (arguments) to a method? What do you think would be the best general purpose payment method? How about payment( principal, term, rate )? You can then determine not only the interest charged in this payment but then the principal pay down - which will help with the problem you were having with generating an amortization table.

    A little syntax issue: the name of methods, by standard usage, starts with a lower case letter. Usually, classes start with an uppercase letter. When I see Payment, I am looking for a class named Payment. When I see payment(), I am looking for a method.
    Last edited by nspils; 03-07-2009 at 02:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center