Develop an application that calculates a salesperson’s commission from the number of items sold (Fig. 11.20). Assume that all items have a fixed price of 100 dollars per unit. Use a switch statement to implement the following sales commission schedule:

Fewer than 10 items sold = 1% commission

Between 10 and 39 items sold = 2% commission

Between 40 and 99 items sold = 3% commission

More than 99 items sold = 4% commission



a) Copying the template to your working directory. Copy the directory C:Examples Tutorial11ExercisesSalesCommissionCalculator to your C:SimplyJava directory.

b) Opening the template file. Open the SalesCommissionCalculator.java file in your text editor. In the template, line 127 in the calculateJButtonActionPerformed method obtains the number of items sold that was entered by the user, converts the number to an int value and assigns the value to the int local variable items. Line 129 declares the double constant DOLLARS_PER_UNIT to represent the cost for each item sold. Line 132 creates the DecimalFormat variable dollars that will be used to for- mat the gross sales and the salesperson’s earnings for display purposes.

c) Calculate the gross sales. In calculateJButtonActionPerformed, insert a stat

```
1 // SalesCommissionCalculator.java
2 // Calculates a salesperson's commission
3 // from the number of units sold.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.DecimalFormat;
8
9 public class SalesCommissionCalculator extends JFrame
10 {
11 // JLabel and JTextField to display number of items sold
12 private JLabel itemsSoldJLabel;
13 private JTextField itemsSoldJTextField;
14
15 // JButton to initiate calculations
16 private JButton calculateJButton;
17
18 // JLabel and JTextField to display gross sales
19 private JLabel salesJLabel;
20 private JTextField salesJTextField;
21
22 // JLabel and JTextField to display commission percentage
23 private JLabel commissionJLabel;
24 private JTextField commissionJTextField;
25
26 // JLabel and JTextField to display total earnings
27 private JLabel earningsJLabel;
28 private JTextField earningsJTextField;
29
30 // no-argument constructor
31 public SalesCommissionCalculator()
32 {
33 createUserInterface();
34 }
35
36 // create and position GUI components; register event handlers
37 private void createUserInterface()
38 {
39 // get content pane for attaching GUI components
40 Container contentPane = getContentPane();
41
42 // enable explicit positioning of GUI components
43 contentPane.setLayout( null );
44
45 // set up itemsSoldJLabel
46 itemsSoldJLabel = new JLabel();
47 itemsSoldJLabel.setBounds( 20, 20, 130, 20 );
48 itemsSoldJLabel.setText( "Number of items sold:" );
49 contentPane.add( itemsSoldJLabel );
50
51 // set up itemsSoldJTextField
52 itemsSoldJTextField = new JTextField();
53 itemsSoldJTextField.setBounds( 170, 20, 90, 20 );
54 itemsSoldJTextField.setHorizontalAlignment( JTextField.RIGHT );
55 contentPane.add( itemsSoldJTextField );
56
57 // set up calculateJButton
58 calculateJButton = new JButton();
59 calculateJButton.setBounds( 170, 45, 90, 25 );
60 calculateJButton.setText( "Calculate" );
61 contentPane.add( calculateJButton );
62 calculateJButton.addActionListener(
63
64 new ActionListener() // anonymous inner class
65 {
66 // event handler called when calculateJButton is pressed
67 public void actionPerformed( ActionEvent event )
68 {
69 calculateJButtonActionPerformed( event );
70 }
71
72 } // end anonymous inner class
73
74 ); // end call to addActionListener
75
76 // set up salesJLabel
77 salesJLabel = new JLabel();
78 salesJLabel.setBounds( 20, 95, 80, 20 );
79 salesJLabel.setText( "Gross sales:" );
80 contentPane.add( salesJLabel );
81
82 // set up salesJTextField
83 salesJTextField = new JTextField();
84 salesJTextField.setBounds( 170, 95, 90, 20 );
85 salesJTextField.setHorizontalAlignment( JTextField.RIGHT );
86 salesJTextField.setEditable( false );
87 contentPane.add( salesJTextField );
88
89 // set up commissionJLabel
90 commissionJLabel = new JLabel();
91 commissionJLabel.setBounds( 20, 130, 110, 20 );
92 commissionJLabel.setText( "Commission (%):" );
93 contentPane.add( commissionJLabel );
94
95 // set up commissionJTextField
96 commissionJTextField = new JTextField();
97 commissionJTextField.setBounds( 170, 130, 90, 20 );
98 commissionJTextField.setHorizontalAlignment(
99 JTextField.RIGHT );
100 commissionJTextField.setEditable( false );
101 contentPane.add( commissionJTextField );
102
103 // set up earningsJLabel
104 earningsJLabel = new JLabel();
105 earningsJLabel.setBounds( 20, 165, 90, 20 );
106 earningsJLabel.setText( "Earnings:" );
107 contentPane.add( earningsJLabel );
108
109 // set up earningsJTextField
110 earningsJTextField = new JTextField();
111 earningsJTextField.setBounds( 170, 165, 90, 20 );
112 earningsJTextField.setHorizontalAlignment( JTextField.RIGHT );
113 earningsJTextField.setEditable( false );
114 contentPane.add( earningsJTextField );
115
116 // set properties of application's window
117 setTitle( "Sales Commission Calculator" ); // set window title
118 setSize( 285, 230 ); // set window size
119 setVisible( true ); // display window
120
121 } // end method createUserInterface
122
123 // calculate the commission
124 private void calculateJButtonActionPerformed( ActionEvent event )
125 {
126 // number of items sold
127 int items = Integer.parseInt( itemsSoldJTextField.getText() );
128
129 final double DOLLARS_PER_UNIT = 100.0; // cost for each item double sales = items * 130 DOLLARS_PER_UNIT; // sales total
131 int commission; // commission percentage
132
133 Switch ( items / 10 )
134 {
135 case 0: // number of items sold 0-9
136 commission = 1; // 1% commission
137 break;
138
139 case 1: // number of items sold 10-39
140 case 2:
141 case 3:
142 commission = 2; // 2% commission
143 break;
144
145 case 4: // number of items sold 40-99
146 case 5:
147 case 6:
148 case 7:
149 case 8:
150 case 9:
151 commission = 3; // 3% commission
152 break;
153
154 default:
155 commission = 4; // 4% commission
156
157 } // end switch statement
158
159 // divide commission by 100 to convert commission
160 // percentage to a decimal value for use in the calculation
161 double commissionRate = commission / 100.0;
162
163 // calculate earnings
164 double earnings = sales * commissionRate;
165
166 // display results
167 DecimalFormat dollars = new DecimalFormat( "$0.00" );
168 salesJTextField.setText( dollars.format( sales ) );
169 commissionJTextField.setText( String.valueOf( commission ) );
170 earningsJTextField.setText( dollars.format( earnings ) );
171
172 } // end method calculateJButtonActionPerformed
173
174 // main method
175 public static void main( String[] args )
176 {
177 SalesCommissionCalculator application =
178 new SalesCommissionCalculator();
179 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
180
181 } // end method main
182
183 } // end class SalesCommissionCalculator
```

Computer Science & Information Technology

You might also like to view...

All Office programs have a ribbon that consists Tabs, Groups and ________

A) Commands B) Menus C) Actions D) Tools

Computer Science & Information Technology

A Tab Index setting of ________ will give the field focus when the form page is displayed, since it will be first on the tab order list

A) Yes B) No C) 1 D) 0

Computer Science & Information Technology