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