Modify the application you developed in Exercise 11.11 to allow the user to input the price of the items and the sales commission percentage. The user enters the number of items sold and the price of items and the gross sales is calculated from this. The commission percentage will be in the range 1 to 10 inclusive. The user has been provided with a JSpinner to choose the commission percentage (Fig. 13.25). When the value in the JSpinner changes, the calculated earnings should automatically be updated.
a) Copying the template to your working directory. Copy the C:Examples Tutorial13ExercisesSalesCommissionCalculator2 directory to your C:Sim- plyJava directory.
b) Opening the template file. Open the SalesCommissionCalculator.java file in your text editor.
c) Completing the stateChanged event handler for commissionJSpinner. On line 107, add code to call method commissionJSpinnerStateChanged. Pass to the method the ChangeEvent object event as the argument.
d) Coding the commissionJSpinnerStateChanged method. Starting after method calculateEarnings, on line 174, declare method commissionJSpinnerState- Changed. This method should call method calculateEarnings. Method calcula- teEarnings has already been declared for you.
e) Completing the actionPerformed event handler for calculateJButton. On line
139, add code to call method calculateJButtonActionPerformed. Pass to the
method the ActionEvent object event as the argument.
f) Coding the ca
```
1 // SalesCommissionCalculator.java
2 // Application calculates a salesperson's commission from the number
3 // of units sold.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.DecimalFormat;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 public class SalesCommissionCalculator extends JFrame
11 {
12 // JLabel and JTextField for items sold
13 private JLabel itemsSoldJLabel;
14 private JTextField itemsSoldJTextField;
15
16 // JLabel and JTextField for price of items
17 private JLabel priceJLabel;
18 private JTextField priceJTextField;
19
20 // JLabel and JTextField for gross sales
21 private JLabel grossSalesJLabel;
22 private JTextField grossSalesJTextField;
23
24 // JLabel and JSpinner for commission
25 private JLabel commissionJLabel;
26 private JSpinner commissionJSpinner;
27
28 // JLabel and JTextField for earnings
29 private JLabel earningsJLabel;
30 private JTextField earningsJTextField;
31
32 // JButton to calculate the earnings
33 private JButton calculateJButton;
34
35 // no-argument constructor
36 public SalesCommissionCalculator()
37 {
38 createUserInterface();
39 }
40
41 // create and position GUI components; register event handlers
42 private void createUserInterface()
43 {
44 // get content pane for attaching GUI components
45 Container contentPane = getContentPane();
46
47 // enable explicit positioning of GUI components
48 contentPane.setLayout( null );
49
50 // set up itemsSoldJLabel
51 itemsSoldJLabel = new JLabel();
52 itemsSoldJLabel.setBounds( 20, 20, 130, 20 );
53 itemsSoldJLabel.setText( "Number of items sold:" );
54 contentPane.add( itemsSoldJLabel );
55
56 // set up itemsSoldJTextField
57 itemsSoldJTextField = new JTextField();
58 itemsSoldJTextField.setBounds( 170, 20, 90, 20 );
59 itemsSoldJTextField.setHorizontalAlignment( JTextField.RIGHT );
60 contentPane.add( itemsSoldJTextField );
61
62 // set up priceJLabel
63 priceJLabel = new JLabel();
64 priceJLabel.setBounds( 20, 55, 130, 20 );
65 priceJLabel.setText( "Price of items:" );
66 contentPane.add( priceJLabel );
67
68 // set up priceJTextField
69 priceJTextField = new JTextField();
70 priceJTextField.setBounds( 170, 55, 90, 20 );
71 priceJTextField.setHorizontalAlignment( JTextField.RIGHT );
72 contentPane.add( priceJTextField );
73
74 // set up grossSalesJLabel
75 grossSalesJLabel = new JLabel();
76 grossSalesJLabel.setBounds( 20, 90, 80, 20 );
77 grossSalesJLabel.setText( "Gross sales:" );
78 contentPane.add( grossSalesJLabel );
79
80 // set up grossSalesJTextField
81 grossSalesJTextField = new JTextField();
82 grossSalesJTextField.setBounds( 170, 90, 90, 20 );
83 grossSalesJTextField.setEditable( false );
84 grossSalesJTextField.setHorizontalAlignment(
85 JTextField.RIGHT );
86 contentPane.add( grossSalesJTextField );
87
88 // set up commissionJLabel
89 commissionJLabel = new JLabel();
90 commissionJLabel.setBounds( 20, 130, 110, 20 );
91 commissionJLabel.setText( "Commission (%):" );
92 contentPane.add( commissionJLabel );
93
94 // set up commissionJSpinner
95 commissionJSpinner = new JSpinner(
96 new SpinnerNumberModel( 1, 1, 10, 1 ) );
97 commissionJSpinner.setBounds( 170, 130, 90, 20 );
98 contentPane.add( commissionJSpinner );
99 commissionJSpinner.addChangeListener(
100
101 new ChangeListener() // anonymous inner class
102 {
103 // event handler called when value in
104 // commissionJSpinner changes
105 public void stateChanged( ChangeEvent event )
106 {
107
108 }
109 commissionJSpinnerStateChanged( event );
110 } // end anonymous inner class
111
112 ); // end call to addChangeListener
113
114 // set up earningsJLabel
115 earningsJLabel = new JLabel();
116 earningsJLabel.setBounds( 20, 170, 90, 20 );
117 earningsJLabel.setText( "Earnings:" );
118 contentPane.add( earningsJLabel );
119
120 // set up earningsJTextField
121 earningsJTextField = new JTextField();
122 earningsJTextField.setBounds( 170, 170, 90, 20 );
123 earningsJTextField.setEditable( false );
124 earningsJTextField.setHorizontalAlignment( JTextField.RIGHT );
125 contentPane.add( earningsJTextField );
126
127 // set up calculateJButton
128 calculateJButton = new JButton();
129 calculateJButton.setBounds( 170, 205, 90, 25 );
130 calculateJButton.setText( "Calculate" );
131 contentPane.add( calculateJButton );
132 calculateJButton.addActionListener(
133
134 new ActionListener() // anonymous inner class
135 {
136 // event handler called when calculateJButton is pressed
137 public void actionPerformed( ActionEvent event )
138 {
139
140 }
141 calculateJButtonActionPerformed( event );
142 } // end anonymous inner class
143
144 ); // end call to addActionListener
145
146 // set properties of application's window
147 setTitle( "Sales Commission Calculator" ); // set window title
148 setSize( 285, 285 ); // set window size
149 setVisible( true ); // show window
150
151 } // end method createUserInterface
152
153 // calculate and display sales and earnings
154 private void calculateEarnings()
155 {
156 // get user input
157 int items = Integer.parseInt( itemsSoldJTextField.getText() );
158 double price = Double.parseDouble( priceJTextField.getText() );
159 Integer integerObject =
160 ( Integer ) commissionJSpinner.getValue();
161 int commissionRate = integerObject.intValue();
162
163 // calculate total sales and earnings
164 double sales = items * price;
165 double earnings = sales * commissionRate / 100;
166
167 // display the results
168 DecimalFormat dollars = new DecimalFormat( "$0.00" );
169 grossSalesJTextField.setText( dollars.format( sales ) );
170 earningsJTextField.setText( dollars.format( earnings ) );
171
172 } // end method calculateEarnings
173
174 // call method calculateEarnings
175 private void commissionJSpinnerStateChanged( ChangeEvent event )
176 {
177 calculateEarnings();
178
179 } // end method commissionJSpinnerStateChanged
180
181 // call method calculateEarnings
182 private void calculateJButtonActionPerformed( ActionEvent event )
183 {
184 calculateEarnings();
185
186 } // end method calculateJButtonActionPerformed
187
188 // main method
189 public static void main( String[] args )
190 {
191 SalesCommissionCalculator application =
192 new SalesCommissionCalculator();
193 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
194
195 } // end method main
196
197 } // end class SalesCommissionCalculator
```
You might also like to view...
The each in array together loop processes each of the items in an array in parallel.
Answer the following statement true (T) or false (F)
You can use layers to make shapes appear to be stacked on top of one another. To move a shape from one layer to another, use options from the _____ group.
A. Arrange B. Reorder C. Layers D. Styles
On what kind of network, such as that illustrated in the accompanying figure, do one or more computers act as a server and the other computers on the network request services from the server?
A. dedicated B. client/server C. integrated D. indexed
The first phase of the system development life cycle is system requirements.
Answer the following statement true (T) or false (F)