Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Calculating Commission with a GUI???

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculating Commission with a GUI???

    I'm back!! Your neighborhood noob extraordinaire!! Here's my problem.. I've not got enough code just yet to post but I'm just totally lost!!!!


    When the user clicks on the 'Get Sales' button allow the input of the sales figures through an input dialog control. You should set up a loop that allows three sales figures to be entered and posted to the JTextArea and totaled. The Show Total Sales and Show Commission Earned buttons should be disabled until all sales figures have been entered. The commission is calculated on the total sales as follows: Sales total greater or = to 400000 is 10% commission, greater than or = to 300000 and less than 400000 is 8%, greater than or = to 200000 and less than 300000 is 6%, greater than or equal to 100000 and less than 200000 is 4% ... otherwise 2% .....
    I'm lost guys.. I'm going to do my best and post what I have from time to time and see where I can get from there.. I'm going to try and recycle code that I have that calculates commission so.. I am just hoping for some help to get me started... Here is an image of what my final product should resemble...


    Photo Album - Imgur


    I'm begging ... PLEASE ...


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    you should divide and conquer. 1st write out the math that will make up your algorithm. Then build your GUI. Then handle your events. Finally tie you logic into your events.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Now I can use else/if or switch's ... here is what I have so far and I've got errors all over... it's my else/if's ...

    		if (sales >= 400000)
    			rate = 0.1;
    		else if (sales >= 300000)
    			rate = 0.08;
    		else (sales < 400000)
    			rate = 0.08;
    		else if (sales >= 200000)
    			rate = 0.06;
    		else (sales < 300000)
    			rate = 0.06;
    		else if (sales >= 100000)
    			rate = 0.04;
    		else (sales < 200000)
    			rate = 0.04;
    		else (sales < 100000)
    			rate = 0.02;

    To calculate .. I think... I have this.. can it be that simple??

    commission = rate * sales;


    I'm so lost with this.. why am I making this so dang hard??!!

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Where's your main method, variable declarations, helper methods?

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
    public class Commission
    {
    	public static void main(String[] args)
    	{
    		String input;
    		double sales;
    		double rate;
    		double commission;
    		double advance;
    		double pay;
     
    		DecimalFormat dollar = new DecimalFormat("#,##0.00");
     
    		DecimalFormat percent = new DecimalFormat("#0%");
     
    		input = JOptionPane.showInputDialog("Enter the amount " + "of monthly sales.");
     
    		sales = Double.parseDouble(input);
     
    		input = JOptionPane.showInputDialog("Enter the amount " + "of advanced pay.");
     
    		advance = Double.parseDouble(input);
     
    		if (sales >= 400000)
    			rate = 0.1;
    		else if (sales >= 300000)
    			rate = 0.08;
    		else (sales < 400000)
    			rate = 0.08;
    		else if (sales >= 200000)
    			rate = 0.06;
    		else (sales < 300000)
    			rate = 0.06;
    		else if (sales >= 100000)
    			rate = 0.04;
    		else (sales < 200000)
    			rate = 0.04;
    		else (sales < 100000)
    			rate = 0.02;
     
    		commission = rate * sales;

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calculating Commission with a GUI???

    What are your questions now?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Quote Originally Posted by Norm View Post
    What are your questions now?
    I need a GUI to collect the information.. in my original post I did up an image if you click the link of what my final product should be.. I'm having trouble getting there.. but Eclipse is having a serious issue with how I have this code... red all over.. says "Syntax error on token(s), misplaced construct(s)"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calculating Commission with a GUI???

    Sorry, I don't know about "red all over".
    Can you use the javac compiler program to generate a list of the errors and post them here.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Quote Originally Posted by Norm View Post
    Sorry, I don't know about "red all over".
    Can you use the javac compiler program to generate a list of the errors and post them here.
    Red all over? That's not an industry term? I'm newer at this than I thought.. LoL.. /joking.. Here is the output of Eclipse...


    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token(s), misplaced construct(s)
    The left-hand side of an assignment must be a variable
    Syntax error, insert "AssignmentOperator Expression" to complete Assignment
    Syntax error, insert ";" to complete Statement

    at Commission.main
    (Commission.java:30)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calculating Commission with a GUI???

    Can you use the javac compiler program to generate a list of the errors and post them here?

    Your IDE says there are errors but does not give the source line number or show the text of the source line.
    What code is at line 30?

    Here is a sample of the output from the javac program:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Quote Originally Posted by Norm View Post
    Can you use the javac compiler program to generate a list of the errors and post them here?

    Your IDE says there are errors but does not give the source line number or show the text of the source line.
    What code is at line 30?

    Here is a sample of the output from the javac program:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    The code at line 30 is
    rate = 0.08;

    I'll look into the compiler to get more details.. But I think I have some code out of place... seems logic.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calculating Commission with a GUI???

    Without the error messages from the compiler, I have nothing to suggest.

  13. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Commission with a GUI???

    Quote Originally Posted by Norm View Post
    Without the error messages from the compiler, I have nothing to suggest.
    Well I just found out the code was handed out because we all bombed on the test ... so here is the code I should have come up with on my own.. Now, in my opinion, this is way advanced for where we are in the class.. we just hit chapter 8 in the book and the book doesn't even cover, aside from a mention, JTextArea until way later in the book.. I don't know, I just figured this early in the game we wouldn't be programming 200+ lines of code in a beginners course... Just my .02 on it.. but here's the full code..

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Commission extends JFrame
    {
      // JLabel and JTextArea for list of sales
      private JLabel salesListJLabel;
      private JTextArea salesListJTextArea;
     
      // JButton initiates retrieving sales
      private JButton getSalesJButton;
     
      // JButton initiates calculating sales total
      private JButton salesTotalJButton;
     
      // JLabel and JTextField used to display sales total
      private JLabel salesTotalJLabel;
      private JTextField salesTotalJTextField;
     
      // JButton initiates calculating commission earned
      private JButton commissionJButton;
     
      // JLabel and JTextField used to display commission earned
      private JLabel commissionJLabel;
      private JTextField commissionJTextField;
     
      private int total = 0; // holds value of the sales total
     
      // no-argument constructor
      public Commission()
      {
         createUserInterface();
      }
     
      // create and position GUI components; register event handlers
      private void createUserInterface()
      {
         // get content pane for attaching GUI components
         Container contentPane = getContentPane();
     
         // enable explicit positioning of GUI components
         contentPane.setLayout( null );
     
         // set up salesListJLabel
         salesListJLabel = new JLabel();
         salesListJLabel.setBounds( 16, 8, 70, 23 );
         salesListJLabel.setText( "Sales list:" );
         contentPane.add( salesListJLabel );
     
         // set up salesListJTextArea
         salesListJTextArea = new JTextArea();
         salesListJTextArea.setBounds( 16, 32, 88, 180 );
         contentPane.add( salesListJTextArea );
     
         // set up getSalesJButton
         getSalesJButton = new JButton();
         getSalesJButton.setBounds( 128, 50, 100, 26 );
         getSalesJButton.setText( "Get Sales" );
         contentPane.add( getSalesJButton );
         getSalesJButton.addActionListener(
     
            new ActionListener() // anonymous inner class
            {
               // event handler called when getGradesJButton is clicked
               public void actionPerformed( ActionEvent event )
               {
                  getSalesJButtonActionPerformed( event );
               }
     
            } // end anonymous inner class
     
         ); // end call to addActionListener
     
         // set up salesTotalJButton
         salesTotalJButton = new JButton();
         salesTotalJButton.setBounds( 128, 90, 150, 26 );
         salesTotalJButton.setText( "Show Total Sales" );
         salesTotalJButton.setEnabled( false );
         contentPane.add( salesTotalJButton );
         salesTotalJButton.addActionListener(
     
            new ActionListener() // anonymous inner class
            {
               // event handler called when averageJButton is clicked
               public void actionPerformed( ActionEvent event )
               {
                   double dblTotal = (double) total;
                   salesTotalJTextField.setText( String.format("$%.2f", dblTotal) );
               }
     
            } // end anonymous inner class
     
         ); // end call to addActionListener
     
         // set up commissionJButton
         commissionJButton = new JButton();
         commissionJButton.setBounds( 300, 90, 200, 26 );
         commissionJButton.setText( "Show Commission Earned" );
         commissionJButton.setEnabled( false );
         contentPane.add( commissionJButton );
         commissionJButton.addActionListener(
     
            new ActionListener() // anonymous inner class
            {
               // event handler called when averageJButton is clicked
               public void actionPerformed( ActionEvent event )
               {
                  double commission = calculateCommission( total );
                  commissionJTextField.setText( String.format("$%.2f",
    commission ) );
               }
     
            } // end anonymous inner class
     
         ); // end call to addActionListener
     
         // set up salesTotalJLabel
         salesTotalJLabel = new JLabel();
         salesTotalJLabel.setBounds( 128, 132, 90, 23 );
         salesTotalJLabel.setText( "Sales Total:" );
         contentPane.add( salesTotalJLabel );
     
         // set up salesTotalJTextField
         salesTotalJTextField = new JTextField();
         salesTotalJTextField.setBounds( 128, 156, 100, 21 );
         salesTotalJTextField.setEditable( false );
         salesTotalJTextField.setHorizontalAlignment(
            JTextField.CENTER );
         contentPane.add( salesTotalJTextField );
     
         // set up commissionJLabel
         commissionJLabel = new JLabel();
         commissionJLabel.setBounds( 340, 132, 150, 23 );
         commissionJLabel.setText( "Commission Earned:" );
         contentPane.add( commissionJLabel );
     
         // set up commissionJTextField
         commissionJTextField = new JTextField();
         commissionJTextField.setBounds( 340, 156, 100, 21 );
         commissionJTextField.setEditable( false );
         commissionJTextField.setHorizontalAlignment(
            JTextField.CENTER );
         contentPane.add( commissionJTextField );
     
         // set properties of application's window
         setTitle( "Commission Calculator" ); // set title bar text
         setSize( 600, 300 );         // set window size
         setVisible( true );          // display window
     
      } // end method createUserInterface
     
      // method retrieves, totals and displays sales from user
      private void getSalesJButtonActionPerformed( ActionEvent event )
      {
         total = 0;        // stores total of sales entered
         int counter = 1;  // counter controls do...while statement
         String input;     // stores data entered into input dialog
         int sales;        // stores int value converted from input
     
         // clear previous grades and calculation result
         salesListJTextArea.setText( "" );
         salesTotalJTextField.setText( "" );
         commissionJTextField.setText( "" );
     
         do
         {
            // get user input
            input = JOptionPane.showInputDialog( null, "Enter Sales" );
            sales = Integer.parseInt( input );
     
            // add text to output
            salesListJTextArea.append( sales + "\n" );
            total += sales;                     // add input to total
            counter++;                          // increment counter
         }
         while ( counter <= 3 );                // end do...while
     
         salesTotalJButton.setEnabled( true );     // enable salesTotalJButton
         salesTotalJButton.requestFocusInWindow(); // transfer focus
             commissionJButton.setEnabled( true ); // enable commissionJButton
     
      } // end method getSalesJButtonActionPerformed
     
      // method calculates commission from total sales
      private double calculateCommission(int total) {
     
              double commission = 0.0;
     
              switch( total / 100000){
                   case 3:
                   commission = total * .08;
                   break;
                   case 2:
                   commission = total * .06;
                   break;
                   case 1:
                   commission = total * .04;
                   break;
                   case 0:
                   commission = total * .02;
                   break;
                   default:
                   commission = total * .10;
                   break;
              } // end switch statement
              return commission;
      } // end calculateCommission method
     
      // main method
      public static void main( String[] args )
      {
         Commission application = new Commission();
         application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
      } // end method main
     
    } // end class Commission

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calculating Commission with a GUI???

    Do you see what the difference is between the two pieces of code you've posted?
    The first one was missing several lines at the end.

Similar Threads

  1. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 08:26 PM
  2. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  3. Calculating average of an array
    By Vika in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 29th, 2011, 08:06 AM
  4. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM
  5. KB/s download speed calculating
    By Koâk in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 03:05 PM