# Calculating Commission with a GUI???

• February 23rd, 2012, 07:18 PM
jbarcus81
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!!!!

Quote:

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

• February 23rd, 2012, 07:39 PM
mikejr76
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.
• February 23rd, 2012, 10:03 PM
jbarcus81
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 ...

Code :

``` 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??

Code :

`commission = rate * sales;`

I'm so lost with this.. why am I making this so dang hard??!!
• February 23rd, 2012, 11:46 PM
mikejr76
Re: Calculating Commission with a GUI???
Where's your main method, variable declarations, helper methods?
• February 24th, 2012, 08:13 AM
jbarcus81
Re: Calculating Commission with a GUI???
Code :

```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;```
• February 24th, 2012, 01:01 PM
Norm
Re: Calculating Commission with a GUI???
• February 24th, 2012, 01:24 PM
jbarcus81
Re: Calculating Commission with a GUI???
Quote:

Originally Posted by Norm

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)"
• February 24th, 2012, 01:30 PM
Norm
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.
• February 24th, 2012, 05:25 PM
jbarcus81
Re: Calculating Commission with a GUI???
Quote:

Originally Posted by Norm
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...

Quote:

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)
• February 24th, 2012, 06:11 PM
Norm
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:
Code :

```TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^```
• February 24th, 2012, 06:22 PM
jbarcus81
Re: Calculating Commission with a GUI???
Quote:

Originally Posted by Norm
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:
Code :

```TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^```

The code at line 30 is
Code :

`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.
• February 24th, 2012, 06:36 PM
Norm
Re: Calculating Commission with a GUI???
Without the error messages from the compiler, I have nothing to suggest.
• February 24th, 2012, 09:51 PM
jbarcus81
Re: Calculating Commission with a GUI???
Quote:

Originally Posted by Norm
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..

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```
• February 25th, 2012, 06:35 AM
Norm
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.