Buttons do not function properly in mortgage application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LadiPrather
    New Member
    • Mar 2010
    • 11

    Buttons do not function properly in mortgage application

    When I press the 7 year with 5.35% the amort table goes across the screen instead of down. When I press the 30 year with 5.75% it exit the program. I cannot figure out what I did wrong, will someone please check and help quick. Here are my codes.

    Code:
      //Set up and initialize buttons for GUI
           bCalc7 = new JButton ("7 Year, 5.32%");
           bCalc7.setActionCommand ("Calculate7");
           bCalc15 = new JButton ("15 Years, 5.50%");
           bCalc15.setActionCommand ("Calculate15");
           bCalc30 = new JButton ("30 Years, 5.75%");
           bCalc30.setActionCommand ("Calculate30");
           bClear = new JButton ("Clear All Fields");
           bClear.setActionCommand ("Clear");
           bExit = new JButton ("Exit Program");
           bExit.setActionCommand ("Exit");
    
           //Set up labels and field sizes for GUI
           labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
           labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
           labelPayment = new JLabel ("Monthly Payment:");
           labelLoanAmount = new JLabel ("Amount of Loan:");
           fieldPayment = new JTextField ("", 12);
           fieldLoanAmount = new JTextField ("", 10);
           labelAmortTable = new JLabel ("Amortization Table");
           areaAmortTable = new JTextArea (10, 300);
    
           JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
             //Set up listerners for each button
           bCalc7.addActionListener (this);
           bCalc15.addActionListener (this);
           bCalc30.addActionListener (this);
           bClear.addActionListener (this);
           bExit.addActionListener (this);
    
           //Construct GUI and set layout
           JPanel calu = new JPanel();
           calu.setLayout (null);
    
           calu.add (labelTitle);
           labelTitle.setBounds (110,30,700,15);
    
           calu.add (labelInstructions);
           labelInstructions.setBounds(30, 70, 450, 15);
    
           calu.add (labelLoanAmount);
           labelLoanAmount.setBounds(130, 110, 100, 25);
    
           calu.add (fieldLoanAmount);
           fieldLoanAmount.setBounds(240, 110, 100, 25);
    
           calu.add (bCalc7);
           bCalc7.setBounds(40, 150, 125, 30);
    
           calu.add (bCalc15);
           bCalc15.setBounds(180, 150, 125, 30);
    
           calu.add (bCalc30);
           bCalc30.setBounds(320, 150, 125, 30);
    
           calu.add (labelPayment);
           labelPayment.setBounds(130, 200, 100, 25);
    
           calu.add (fieldPayment);
           fieldPayment.setBounds(240, 200, 100, 25);
           fieldPayment.setEditable(false);
    
           calu.add (labelAmortTable);
           labelAmortTable.setBounds(180, 250, 300, 25);
    
           calu.add (scrollPane);
           scrollPane.setBounds(50, 280, 400, 270);
           areaAmortTable.setEditable(false);
    
           calu.add (bClear);
           bClear.setBounds(110, 570, 125, 30);
    
           calu.add (bExit);
           bExit.setBounds(250, 570, 125, 30);
    
           this.setContentPane(calu);
           this.pack();
           this.setTitle("Mortgage Calculator5");
    
           //Set windwo size
           Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
           setSize (600, 700);
    
            }
       public double loanAmount ()
       {
          double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
          return loanAmount;
    
       }
    
    
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Where is the code for the actionCommands?

    Comment

    • LadiPrather
      New Member
      • Mar 2010
      • 11

      #3
      I am using the actionListener, it runs but those two buttons are not doing what they should, do I need to post my hold program. Thanks for any help you can give, I am to turn this in tonight.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        The GUI code is useless for debugging eg:
        calu.add (labelAmortTabl e);

        There doesn't appear to be an error with the code posted so far.

        Comment

        • LadiPrather
          New Member
          • Mar 2010
          • 11

          #5
          Here is the the whole set up
          package mortgagecalcula tor5;

          Code:
          /**
           *
           * 
           */
          
          import java.text.*;
          import java.math.*;
          import java.io.*;
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;
          import java.lang.String;
          import javax.swing.JLabel;
          
          
          public class Main extends JFrame {
          
              /**
               * @param args the command line arguments
               */
           public class MortgageCalculator5 {
             }
           {
          
           }
              public static void main(String[] args) {
                JFrame window = new MortCalcu5GUI();
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                window.setVisible(true);
          
              }
          }
          class MortCalcu5GUI extends JFrame implements ActionListener {
          
                  // Declare GUI function variables
              protected JButton bCalc7, bCalc15, bCalc30, bClear, bExit;
              protected JLabel labelPayment, labelLoanAmount, labelTitle, labelInstructions, labelAmortTable;
              protected JTextField fieldPayment, fieldLoanAmount;
              protected JTextArea areaAmortTable;
          
              //Declare variables and loan array and initialize with values
              double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount;
             int[] loanTerm = {7, 15, 30}; //loan term for 7 years, 15 years, and 30 years
             double[] intRate = {5.35, 5.50, 5.75};//interest rates for 7 years, 15 years, and 30 years
             double loanAmt = loanAmount;
          
          
                     public void actionPerformed (ActionEvent e) {
          
                 //Perform actions based upon which button is pressed (provides looping function)
                    if ("Calculate7".equals(e.getActionCommand())) {
          
                        DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
                        NumberFormat currency = NumberFormat.getCurrencyInstance();
                        double paymentAmount = CalculatePayment7();
                        fieldPayment.setText ("" + (currency.format(paymentAmount)));
                        areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
                        loanAmt = 
                                Double.parseDouble (fieldLoanAmount.getText());
                        for (int x = 1; x<=(loanTerm[0]*12); x++)//Loop through all monthly payments for each loan
                        {
                            Balance = loanAmt;//Set Balance to Loan Amount
                       //Calculations on monthly payment
                            InterestPaid = (((intRate[0]/100)/12)*Balance);//Monthly Interest paid
                            PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
                            loanAmt = (Balance-PrinciplePaid);//loan balance after payment
                            areaAmortTable.append (x + "\t" + currency.format (loanAmt) + " \t\t" + currency.format(InterestPaid) + "n");
                             }
                        if (loanAmt <= 0)//ending statement, balance at $0.00
                        {
                            areaAmortTable.append ("\tRemaining balance = $0.00");
                            //paymentNumber = 0;
                            //InterestedPaid = 0.0;
                        }
          
                    }
                    else if ("Calculate15".equals(e.getActionCommand())){
                    
                        DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
                        NumberFormat currency = NumberFormat.getCurrencyInstance();
                        double paymentAmount = CalculatePayment15();
                        fieldPayment.setText ("" + (currency.format(paymentAmount)));
                        areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
                        loanAmt = Double.parseDouble(fieldLoanAmount.getText());
                        for (int x = 1; x<=(loanTerm[1]*12); x++)//Loop through all monthly payment for each loan
                        {
                            Balance = loanAmt;
                       //Calculation on monthly payment
                            InterestPaid = ((intRate[1]/100/12)*Balance);//Monthly Interest paid
                            PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
                            loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
                            areaAmortTable.append (x + '\t' + currency.format (loanAmt) + "\t\t" + currency.format (InterestPaid)+ "\n");
                        }
                        if (loanAmt <= 0)//ending statement, blance at $0.00
                        {
                            areaAmortTable.append ("\tRemaining balance = $0.00");
                            //paymentNumber =0;
                            //InterestPaid = 0.0;
                        }
                    }
                    else if ("calculate30".equals(e.getActionCommand()))
                    {
                        DecimalFormat decimalPlaces = new DecimalFormat("0.00");
                        NumberFormat currency = NumberFormat.getCurrencyInstance();
                        double paymentAmount = CalculatePayment30();
                        fieldPayment.setText ("" + (currency.format (paymentAmount)));
                        areaAmortTable.append ("payment # \t Remaining Balance \t Interest Paid\n");
                        loanAmt = Double.parseDouble (fieldLoanAmount.getText());
                        for (int x = 1; x <=(loanTerm[2]*12); x++)//Loop through all monthly payments for each loan
                        {
                            Balance = loanAmt;
                      //Calulations on monthly payments
                            InterestPaid = ((intRate[2]/100/12)*Balance);//Monthly Interest Paid
                            PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
                            loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
                            areaAmortTable.append (x + "\t" + currency.format(loanAmt) + "\t\t" + currency.format(InterestPaid)+"\n");
                        }
                        if (loanAmt <= 0)//ending statement, balance at $0.00
                        {
                           areaAmortTable.append ("\tRemaining balance = $0.00");
                           //paymentNumber = 0;
                           //InterstPaid =0.0;
                        }
          
                        }
                    else if ("Clear".equals(e.getActionCommand()))
                    {
                        ClearFields();
                    }
                    else
                    {
                        //end and close application
                        System.exit(0);
                    }
                    }
             public double CalculatePayment7 ()
             {
                 //Check for numeric input
                 try
                 {
                     //Perform payment calculations if input is valid
                     fieldPayment.setText ("");
                     double Payment = (loanAmount()* (intRate[0]/100/12)) / (1- Math.pow(1/(1 + (intRate[0]/100/12)), loanTerm[0]*12));
                     return Payment;
                 }
                 catch (NumberFormatException event)
                 {
                     //Display error message if input is invalid
                     JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!","ERROR",
                             JOptionPane.ERROR_MESSAGE);
                     return 0;
                 }
             }
             public double CalculatePayment15()
             {
                 //Check for valid input
                 try
                 {
                     //Perform payment calculations if input is valid
                     fieldPayment.setText ("");
                     double Payment = (loanAmount( )* (intRate[1]/100/12)) / (1 - Math.pow(1/(1 +(intRate[1]/100/12)),loanTerm[1]*12));
                     return Payment;
                 }
                 catch (NumberFormatException event)
                 {
                 //Display erroe message if input is invalid
                     JOptionPane.showMessageDialog(null, "Invalid entry!\nPlease enter only numeric values!!",
                             "ERROR", JOptionPane.ERROR_MESSAGE);
                     return 0;
                 }
             }
             public double CalculatePayment30()
             {
                 //Check for vilid numeric input
                 try
                 {
                     //Perform payment calculation if input in valid
                     fieldPayment.setText ("");
                     double Payment = (loanAmount()* (intRate[2]/100/12))/(1 - Math.pow (1/(1 + (intRate[2]/100/12)), loanTerm[2]*12));
                     return Payment;
                 }
                 catch (NumberFormatException event)
                 {
                   //Display error message if input is invalid
                     JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!",
                             "ERROR", JOptionPane.ERROR_MESSAGE);
                     return 0;
                 }
             }
             public void ClearFields()
             {
                    //Set all text to blank
                 fieldPayment.setText ("");
                 fieldLoanAmount.setText ("");
                 areaAmortTable.setText ("");
             }
             public MortCalcu5GUI ()
             {
                 //Set up and initialize buttons for GUI
                 bCalc7 = new JButton ("7 Year, 5.32%");
                 bCalc7.setActionCommand ("Calculate7");
                 bCalc15 = new JButton ("15 Years, 5.50%");
                 bCalc15.setActionCommand ("Calculate15");
                 bCalc30 = new JButton ("30 Years, 5.75%");
                 bCalc30.setActionCommand ("Calculate30");
                 bClear = new JButton ("Clear All Fields");
                 bClear.setActionCommand ("Clear");
                 bExit = new JButton ("Exit Program");
                 bExit.setActionCommand ("Exit");
          
                 //Set up labels and field sizes for GUI
                 labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
                 labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
                 labelPayment = new JLabel ("Monthly Payment:");
                 labelLoanAmount = new JLabel ("Amount of Loan:");
                 fieldPayment = new JTextField ("", 12);
                 fieldLoanAmount = new JTextField ("", 10);
                 labelAmortTable = new JLabel ("Amortization Table");
                 areaAmortTable = new JTextArea (10, 300);
          
                 JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                         JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
          
                   //Set up listerners for each button
                 bCalc7.addActionListener (this);
                 bCalc15.addActionListener (this);
                 bCalc30.addActionListener (this);
                 bClear.addActionListener (this);
                 bExit.addActionListener (this);
          
                 //Construct GUI and set layout
                 JPanel calu = new JPanel();
                 calu.setLayout (null);
          
                 calu.add (labelTitle);
                 labelTitle.setBounds (110,30,700,15);
          
                 calu.add (labelInstructions);
                 labelInstructions.setBounds(30, 70, 450, 15);
          
                 calu.add (labelLoanAmount);
                 labelLoanAmount.setBounds(130, 110, 100, 25);
          
                 calu.add (fieldLoanAmount);
                 fieldLoanAmount.setBounds(240, 110, 100, 25);
          
                 calu.add (bCalc7);
                 bCalc7.setBounds(40, 150, 125, 30);
          
                 calu.add (bCalc15);
                 bCalc15.setBounds(180, 150, 125, 30);
          
                 calu.add (bCalc30);
                 bCalc30.setBounds(320, 150, 125, 30);
          
                 calu.add (labelPayment);
                 labelPayment.setBounds(130, 200, 100, 25);
          
                 calu.add (fieldPayment);
                 fieldPayment.setBounds(240, 200, 100, 25);
                 fieldPayment.setEditable(false);
          
                 calu.add (labelAmortTable);
                 labelAmortTable.setBounds(180, 250, 300, 25);
          
                 calu.add (scrollPane);
                 scrollPane.setBounds(125, 280, 400, 270);
                 areaAmortTable.setEditable(false);
          
                 calu.add (bClear);
                 bClear.setBounds(110, 570, 125, 30);
          
                 calu.add (bExit);
                 bExit.setBounds(250, 570, 125, 30);
          
                 this.setContentPane(calu);
                 this.pack();
                 this.setTitle("Mortgage Calculator5");
          
                 //Set windwo size
                 Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
                 setSize (675, 700);
          
                  }
             public double loanAmount ()
             {
                double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
                return loanAmount;
          
             }
          
          
          }

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            bCalc30.setActi onCommand ("Calculate30") ;
            else if ("calculate30". equals(e.getAct ionCommand()))

            The case of C on "calculate3 0" differs.

            Don't use exit as the default case. Do an explicit else if test for it. Then have the default case be some sort of error alert, eg ERROR button is calling command .... but it's not found.

            Comment

            • LadiPrather
              New Member
              • Mar 2010
              • 11

              #7
              Thank you that fixed that field it works, but the Calculate7 still scrolls across the page instead of down like the others and it has an n. here is what it look like
              $8,828.92n3 ------->1,940,629.66 , and so on the arrow line is not there just to show you the direction it is going. It is set up the same as the others. I found one other error there corrected it but it still does the same thing.

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                You noticed the n?
                You put "n" instead of "\n", like in the other lines.

                I notice you have a lot of repeated code.
                You might want to consider merging the code together, simply changing the variables that differ...
                eg:
                Code:
                if ("Calculate".equals(e.getActionCommand().Substring(0,9)){
                   ...
                   double interestRate;
                  if ("Calculate7".equals(e.getActionCommand().Substring(0,9)){
                     double paymentAmount = CalculatePayment7(); 
                     interestRate = intRate[0];
                   } else if ("Calculate15".equals(e.getActionCommand().Substring(0,9)){
                     double paymentAmount = CalculatePayment15(); 
                     interestRate = intRate[1];
                   } else if ....
                   ...
                }

                Comment

                • LadiPrather
                  New Member
                  • Mar 2010
                  • 11

                  #9
                  Thank you, you must been doing this a long time. Again Thanks

                  Comment

                  Working...