Write an application that calculates the cost of all the supplies added to the user’s shopping list (Fig. 23.17). The application should contain two JLists. The first JList contains all the supplies offered and their respective prices. Users should be able to select the desired supplies from the first JList and add them to the second JList. The user may select an item in the first JList multiple times. Provide a Calcu- late JButton that displays the total price for the user’s shopping list (the contents of the second JList).



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

b) Opening the template file. Open the SupplyCalculator.java file in your text edi- tor.

c) Adding code to the calculcateJButtonActionPerformed method. In the calcu- lateJButtonActionPerformed method (lines 242–245), declare two variables—the first should be a double (called total), which will keep track of the total price. The second will be a String (called price) used as a temporary variable for the price. Then, add a for statement to loop through all the items in the ArrayList userAr-

rayList. You will use this for statement to iterate through each item in the Items in Your List: JList. These items have already been added to the userArrayList for you.

d) Extracting the prices and calculating the total. Inside the for statement you added in Step c, convert the current item from the userArrayList into a String. Store t

```
1 // SupplyCalculator.java
2 // Enables users to select from a list of supplies
3 // and determine the price of the selected supplies.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.*;
7 import java.util.*;
8 import javax.swing.*;
9
10 public class SupplyCalculator extends JFrame
11 {
12 // JLabel, JList and JScrollPane for displaying items in stock
13 private JLabel stockListJLabel;
14 private JList stockJList;
15 private JScrollPane stockJScrollPane;
16
17 // JButtons to add and remove items from the user item list
18 private JButton addJButton;
19 private JButton removeJButton;
20
21 // JButton to calculate total amount user is spending
22 private JButton calculateJButton;
23
24 // JLabel, JList and JScrollPane for displaying user item list
25 private JLabel itemsJLabel;
26 private JList itemsJList;
27 private JScrollPane itemsJScrollPane;
28
29 // JLabel and JTextField for displaying total cost
30 private JLabel totalJLabel;
31 private JTextField totalJTextField;
32
33 // ArrayList for storing items user has selected
34 private ArrayList userArrayList = new ArrayList();
35
36 // String for storing items in stock
37 private String stockItems[] = { "Staples - $2.10",
38 "Stapler - $5.99", "Folder - $3.65", "Notebook - $4.99",
39 "Pencil - $1.29", "Eraser - $1.49", "Pen - $1.89",
40 "Post-it - $0.99", "Marker - $1.99", "MousePad - $2.49" };
41
42 // no-argument constructor
43 public SupplyCalculator()
44 {
45 createUserInterface();
46 }
47
48 // create and position GUI components; register event handlers
49 private void createUserInterface()
50 {
51 // get content pane for attaching GUI components
52 Container contentPane = getContentPane();
53
54 // enable explicit positioning of GUI components
55 contentPane.setLayout( null );
56
57 // set up stockListJLabel
58 stockListJLabel = new JLabel();
59 stockListJLabel.setBounds( 16, 16, 100, 21 );
60 stockListJLabel.setText( "Items in Stock:" );
61 contentPane.add( stockListJLabel );
62
63 // set up stockJList and stockJScrollPane
64 stockJList = new JList( stockItems );
65 stockJScrollPane = new JScrollPane( stockJList );
66 stockJScrollPane.setBounds( 16, 40, 112, 134 );
67 contentPane.add( stockJScrollPane );
68
69 // set up addJButton
70 addJButton = new JButton();
71 addJButton.setBounds( 144, 56, 104, 23 );
72 addJButton.setText( "Add >>" );
73 contentPane.add( addJButton );
74 addJButton.addActionListener(
75
76 new ActionListener() // anonymous inner class
77 {
78 // event handler called when addJButton is pressed
79 public void actionPerformed( ActionEvent event )
80 {
81 addJButtonActionPerformed( event );
82 }
83
84 } // end anonymous inner class
85
86 ); // end call to addActionListener
87
88 // set up removeJButton
89 removeJButton = new JButton();
90 removeJButton.setBounds( 144, 96, 104, 23 );
91 removeJButton.setText( "<< Remove" );
92 removeJButton.setEnabled( false );
93 contentPane.add( removeJButton );
94 removeJButton.addActionListener(
95
96 new ActionListener() // anonymous inner class
97 {
98 // event handler called when removeJButton is pressed
99 public void actionPerformed( ActionEvent event )
100 {
101 removeJButtonActionPerformed( event );
102 }
103
104 } // end anonymous inner class
105
106 ); // end call to addActionListener
107
108 // set up calculateJButton
109 calculateJButton = new JButton();
110 calculateJButton.setBounds( 144, 144, 104, 23 );
111 calculateJButton.setText( "Calculate" );
112 calculateJButton.setEnabled( false );
113 contentPane.add( calculateJButton );
114 calculateJButton.addActionListener(
115
116 new ActionListener() // anonymous inner class
117 {
118 // event handler called when calculateJButton is pressed
119 public void actionPerformed( ActionEvent event )
120 {
121 calculateJButtonActionPerformed( event );
122 }
123
124 } // end anonymous inner class
125
126 ); // end call to addActionListener
127
128 // set up itemsJLabel
129 itemsJLabel = new JLabel();
130 itemsJLabel.setBounds( 262, 16, 110, 21 );
131 itemsJLabel.setText( "Items in Your List:" );
132 contentPane.add( itemsJLabel );
133
134 // set up itemsJList and itemsJScrollPane
135 itemsJList = new JList();
136 itemsJScrollPane = new JScrollPane( itemsJList );
137 itemsJScrollPane.setBounds( 262, 40, 112, 134 );
138 contentPane.add( itemsJScrollPane );
139
140 // set up totalJLabel
141 totalJLabel = new JLabel();
142 totalJLabel.setBounds( 214, 184, 40, 23 );
143 totalJLabel.setText( "Total:" );
144 contentPane.add( totalJLabel );
145
146 // set up totalJTextField
147 totalJTextField = new JTextField();
148 totalJTextField.setBounds( 262, 184, 112, 23 );
149 totalJTextField.setHorizontalAlignment( JTextField.CENTER );
150 totalJTextField.setEditable( false );
151 contentPane.add( totalJTextField );
152
153 // set properties of application's window
154 setTitle( "Supply Cost Calculator" ); // set title bar string
155 setSize( 398, 248 ); // set window size
156 setVisible( true ); // display window
157
158 } // end method createUserInterface
159
160 // add shopping item to itemsJList
161 private void addJButtonActionPerformed( ActionEvent event )
162 {
163 // Object array to hold user selection
164 Object selectedValue[] = stockJList.getSelectedValues();
165
166 // if there is at least one item selected
167 if ( selectedValue.length != 0 )
168 {
169 for ( int counter = 0; counter < selectedValue.length;
170 counter++ )
171 {
172 // add item to userArrayList
173 userArrayList.add(
174 ( String ) ( selectedValue[ counter ] ) );
175 }
176
177 // add item to itemsJList
178 itemsJList.setListData( userArrayList.toArray() );
179
180 // enable Calculate and Remove JButtons
181 removeJButton.setEnabled( true );
182 calculateJButton.setEnabled( true );
183
184 totalJTextField.setText( "" ); // clear totalJTextField
185 }
186 else
187 {
188 // display message if there is no item selected
128 // set up itemsJLabel
129 itemsJLabel = new JLabel();
130 itemsJLabel.setBounds( 262, 16, 110, 21 );
131 itemsJLabel.setText( "Items in Your List:" );
132 contentPane.add( itemsJLabel );
133
134 // set up itemsJList and itemsJScrollPane
135 itemsJList = new JList();
136 itemsJScrollPane = new JScrollPane( itemsJList );
137 itemsJScrollPane.setBounds( 262, 40, 112, 134 );
138 contentPane.add( itemsJScrollPane );
139
140 // set up totalJLabel
141 totalJLabel = new JLabel();
142 totalJLabel.setBounds( 214, 184, 40, 23 );
143 totalJLabel.setText( "Total:" );
144 contentPane.add( totalJLabel );
145
146 // set up totalJTextField
147 totalJTextField = new JTextField();
148 totalJTextField.setBounds( 262, 184, 112, 23 );
149 totalJTextField.setHorizontalAlignment( JTextField.CENTER );
150 totalJTextField.setEditable( false );
151 contentPane.add( totalJTextField );
152
153 // set properties of application's window
154 setTitle( "Supply Cost Calculator" ); // set title bar string
155 setSize( 398, 248 ); // set window size
156 setVisible( true ); // display window
157
158 } // end method createUserInterface
159
160 // add shopping item to itemsJList
161 private void addJButtonActionPerformed( ActionEvent event )
162 {
163 // Object array to hold user selection
164 Object selectedValue[] = stockJList.getSelectedValues();
165
166 // if there is at least one item selected
167 if ( selectedValue.length != 0 )
168 {
169 for ( int counter = 0; counter < selectedValue.length;
170 counter++ )
171 {
172 // add item to userArrayList
173 userArrayList.add(
174 ( String ) ( selectedValue[ counter ] ) );
175 }
176
177 // add item to itemsJList
178 itemsJList.setListData( userArrayList.toArray() );
179
180 // enable Calculate and Remove JButtons
181 removeJButton.setEnabled( true );
182 calculateJButton.setEnabled( true );
183
184 totalJTextField.setText( "" ); // clear totalJTextField
185 }
186 else
187 {
188 // display message if there is no item sele

Computer Science & Information Technology

You might also like to view...

C:\Users\username\OneDrive is an example of a(n) ________

A) AutoRecovery in progress B) ScreenTip C) file path D) file extension

Computer Science & Information Technology

_____ a fileplaces it in a new location but does not remove it from its current location.

A. ?Shifting B. ?Copying C. ?Transforming D. ?Altering

Computer Science & Information Technology