A clothing manufacturer has asked you to create an application that will calculate the total sales of that manufacturer in a week. Sales values should be input separately for each clothing item, but the amount of sales for all five week- days should be input at once. The application should calculate the total amount of sales for each item in the week and also calculate the total sales for the manufacturer for all the items in the week. Because the manufacturer is a small company, it will produce at most ten items in any week. The application is shown in Fig. 17.32.
a) Copying the template to your working directory. Copy the C:ExamplesTutorial17ExercisesSalesReport directory to your C:SimplyJava directory.
b) Opening the template file. Open the SalesReport.java file in your text editor.
c) Inputting data from the user. Add code starting in line 231 to input the data from the user. Variable nameOfItem stores the name of the item. This must be assigned to the itemNames array, indexed with itemCount (which stores the number of items added). Variables monday, tuesday, wednesday, thursday and friday store the sales data for each of the five weekdays. These variables must be assigned to the two-dimensional dailyItems array. The first index to this array should be itemCount, and the second will range from 0 to 4. Finally, increment variable itemCount to record that another item’s sales data has been added.
d) Iterating over all the items added. Inside method displaySales, after variable sal- esTotal has been declared (line 270), add code to begin
```
1 // SalesReport.java
2 // This application calculates and displays one week's sales.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 public class SalesReport extends JFrame
10 {
11 // JPanel for user inputs
12 private JPanel inputItemJPanel;
13
14 // JLabel and JTextField for item name
15 private JLabel itemJLabel;
16 private JTextField itemJTextField;
17
18 // JLabel and JTextField for monday
19 private JLabel mondayJLabel;
20 private JTextField mondayJTextField;
21
22 // JLabel and JTextField for tuesday
23 private JLabel tuesdayJLabel;
24 private JTextField tuesdayJTextField;
25
26 // JLabel and JTextField for wednesday
27 private JLabel wednesdayJLabel;
28 private JTextField wednesdayJTextField;
29
30 // JLabel and JTextField for thursday
31 private JLabel thursdayJLabel;
32 private JTextField thursdayJTextField;
33
34 // JLabel and JTextField for friday
35 private JLabel fridayJLabel;
36 private JTextField fridayJTextField;
37
38 // JButton to submit gain or loss
39 private JButton submitItemJButton;
40
41 // JLabel, JTextArea and JScrollPane to display sales
42 private JLabel displayJLabel;
43 private JTextArea displayJTextArea;
44 private JScrollPane displayJScrollPane;
45
46 // JLabel and JTextField to display sales
47 private JLabel salesJLabel;
48 private JTextField salesJTextField;
49
50 // initialize number of items to zero
51 private int itemCount = 0;
52
53 // constants
54 private final int NUMBER_OF_DAYS = 5;
55 private final int MAXIMUM_ITEMS = 10;
56
57 // one-dimensional array to store names of items
58 private String itemNames[] = new String[ MAXIMUM_ITEMS ];
59
60 // two-dimensional arrays to store items
61 private double dailyItems[][] =
62 new double[ MAXIMUM_ITEMS ][ NUMBER_OF_DAYS ];
63
64 // DecimalFormat for two digits of precision
65 private DecimalFormat dollars = new DecimalFormat( "$0.00" );
66
67 // no-argument constructor
68 public SalesReport()
69 {
70 createUserInterface();
71 }
72
73 // create and position GUI components; register event handlers
74 private void createUserInterface()
75 {
76 // get content pane for attaching GUI components
77 Container contentPane = getContentPane();
78
79 // enable explicit positioning of GUI components
80 contentPane.setLayout( null );
81
82 // set up inputItemJPanel
83 inputItemJPanel = new JPanel();
84 inputItemJPanel.setBounds( 16, 16, 208, 242 );
85 inputItemJPanel.setBorder( new TitledBorder( "Input Item" ) );
86 inputItemJPanel.setLayout( null );
87 contentPane.add( inputItemJPanel );
88
89 // set up itemJLabel
90 itemJLabel = new JLabel();
91 itemJLabel.setBounds( 12, 32, 90, 23 );
92 itemJLabel.setText( "Item:" );
93 inputItemJPanel.add( itemJLabel );
94
95 // set up itemJTextField
96 itemJTextField = new JTextField();
97 itemJTextField.setBounds( 104, 32, 88, 21 );
98 itemJTextField.setHorizontalAlignment( JTextField.RIGHT );
99 inputItemJPanel.add( itemJTextField );
100
101 // set up mondayJLabel
102 mondayJLabel = new JLabel();
103 mondayJLabel.setBounds( 12, 74, 80, 23 );
104 mondayJLabel.setText( "Monday:" );
105 inputItemJPanel.add( mondayJLabel );
106
107 // set up mondayJTextField
108 mondayJTextField = new JTextField();
109 mondayJTextField.setBounds( 136, 74, 56, 21 );
110 mondayJTextField.setHorizontalAlignment( JTextField.RIGHT );
111 inputItemJPanel.add( mondayJTextField );
112
113 // set up tuesdayJLabel
114 tuesdayJLabel = new JLabel();
115 tuesdayJLabel.setBounds( 12, 98, 80, 23 );
116 tuesdayJLabel.setText( "Tuesday:" );
117 inputItemJPanel.add( tuesdayJLabel );
118
119 // set up tuesdayJTextField
120 tuesdayJTextField = new JTextField();
121 tuesdayJTextField.setBounds( 136, 98, 56, 21 );
122 tuesdayJTextField.setHorizontalAlignment( JTextField.RIGHT );
123 inputItemJPanel.add( tuesdayJTextField );
124
125 // set up wednesdayJLabel
126 wednesdayJLabel = new JLabel();
127 wednesdayJLabel.setBounds( 12, 122, 80, 23 );
128 wednesdayJLabel.setText( "Wednesday:" );
129 inputItemJPanel.add( wednesdayJLabel );
130
131 // set up wednesdayJTextField
132 wednesdayJTextField = new JTextField();
133 wednesdayJTextField.setBounds( 136, 122, 56, 21 );
134 wednesdayJTextField.setHorizontalAlignment( JTextField.RIGHT );
135 inputItemJPanel.add( wednesdayJTextField );
136
137 // set up thursdayJLabel
138 thursdayJLabel = new JLabel();
139 thursdayJLabel.setBounds( 12, 146, 80, 23 );
140 thursdayJLabel.setText( "Thursday:" );
141 inputItemJPanel.add( thursdayJLabel );
142
143 // set up thursdayJTextField
144 thursdayJTextField = new JTextField();
145 thursdayJTextField.setBounds( 136, 146, 56, 21 );
146 thursdayJTextField.setHorizontalAlignment( JTextField.RIGHT );
147 inputItemJPanel.add( thursdayJTextField );
148
149 // set up fridayJLabel
150 fridayJLabel = new JLabel();
151 fridayJLabel.setBounds( 12, 170, 80, 23 );
152 fridayJLabel.setText( "Friday:" );
153 inputItemJPanel.add( fridayJLabel );
154
155 // set up fridayJTextField
156 fridayJTextField = new JTextField();
157 fridayJTextField.setBounds( 136, 170, 56, 21 );
158 fridayJTextField.setHorizontalAlignment( JTextField.RIGHT );
159 inputItemJPanel.add( fridayJTextField );
160
161 // set up submitItemJButton
162 submitItemJButton = new JButton();
163 submitItemJButton.setBounds( 64, 206, 128, 24 );
164 submitItemJButton.setText( "Submit Item" );
165 inputItemJPanel.add( submitItemJButton );
166 submitItemJButton.addActionListener(
167
168 new ActionListener() // anonymous inner class
169 {
170 // event handler called when submitItemJButton is clicked
171 public void actionPerformed( ActionEvent event )
172 {
173 submitItemJButtonActionPerformed( event );
174 }
175
176 } // end anonymous inner class
177
178 ); // end call to addActionListener
179
180 // set up displayJLabel
181 displayJLabel = new JLabel();
182 displayJLabel.setBounds( 240, 16, 150, 23 );
183 displayJLabel.setText( "Itemized sales:" );
184 contentPane.add( displayJLabel );
185
186 // set up displayJTextArea
187 displayJTextArea = new JTextArea();
188 displayJTextArea.setEditable( false );
189
190 // set up displayJScrollPane
191 displayJScrollPane = new JScrollPane( displayJTextArea );
192 displayJScrollPane.setBounds( 240, 48, 402, 208 );
193 contentPane.add( displayJScrollPane );
194
195 // set up salesJLabel
196 salesJLabel = new JLabel();
197 salesJLabel.setBounds( 490, 268, 96, 23 );
198 salesJLabel.setText( "Gross sales:" );
199 contentPane.add( salesJLabel );
139 thursdayJLabel.setBounds( 12, 146, 80, 23 );
140 thursdayJLabel.setText( "Thursday:" );
141 inputItemJPanel.add( thursdayJLabel );
142
143 // set up thursdayJTextField
144 thursdayJTextField = new JTextField();
145 thursdayJTextField.setBounds( 136, 146, 56, 21 );
146 thursdayJTextField.setHorizontalAlignment( JTextField.RIGHT );
147 inputItemJPanel.add( thursdayJTextField );
148
149 // set up fridayJLabel
150 fridayJLabel = new JLabel();
151 fridayJLabel.setBounds( 12, 170, 80, 23 );
152 fridayJLabel.setText( "Friday:" );
153 inputItemJPanel.add( fridayJLabel );
154
155 // set up fridayJTextField
156 fridayJTextField = new JTextField();
157 fridayJTextField.setBounds( 136, 170, 56, 21 );
158 fridayJTextField.setHorizontalAlignment( JTextField.RIGHT );
159 inputItemJPanel.add( fridayJTextField );
160
161 // set up submitItemJButton
162 submitItemJButton = new JButton();
163 submitItemJButton.setBounds( 64, 206, 128, 24 );
164 submitItemJButton.setText( "Submit Item" );
165 inputItemJPanel.add( submitItemJButton );
166 submitItemJButton.addActionListener(
167
168 new ActionListener() // anonymous inner class
169 {
170 // event handler called when submitItemJButton is clicked
171 public void actionPerformed( ActionEvent event )
172 {
173 submitItemJButtonActionPerformed( event );
174 }
175
176 } // end anonymous inner class
177
178 ); // end call to addActionListener
179
180 // set up displayJLabel
181 displayJLabel = new JLabel();
182 displayJLabel.setBounds( 240, 16, 150, 23 );
183 displayJLabel.setText( "Itemized sales:" );
184 contentPane.add( displayJLabel );
185
186 // set up displayJTextArea
187 displayJTextArea = new JTextArea();
188 displayJTextArea.setEditable( false );
189
190 // set up displayJScrollPane
191 displayJScrollPane = new JScrollPane( displayJTextArea );
192 displayJScrollPane.setBounds( 240, 48, 402, 208 );
193 contentPane.add( di
You might also like to view...
Move semantics and forwarding functions in C++0x can be written using __________.
Fill in the blank(s) with the appropriate word(s).
An ________ area is a range of cells containing values for variables used in formulas
Fill in the blank(s) with correct word
In order for buttons to be fully functional and interactive, you need to add instructions called ____.
A. actions B. scripts C. keyframes D. events
Someone once said, “RISC is to hardware what UNIX is to software”. What do you think this statement means and is it true?
What will be an ideal response?