Create an application that mimics the functionality of a vending machine. Your application’s GUI has been completed for you (Fig. 14.18). Store the prices of each item for sale in the vending machine in an instance variable. Also, add the code that will display the price of an item when the item’s number has been entered and Enter JButton has been clicked.
a) Copying the template to your working directory. Copy the C:Exam- plesTutorial14ExercisesVendingMachine2 directory to your C:SimplyJava directory.
b) Opening the template file. Open the VendingMachine.java file in your text editor. c) Adding instance variables. After the last GUI component is declared (line 53),
declare private String instance variables to store the prices of each of the snacks.
Each String should be in the form "$0.00" where the price of the snack is substi- tuted for the zeros. The variables’ names and prices should be snackPrice1 ("$1.25"), snackPrice2 ("$0.50"), snackPrice3 ("$1.25"), snackPrice4 ("$1.00"), snackPrice5 ("$1.25"), snackPrice6 ("$1.25"), snackPrice7 ("$1.00") and snackPrice8 ("$0.50").
d) Coding the enterJButtonActionPerformed method. Locate the enterJButtonAc- tionPerformed method. In this method, store the user input entered in the inputJ- TextField. Then, use a switch statement to di
```
1 // VendingMachine.java
2 // Creates vending machine that will display price of entered item.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class VendingMachine extends JFrame
9 {
10 // JPanel to hold all pictures
11 private JPanel windowJPanel;
12
13 // JLabels for first snack shown
14 private JLabel oneJLabel;
15 private JLabel oneIconJLabel;
16
17 // JLabels for second snack shown
18 private JLabel twoJLabel;
19 private JLabel twoIconJLabel;
20
21 // JLabels for third snack shown
22 private JLabel threeJLabel;
23 private JLabel threeIconJLabel;
24
25 // JLabels for fourth snack shown
26 private JLabel fourJLabel;
27 private JLabel fourIconJLabel;
28
29 // JLabels for fifth snack shown
30 private JLabel fiveJLabel;
31 private JLabel fiveIconJLabel;
32
33 // JLabels for sixth snack shown
34 private JLabel sixJLabel;
35 private JLabel sixIconJLabel;
36
37 // JLabels for seventh snack shown
38 private JLabel sevenJLabel;
39 private JLabel sevenIconJLabel;
40
41 // JLabels for eighth snack shown
42 private JLabel eightJLabel;
43 private JLabel eightIconJLabel;
44
45 // JTextField for displaying snack price
46 private JTextField displayJTextField;
47
48 // JLabel and JTextField for user input
49 private JLabel inputJLabel;
50 private JTextField inputJTextField;
51
52 // JButton to enter user input
53 private JButton enterJButton;
54
55 // Strings to hold the prices of the snacks
56 private String snackPrice1 = "$1.25";
57 private String snackPrice2 = "$0.50";
58 private String snackPrice3 = "$1.25";
59 private String snackPrice4 = "$1.00";
60 private String snackPrice5 = "$1.25";
61 private String snackPrice6 = "$1.25";
62 private String snackPrice7 = "$1.00";
63 private String snackPrice8 = "$0.50";
64
65 // no-argument constructor
66 public VendingMachine()
67 {
68 createUserInterface();
69 }
70
71 // create and position GUI components; register event handlers
72 private void createUserInterface()
73 {
74 // get content pane for attaching GUI components
75 Container contentPane = getContentPane();
76
77 // enable explicit positioning of GUI components
78 contentPane.setLayout( null );
79
80 // set up windowJPanel
81 windowJPanel = new JPanel();
82 windowJPanel.setBounds( 10, 10, 260, 170 );
83 windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
84 windowJPanel.setLayout( null );
85 contentPane.add( windowJPanel );
86
87 // set up oneIconJLabel
88 oneIconJLabel = new JLabel();
89 oneIconJLabel.setBounds( 10, 10, 50, 50 );
90 oneIconJLabel.setIcon( new ImageIcon( "images/cookie.png" ) );
91 windowJPanel.add( oneIconJLabel );
92
93 // set up oneJLabel
94 oneJLabel = new JLabel();
95 oneJLabel.setBounds( 10, 60, 50, 20 );
96 oneJLabel.setText( "1" );
97 oneJLabel.setHorizontalAlignment( JLabel.CENTER );
98 windowJPanel.add( oneJLabel );
99
100 // set up twoIconJLabel
101 twoIconJLabel = new JLabel();
102 twoIconJLabel.setBounds( 70, 10, 50, 50 );
103 twoIconJLabel.setIcon( new ImageIcon( "images/gum.png" ) );
104 windowJPanel.add( twoIconJLabel );
105
106 // set up twoJLabel
107 twoJLabel = new JLabel();
108 twoJLabel.setBounds( 70, 60, 50, 20 );
109 twoJLabel.setText( "2" );
110 twoJLabel.setHorizontalAlignment( JLabel.CENTER );
111 windowJPanel.add( twoJLabel );
112
113 // set up threeIconJLabel
114 threeIconJLabel = new JLabel();
115 threeIconJLabel.setBounds( 130, 10, 50, 50 );
116 threeIconJLabel.setIcon(
117 new ImageIcon( "images/pretzel.png" ) );
118 windowJPanel.add( threeIconJLabel );
119
120 // set up threeJLabel
121 threeJLabel = new JLabel();
122 threeJLabel.setBounds( 130, 60, 50, 20 );
123 threeJLabel.setText( "3" );
124 threeJLabel.setHorizontalAlignment( JLabel.CENTER );
125 windowJPanel.add( threeJLabel );
126
127 // set up fourIconJLabel
128 fourIconJLabel = new JLabel();
129 fourIconJLabel.setBounds( 190, 10, 50, 50 );
130 fourIconJLabel.setIcon( new ImageIcon( "images/soda.png" ) );
131 windowJPanel.add( fourIconJLabel );
132
133 // set up fourJLabel
134 fourJLabel = new JLabel();
135 fourJLabel.setBounds( 190, 60, 50, 20 );
136 fourJLabel.setText( "4" );
137 fourJLabel.setHorizontalAlignment( JLabel.CENTER );
138 windowJPanel.add( fourJLabel );
139
140 // set up fiveIconJLabel
141 fiveIconJLabel = new JLabel();
142 fiveIconJLabel.setBounds( 10, 90, 50, 50 );
143 fiveIconJLabel.setIcon( new ImageIcon(
144 "images/pretzel.png" ) );
145 windowJPanel.add( fiveIconJLabel );
146
147 // set up fiveJLabel
148 fiveJLabel = new JLabel();
149 fiveJLabel.setBounds( 10, 140, 50, 20 );
150 fiveJLabel.setText( "5" );
151 fiveJLabel.setHorizontalAlignment( JLabel.CENTER );
152 windowJPanel.add( fiveJLabel );
153
154 // set up sixIconJLabel
155 sixIconJLabel = new JLabel();
156 sixIconJLabel.setBounds( 70, 90, 50, 50 );
157 sixIconJLabel.setIcon( new ImageIcon( "images/cookie.png" ) );
158 windowJPanel.add( sixIconJLabel );
159
160 // set up sixJLabel
161 sixJLabel = new JLabel();
162 sixJLabel.setBounds( 70, 140, 50, 20 );
163 sixJLabel.setText( "6" );
164 sixJLabel.setHorizontalAlignment( JLabel.CENTER );
165 windowJPanel.add( sixJLabel );
166
167 // set up sevenIconJLabel
168 sevenIconJLabel = new JLabel();
169 sevenIconJLabel.setBounds( 130, 90, 50, 50 );
170 sevenIconJLabel.setIcon( new ImageIcon( "images/soda.png" ) );
171 windowJPanel.add( sevenIconJLabel );
172
173 // set up sevenJLabel
174 sevenJLabel = new JLabel();
175 sevenJLabel.setBounds( 130, 140, 50, 20 );
176 sevenJLabel.setText( "7" );
177 sevenJLabel.setHorizontalAlignment( JLabel.CENTER );
178 windowJPanel.add( sevenJLabel );
179
180 // set up eightIconJLabel
181 eightIconJLabel = new JLabel();
182 eightIconJLabel.setBounds( 190, 90, 50, 50 );
183 eightIconJLabel.setIcon( new ImageIcon( "images/gum.png" ) );
184 windowJPanel.add( eightIconJLabel );
185
186 // set up eightJLabel
187 eightJLabel = new JLabel();
188 eightJLabel.setBounds( 190, 140, 50, 20 );
189 eightJLabel.setText( "8" );
190 eightJLabel.setHorizontalAlignment( JLabel.CENTER );
191 windowJPanel.add( eightJLabel );
192
193 // set up enterJButton
194 enterJButton = new JButton();
195 enterJButton.setBounds( 280, 80, 135, 30 );
196 enterJButton.setText( "Enter" );
197 contentPane.add( enterJButton );
198 enterJButton.addActionListener(
199
200 new ActionListener() // anonymous inner class
201 {
202 // event handler called when enterJButton is clicked
203 public void actionPerformed( ActionEvent event )
204 {
205 enterJButtonActionPerformed( event );
206 }
207
208 } // end anonymous inner class
209
210 ); // end call to addActionListener
211
212 // set up inputJLabel
213 inputJLabel = new JLabel();
214 inputJLabel.setBounds( 280, 10, 135, 20 );
215 inputJLabel.setText( "Please make selection:" );
216 contentPane.add( inputJLabel );
217
218 // set up inputJTextField
219 inputJTextField = new JTextField();
220 inputJTextField.setBounds( 280, 35, 135, 25 );
221 inputJTextField.setHorizontalAlignment( JTextField.RIGHT );
222 contentPane.add( inputJTextField );
223
224 // set up displayJTextField
225 displayJTextField = new JTextField();
226 displayJTextField.setBounds( 10, 190, 260, 50 );
227 displayJTextField.setEditable( false );
228 displayJTextField.setHorizontalAlignment( JTextField.CENTER );
229 contentPane.add( displayJTextField );
230
231 // set properties of application’s window
232 setTitle( "Vending Machine" ); // set title bar string
233 setSize( 432, 275 ); // set window size
234 setVisible( true ); // display window
235
236 } // end method createUserInterface
237
238 // get user input and display price of specified snack
239 private void enterJButtonActionPerformed( ActionEvent event )
240 {
241 // get user input
242 int selection = Integer.parseInt( inputJTextField.getText() );
243
244 switch ( selection )
245 {
246 case 1: // user selected first snack
247 displayJTextField.setText( "Price: " + snackPrice1 );
248 break;
249
250 case 2: // user selected second snack
251 displayJTextField.setText( "Price: " + snackPrice2 );
252 break;
253
254 case 3: // user selected third snack
255 displayJTextField.setText( "Price: " + snackPrice3 );
256 break;
257
258 case 4: // user selected fourth snack
259 displayJTextField.setText( "Price: " + snackPrice4 );
260 break;
261
262 case 5: // user selected fifth snack
263 displayJTextField.setText( "Price: " + snackPrice5 );
264 break;
265
266 case 6: // user selected sixth snack
267 displayJTextField.setText( "Price: " + snackPrice6 );
268 break;
269
270 case 7: // user selected seventh snack
271 displayJTextField.setText( "Price: " + snackPrice7 );
272 break;
273
274 case 8: // user selected eighth snack
You might also like to view...
The numeric data types in C++ can be broken into two general categories which are
a. numbers and characters b. singles and doubles c. integers and floating-point numbers d. real and unreal numbers e. numbers and literals
Surge protectors are effective for preventing damage to the computer from fluctuations in ________
Fill in the blank(s) with correct word
The frame rate for broadcast television is roughly 60 fps.
Answer the following statement true (T) or false (F)
You use a ____ following the closing brace of an array initialization list.
A. . B. ; C. : D. ,