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

Thread: Calculating Commission with a GUI???

1. 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

2. 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. 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. Re: Calculating Commission with a GUI???

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

5. 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 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.");

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

7. Re: Calculating Commission with a GUI???

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)"

8. 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. Re: Calculating Commission with a GUI???

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...

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. 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. Re: Calculating Commission with a GUI???

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:
```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. Re: Calculating Commission with a GUI???

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

13. Re: Calculating Commission with a GUI???

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..

```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:" );

// set up salesListJTextArea
salesListJTextArea = new JTextArea();
salesListJTextArea.setBounds( 16, 32, 88, 180 );

// set up getSalesJButton
getSalesJButton = new JButton();
getSalesJButton.setBounds( 128, 50, 100, 26 );
getSalesJButton.setText( "Get Sales" );

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 );

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 );

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:" );

// set up salesTotalJTextField
salesTotalJTextField = new JTextField();
salesTotalJTextField.setBounds( 128, 156, 100, 21 );
salesTotalJTextField.setEditable( false );
salesTotalJTextField.setHorizontalAlignment(
JTextField.CENTER );

// set up commissionJLabel
commissionJLabel = new JLabel();
commissionJLabel.setBounds( 340, 132, 150, 23 );
commissionJLabel.setText( "Commission Earned:" );

// set up commissionJTextField
commissionJTextField = new JTextField();
commissionJTextField.setBounds( 340, 156, 100, 21 );
commissionJTextField.setEditable( false );
commissionJTextField.setHorizontalAlignment(
JTextField.CENTER );

// 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 );

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. 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.