473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.   //Set up and initialize buttons for GUI
  2.        bCalc7 = new JButton ("7 Year, 5.32%");
  3.        bCalc7.setActionCommand ("Calculate7");
  4.        bCalc15 = new JButton ("15 Years, 5.50%");
  5.        bCalc15.setActionCommand ("Calculate15");
  6.        bCalc30 = new JButton ("30 Years, 5.75%");
  7.        bCalc30.setActionCommand ("Calculate30");
  8.        bClear = new JButton ("Clear All Fields");
  9.        bClear.setActionCommand ("Clear");
  10.        bExit = new JButton ("Exit Program");
  11.        bExit.setActionCommand ("Exit");
  12.  
  13.        //Set up labels and field sizes for GUI
  14.        labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
  15.        labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
  16.        labelPayment = new JLabel ("Monthly Payment:");
  17.        labelLoanAmount = new JLabel ("Amount of Loan:");
  18.        fieldPayment = new JTextField ("", 12);
  19.        fieldLoanAmount = new JTextField ("", 10);
  20.        labelAmortTable = new JLabel ("Amortization Table");
  21.        areaAmortTable = new JTextArea (10, 300);
  22.  
  23.        JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  24.                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  25.  
  26.          //Set up listerners for each button
  27.        bCalc7.addActionListener (this);
  28.        bCalc15.addActionListener (this);
  29.        bCalc30.addActionListener (this);
  30.        bClear.addActionListener (this);
  31.        bExit.addActionListener (this);
  32.  
  33.        //Construct GUI and set layout
  34.        JPanel calu = new JPanel();
  35.        calu.setLayout (null);
  36.  
  37.        calu.add (labelTitle);
  38.        labelTitle.setBounds (110,30,700,15);
  39.  
  40.        calu.add (labelInstructions);
  41.        labelInstructions.setBounds(30, 70, 450, 15);
  42.  
  43.        calu.add (labelLoanAmount);
  44.        labelLoanAmount.setBounds(130, 110, 100, 25);
  45.  
  46.        calu.add (fieldLoanAmount);
  47.        fieldLoanAmount.setBounds(240, 110, 100, 25);
  48.  
  49.        calu.add (bCalc7);
  50.        bCalc7.setBounds(40, 150, 125, 30);
  51.  
  52.        calu.add (bCalc15);
  53.        bCalc15.setBounds(180, 150, 125, 30);
  54.  
  55.        calu.add (bCalc30);
  56.        bCalc30.setBounds(320, 150, 125, 30);
  57.  
  58.        calu.add (labelPayment);
  59.        labelPayment.setBounds(130, 200, 100, 25);
  60.  
  61.        calu.add (fieldPayment);
  62.        fieldPayment.setBounds(240, 200, 100, 25);
  63.        fieldPayment.setEditable(false);
  64.  
  65.        calu.add (labelAmortTable);
  66.        labelAmortTable.setBounds(180, 250, 300, 25);
  67.  
  68.        calu.add (scrollPane);
  69.        scrollPane.setBounds(50, 280, 400, 270);
  70.        areaAmortTable.setEditable(false);
  71.  
  72.        calu.add (bClear);
  73.        bClear.setBounds(110, 570, 125, 30);
  74.  
  75.        calu.add (bExit);
  76.        bExit.setBounds(250, 570, 125, 30);
  77.  
  78.        this.setContentPane(calu);
  79.        this.pack();
  80.        this.setTitle("Mortgage Calculator5");
  81.  
  82.        //Set windwo size
  83.        Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
  84.        setSize (600, 700);
  85.  
  86.         }
  87.    public double loanAmount ()
  88.    {
  89.       double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
  90.       return loanAmount;
  91.  
  92.    }
  93.  
  94.  
  95. }
  96.  
Mar 27 '10 #1

✓ answered by jkmyoung

bCalc30.setActionCommand ("Calculate30");
else if ("calculate30".equals(e.getActionCommand()))

The case of C on "calculate30" 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.

8 1805
jkmyoung
2,057 Expert 2GB
Where is the code for the actionCommands?
Mar 29 '10 #2
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.
Mar 29 '10 #3
jkmyoung
2,057 Expert 2GB
The GUI code is useless for debugging eg:
calu.add (labelAmortTable);

There doesn't appear to be an error with the code posted so far.
Mar 29 '10 #4
Here is the the whole set up
package mortgagecalculator5;

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  *
  3.  * 
  4.  */
  5.  
  6. import java.text.*;
  7. import java.math.*;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import java.lang.String;
  13. import javax.swing.JLabel;
  14.  
  15.  
  16. public class Main extends JFrame {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.  public class MortgageCalculator5 {
  22.    }
  23.  {
  24.  
  25.  }
  26.     public static void main(String[] args) {
  27.       JFrame window = new MortCalcu5GUI();
  28.       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.       window.setVisible(true);
  30.  
  31.     }
  32. }
  33. class MortCalcu5GUI extends JFrame implements ActionListener {
  34.  
  35.         // Declare GUI function variables
  36.     protected JButton bCalc7, bCalc15, bCalc30, bClear, bExit;
  37.     protected JLabel labelPayment, labelLoanAmount, labelTitle, labelInstructions, labelAmortTable;
  38.     protected JTextField fieldPayment, fieldLoanAmount;
  39.     protected JTextArea areaAmortTable;
  40.  
  41.     //Declare variables and loan array and initialize with values
  42.     double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount;
  43.    int[] loanTerm = {7, 15, 30}; //loan term for 7 years, 15 years, and 30 years
  44.    double[] intRate = {5.35, 5.50, 5.75};//interest rates for 7 years, 15 years, and 30 years
  45.    double loanAmt = loanAmount;
  46.  
  47.  
  48.            public void actionPerformed (ActionEvent e) {
  49.  
  50.        //Perform actions based upon which button is pressed (provides looping function)
  51.           if ("Calculate7".equals(e.getActionCommand())) {
  52.  
  53.               DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
  54.               NumberFormat currency = NumberFormat.getCurrencyInstance();
  55.               double paymentAmount = CalculatePayment7();
  56.               fieldPayment.setText ("" + (currency.format(paymentAmount)));
  57.               areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
  58.               loanAmt = 
  59.                       Double.parseDouble (fieldLoanAmount.getText());
  60.               for (int x = 1; x<=(loanTerm[0]*12); x++)//Loop through all monthly payments for each loan
  61.               {
  62.                   Balance = loanAmt;//Set Balance to Loan Amount
  63.              //Calculations on monthly payment
  64.                   InterestPaid = (((intRate[0]/100)/12)*Balance);//Monthly Interest paid
  65.                   PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
  66.                   loanAmt = (Balance-PrinciplePaid);//loan balance after payment
  67.                   areaAmortTable.append (x + "\t" + currency.format (loanAmt) + " \t\t" + currency.format(InterestPaid) + "n");
  68.                    }
  69.               if (loanAmt <= 0)//ending statement, balance at $0.00
  70.               {
  71.                   areaAmortTable.append ("\tRemaining balance = $0.00");
  72.                   //paymentNumber = 0;
  73.                   //InterestedPaid = 0.0;
  74.               }
  75.  
  76.           }
  77.           else if ("Calculate15".equals(e.getActionCommand())){
  78.  
  79.               DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
  80.               NumberFormat currency = NumberFormat.getCurrencyInstance();
  81.               double paymentAmount = CalculatePayment15();
  82.               fieldPayment.setText ("" + (currency.format(paymentAmount)));
  83.               areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
  84.               loanAmt = Double.parseDouble(fieldLoanAmount.getText());
  85.               for (int x = 1; x<=(loanTerm[1]*12); x++)//Loop through all monthly payment for each loan
  86.               {
  87.                   Balance = loanAmt;
  88.              //Calculation on monthly payment
  89.                   InterestPaid = ((intRate[1]/100/12)*Balance);//Monthly Interest paid
  90.                   PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
  91.                   loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
  92.                   areaAmortTable.append (x + '\t' + currency.format (loanAmt) + "\t\t" + currency.format (InterestPaid)+ "\n");
  93.               }
  94.               if (loanAmt <= 0)//ending statement, blance at $0.00
  95.               {
  96.                   areaAmortTable.append ("\tRemaining balance = $0.00");
  97.                   //paymentNumber =0;
  98.                   //InterestPaid = 0.0;
  99.               }
  100.           }
  101.           else if ("calculate30".equals(e.getActionCommand()))
  102.           {
  103.               DecimalFormat decimalPlaces = new DecimalFormat("0.00");
  104.               NumberFormat currency = NumberFormat.getCurrencyInstance();
  105.               double paymentAmount = CalculatePayment30();
  106.               fieldPayment.setText ("" + (currency.format (paymentAmount)));
  107.               areaAmortTable.append ("payment # \t Remaining Balance \t Interest Paid\n");
  108.               loanAmt = Double.parseDouble (fieldLoanAmount.getText());
  109.               for (int x = 1; x <=(loanTerm[2]*12); x++)//Loop through all monthly payments for each loan
  110.               {
  111.                   Balance = loanAmt;
  112.             //Calulations on monthly payments
  113.                   InterestPaid = ((intRate[2]/100/12)*Balance);//Monthly Interest Paid
  114.                   PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
  115.                   loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
  116.                   areaAmortTable.append (x + "\t" + currency.format(loanAmt) + "\t\t" + currency.format(InterestPaid)+"\n");
  117.               }
  118.               if (loanAmt <= 0)//ending statement, balance at $0.00
  119.               {
  120.                  areaAmortTable.append ("\tRemaining balance = $0.00");
  121.                  //paymentNumber = 0;
  122.                  //InterstPaid =0.0;
  123.               }
  124.  
  125.               }
  126.           else if ("Clear".equals(e.getActionCommand()))
  127.           {
  128.               ClearFields();
  129.           }
  130.           else
  131.           {
  132.               //end and close application
  133.               System.exit(0);
  134.           }
  135.           }
  136.    public double CalculatePayment7 ()
  137.    {
  138.        //Check for numeric input
  139.        try
  140.        {
  141.            //Perform payment calculations if input is valid
  142.            fieldPayment.setText ("");
  143.            double Payment = (loanAmount()* (intRate[0]/100/12)) / (1- Math.pow(1/(1 + (intRate[0]/100/12)), loanTerm[0]*12));
  144.            return Payment;
  145.        }
  146.        catch (NumberFormatException event)
  147.        {
  148.            //Display error message if input is invalid
  149.            JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!","ERROR",
  150.                    JOptionPane.ERROR_MESSAGE);
  151.            return 0;
  152.        }
  153.    }
  154.    public double CalculatePayment15()
  155.    {
  156.        //Check for valid input
  157.        try
  158.        {
  159.            //Perform payment calculations if input is valid
  160.            fieldPayment.setText ("");
  161.            double Payment = (loanAmount( )* (intRate[1]/100/12)) / (1 - Math.pow(1/(1 +(intRate[1]/100/12)),loanTerm[1]*12));
  162.            return Payment;
  163.        }
  164.        catch (NumberFormatException event)
  165.        {
  166.        //Display erroe message if input is invalid
  167.            JOptionPane.showMessageDialog(null, "Invalid entry!\nPlease enter only numeric values!!",
  168.                    "ERROR", JOptionPane.ERROR_MESSAGE);
  169.            return 0;
  170.        }
  171.    }
  172.    public double CalculatePayment30()
  173.    {
  174.        //Check for vilid numeric input
  175.        try
  176.        {
  177.            //Perform payment calculation if input in valid
  178.            fieldPayment.setText ("");
  179.            double Payment = (loanAmount()* (intRate[2]/100/12))/(1 - Math.pow (1/(1 + (intRate[2]/100/12)), loanTerm[2]*12));
  180.            return Payment;
  181.        }
  182.        catch (NumberFormatException event)
  183.        {
  184.          //Display error message if input is invalid
  185.            JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!",
  186.                    "ERROR", JOptionPane.ERROR_MESSAGE);
  187.            return 0;
  188.        }
  189.    }
  190.    public void ClearFields()
  191.    {
  192.           //Set all text to blank
  193.        fieldPayment.setText ("");
  194.        fieldLoanAmount.setText ("");
  195.        areaAmortTable.setText ("");
  196.    }
  197.    public MortCalcu5GUI ()
  198.    {
  199.        //Set up and initialize buttons for GUI
  200.        bCalc7 = new JButton ("7 Year, 5.32%");
  201.        bCalc7.setActionCommand ("Calculate7");
  202.        bCalc15 = new JButton ("15 Years, 5.50%");
  203.        bCalc15.setActionCommand ("Calculate15");
  204.        bCalc30 = new JButton ("30 Years, 5.75%");
  205.        bCalc30.setActionCommand ("Calculate30");
  206.        bClear = new JButton ("Clear All Fields");
  207.        bClear.setActionCommand ("Clear");
  208.        bExit = new JButton ("Exit Program");
  209.        bExit.setActionCommand ("Exit");
  210.  
  211.        //Set up labels and field sizes for GUI
  212.        labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
  213.        labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
  214.        labelPayment = new JLabel ("Monthly Payment:");
  215.        labelLoanAmount = new JLabel ("Amount of Loan:");
  216.        fieldPayment = new JTextField ("", 12);
  217.        fieldLoanAmount = new JTextField ("", 10);
  218.        labelAmortTable = new JLabel ("Amortization Table");
  219.        areaAmortTable = new JTextArea (10, 300);
  220.  
  221.        JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  222.                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  223.  
  224.          //Set up listerners for each button
  225.        bCalc7.addActionListener (this);
  226.        bCalc15.addActionListener (this);
  227.        bCalc30.addActionListener (this);
  228.        bClear.addActionListener (this);
  229.        bExit.addActionListener (this);
  230.  
  231.        //Construct GUI and set layout
  232.        JPanel calu = new JPanel();
  233.        calu.setLayout (null);
  234.  
  235.        calu.add (labelTitle);
  236.        labelTitle.setBounds (110,30,700,15);
  237.  
  238.        calu.add (labelInstructions);
  239.        labelInstructions.setBounds(30, 70, 450, 15);
  240.  
  241.        calu.add (labelLoanAmount);
  242.        labelLoanAmount.setBounds(130, 110, 100, 25);
  243.  
  244.        calu.add (fieldLoanAmount);
  245.        fieldLoanAmount.setBounds(240, 110, 100, 25);
  246.  
  247.        calu.add (bCalc7);
  248.        bCalc7.setBounds(40, 150, 125, 30);
  249.  
  250.        calu.add (bCalc15);
  251.        bCalc15.setBounds(180, 150, 125, 30);
  252.  
  253.        calu.add (bCalc30);
  254.        bCalc30.setBounds(320, 150, 125, 30);
  255.  
  256.        calu.add (labelPayment);
  257.        labelPayment.setBounds(130, 200, 100, 25);
  258.  
  259.        calu.add (fieldPayment);
  260.        fieldPayment.setBounds(240, 200, 100, 25);
  261.        fieldPayment.setEditable(false);
  262.  
  263.        calu.add (labelAmortTable);
  264.        labelAmortTable.setBounds(180, 250, 300, 25);
  265.  
  266.        calu.add (scrollPane);
  267.        scrollPane.setBounds(125, 280, 400, 270);
  268.        areaAmortTable.setEditable(false);
  269.  
  270.        calu.add (bClear);
  271.        bClear.setBounds(110, 570, 125, 30);
  272.  
  273.        calu.add (bExit);
  274.        bExit.setBounds(250, 570, 125, 30);
  275.  
  276.        this.setContentPane(calu);
  277.        this.pack();
  278.        this.setTitle("Mortgage Calculator5");
  279.  
  280.        //Set windwo size
  281.        Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
  282.        setSize (675, 700);
  283.  
  284.         }
  285.    public double loanAmount ()
  286.    {
  287.       double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
  288.       return loanAmount;
  289.  
  290.    }
  291.  
  292.  
  293. }
  294.  
Mar 29 '10 #5
jkmyoung
2,057 Expert 2GB
bCalc30.setActionCommand ("Calculate30");
else if ("calculate30".equals(e.getActionCommand()))

The case of C on "calculate30" 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.
Mar 29 '10 #6
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.
Mar 29 '10 #7
jkmyoung
2,057 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. if ("Calculate".equals(e.getActionCommand().Substring(0,9)){
  2.    ...
  3.    double interestRate;
  4.   if ("Calculate7".equals(e.getActionCommand().Substring(0,9)){
  5.      double paymentAmount = CalculatePayment7(); 
  6.      interestRate = intRate[0];
  7.    } else if ("Calculate15".equals(e.getActionCommand().Substring(0,9)){
  8.      double paymentAmount = CalculatePayment15(); 
  9.      interestRate = intRate[1];
  10.    } else if ....
  11.    ...
  12. }
  13.  
Mar 30 '10 #8
Thank you, you must been doing this a long time. Again Thanks
Mar 31 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ralph Freshour | last post by:
Is it possible to inhibit the browser Back/Fwd buttons via PHP? Thanks...
2
by: Matt | last post by:
The ASP page has multiple buttons, and when the user clicks different buttons, it will submit the form data to different URLs. My first approach was to use BUTTON type, and triggers javascript...
2
by: longtim | last post by:
I have been trying to implement navigation buttons for a number of forms in my application. Creating the 'Next' 'Previous' buttons has been fine but setting the enabled properties doesn't work...
2
by: timm.wong | last post by:
Hi, I have a form where i have a thread working in the background that tests a connection to a server. If the connection is not successful,then I want it to display 2 buttons on the form. I...
2
by: AA Arens | last post by:
During my database construction, I left the names of the buttons, comboboxes named by default like Combo77, Command43. I can rename them with Properties Other Name, but them I start getting...
4
by: promiscuoustx | last post by:
The problem is that my code below used to run wonderfully, until the instructor decided that he wants to use characters instead of integers, and wants my code to trap the bad and have the program...
3
by: cameron | last post by:
Hi I am new here in this forum: I am writing a C++ program to calculate a Montly Mortgage Payment where the loan amount is 200,000.00 with a 5.75% interest rate with a term of 30 years. My program...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
1
by: phjones | last post by:
please help resolve some error messages code is compling with errors see below the code. I am new at this please help! /** * @(#)3 Mortgage loans.java * * 3 Mortgage loans application * ...
1
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.