-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachineView.java
More file actions
302 lines (228 loc) · 8.65 KB
/
VendingMachineView.java
File metadata and controls
302 lines (228 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.BorderFactory;
import javax.swing.Box;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.awt.GridLayout;
//MinimalSwingUITemplate taken from - http://www.javapractices.com/topic/TopicAction.do?Id=231
public final class VendingMachineView extends JPanel implements ActionListener, VendingMachineViewInterface{
private static VendingMachineController controller;
public static void main(String... aArgs){
VendingMachineView app = new VendingMachineView();
controller = new VendingMachineController(app);
controller.populateMachine();
app.buildAndDisplayGui();
}
//---------------------------------------------------------------------------------------------
// Setup GUI
//---------------------------------------------------------------------------------------------
private void buildAndDisplayGui(){
JFrame frame = new JFrame("Vending Machine");
frame.setPreferredSize(new Dimension(900, 600));
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
JPanel vendingView = new JPanel();
frame.getContentPane().add(vendingView);
frame.getContentPane().add(buttons);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
buttons.add(Box.createVerticalStrut(40));
addScreentoUI(buttons);
buttons.add(Box.createVerticalStrut(40));
addCoinButtonsToUI(buttons);
buttons.add(Box.createVerticalStrut(20));
addCoinReturnAndSlotToUI(buttons);
buttons.add(Box.createVerticalStrut(100));
addKeypadToUI(buttons);
buttons.add(Box.createVerticalStrut(25));
addReloadButtonToUI(buttons);
addVendingMachineToUI(vendingView);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
JLabel screen;
private void addScreentoUI(JPanel panel){
screen = new JLabel("Vending machine is ready.");
screen.setBorder(BorderFactory.createLineBorder(Color.black, 2));
panel.add(screen);
}
private void addCoinButtonsToUI(JPanel panel){
JPanel coinPanel = new JPanel();
coinPanel.setPreferredSize(new Dimension(350, 10));
coinPanel.setLayout(new GridLayout(2, 4));
JButton onep = new JButton("1p");
onep.setActionCommand("coin1");
onep.addActionListener(this);
coinPanel.add(onep);
JButton twop = new JButton("2p");
twop.setActionCommand("coin2");
twop.addActionListener(this);
coinPanel.add(twop);
JButton fivep = new JButton("5p");
fivep.setActionCommand("coin5");
fivep.addActionListener(this);
coinPanel.add(fivep);
JButton tenp = new JButton("10p");
tenp.setActionCommand("coin10");
tenp.addActionListener(this);
coinPanel.add(tenp);
JButton twentyp = new JButton("20p");
twentyp.setActionCommand("coin20");
twentyp.addActionListener(this);
coinPanel.add(twentyp);
JButton fiftyp = new JButton("50p");
fiftyp.setActionCommand("coin50");
fiftyp.addActionListener(this);
coinPanel.add(fiftyp);
JButton onepound = new JButton("\u00A31");//\u00A3 is the pound symbol.
onepound.setActionCommand("coin100");
onepound.addActionListener(this);
coinPanel.add(onepound);
JButton twopound = new JButton("\u00A32");//\u00A3 is the pound symbol.
twopound.setActionCommand("coin200");
twopound.addActionListener(this);
coinPanel.add(twopound);
panel.add(coinPanel);
}
private void addCoinReturnAndSlotToUI(JPanel panel){
//we use a panel here in case we want to add more things
//onto the panel later, such as a picture of a coin slot.
JButton coinReturn = new JButton("COIN RETURN");
coinReturn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
controller.returnCoins();
}
});
panel.add(coinReturn);
}
private void addReloadButtonToUI(JPanel panel){
JButton reloadMachine = new JButton("ReloadMachine");
reloadMachine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
controller.reloadMachine();
}
});
panel.add(reloadMachine);
}
private void addVendingMachineToUI(JPanel panel){
String IMG_PATH = "./machine.jpg";
try{
BufferedImage img = ImageIO.read(new File(IMG_PATH));
ImageIcon icon = new ImageIcon(img);
panel.add(new JLabel(icon));
}
catch (IOException e) {
e.printStackTrace();
}
}
private void addKeypadToUI(JPanel panel){
JLabel label = new JLabel("Keypad");
panel.add(label);
JPanel keyPanel = new JPanel();
keyPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
// keyPanel.setPreferredSize(new Dimension(350, 40));
keyPanel.setLayout(new GridLayout(4, 3));
for(int i=1; i<=9; i++){
JButton key = new JButton(Integer.toString(i));
key.setActionCommand(Integer.toString(i));
key.addActionListener(this);
keyPanel.add(key);
}
JButton key = new JButton("<==");
key.setActionCommand(Integer.toString(-1));
key.addActionListener(this);
keyPanel.add(key);
JButton key2 = new JButton(Integer.toString(0));
key2.setActionCommand(Integer.toString(0));
key2.addActionListener(this);
keyPanel.add(key2);
firstPressed=-1;
panel.add(keyPanel);
}
//---------------------------------------------------------------------------------------------
// Handling Actions from buttons
//---------------------------------------------------------------------------------------------
private int firstPressed;
public void actionPerformed(ActionEvent e) {
String buttonID = e.getActionCommand();
//Coin Input
if(buttonID.charAt(0)=='c'){
String coin = buttonID.substring(4);
int val = Integer.parseInt(coin);
controller.coinInserted(val);
}
//Product selection.
else{
int keyPressed = Integer.parseInt(buttonID);
if(keyPressed==-1){ firstPressed=-1;
String message = "Current Selection: "+"__";
UpdateScreen(message);
}
else{
if(firstPressed == -1){
firstPressed = Integer.parseInt(buttonID);
String message = "Current Selection: "+buttonID +"_";
UpdateScreen(message);
}
else{
int choice = (firstPressed*10) + Integer.parseInt(buttonID);
String message = "Current Selection: "+choice;
UpdateScreen(message);
controller.madeChoiceOfItem(choice);
firstPressed = -1;
}
}
}
}
//---------------------------------------------------------------------------------------------
// Implementing VendingMachineViewInterface
//---------------------------------------------------------------------------------------------
public void UpdateScreen(String message){
screen.setText(message);
}
public void DispenseProduct(int product){
String name = controller.nameOfProductWithChoice(product);
JOptionPane.showMessageDialog(null, "Outputting item:"+ name);
//in this case this is equivalent to "Outputting item:"+ product);
}
public void GiveChange(int[] change){
String changeStr = "";
int totalReturn = 0;
for(int i = 0; i<controller.change().length; i++ ){
int noOfThisCoin = change[i];
if(noOfThisCoin>0){
int thisCoin = controller.change()[i];
int val = noOfThisCoin * thisCoin;
totalReturn += val;
if(thisCoin<100){
changeStr += Integer.toString(noOfThisCoin) + " x " + thisCoin+ "p.\n";
}
else{
changeStr += Integer.toString(noOfThisCoin) + " x \u00A3" + thisCoin/100+ "\n";
}
}
}
double value = totalReturn / 100.0;
DecimalFormat df = new DecimalFormat("#0.00");
changeStr+="total = " + "\u00A3" + df.format(value);
if(totalReturn==0) changeStr = "";
if(!changeStr.equals("")) JOptionPane.showMessageDialog(null, "Returning these coins:\n"+ changeStr);
UpdateScreen("Vending machine is ready.");
}
}