-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIMenu.java
393 lines (343 loc) · 11 KB
/
GUIMenu.java
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.*;
import javafx.stage.Stage;
import java.io.*;
/**
* Creates the GUI for Vending Machine
*/
public class GUIMenu extends Application
{
private VendingMachine machine;
private Button backButton;
private Stage primaryStage;
/**
* start() - overrides the start method in Application
*/
@Override
public void start(Stage ps) throws IOException
{
primaryStage = ps;
machine = new VendingMachine();
Scene userMenu = createUserMenu();
backButton = new Button("Back");
backButton.setOnAction(e -> primaryStage.setScene(userMenu));
primaryStage.setScene(userMenu);
primaryStage.setTitle("Vending Machine");
primaryStage.show();
primaryStage.setOnHiding(e -> {
try
{
WriteToFiles.writeStock(machine.getProducts());
WriteToFiles.writeCoins(machine.coins.getSet());
}
catch(IOException ex)
{
System.out.println("ERROR: Failed to save data.\n" + ex.getMessage());
}
});
}
/**
* createUserMenu() - Creates the UserMenu Scene
* @returns Scene - returns the scene to the primaryStage
*/
public Scene createUserMenu()
{
// Main menu for user
HBox buttonsBox = new HBox(2);
buttonsBox.setAlignment(Pos.CENTER);
Button show = new Button("Show products");
Button insert = new Button("Insert coin");
Button buy = new Button("Buy");
Button login = new Button("Admin menu");
Button quit = new Button("Quit");
buttonsBox.getChildren().addAll(show, insert, buy, login, quit);
show.setOnAction(e -> primaryStage.setScene(createShowProductsWindow()));
insert.setOnAction(e -> primaryStage.setScene(createInsertCoinWindow()));
buy.setOnAction(e -> primaryStage.setScene(createBuyWindow()));
login.setOnAction(e -> primaryStage.setScene(createLoginWindow()));
quit.setOnAction(e -> primaryStage.hide());
BorderPane borderPane = new BorderPane();
borderPane.setBottom(buttonsBox);
borderPane.setCenter(new Text("Hello please select an option: "));
Scene mainScene = new Scene(borderPane, 500, 300);
return mainScene;
}
/**
* createAdminMenu() - Creates the operator scene
* @returns Scene - returns the mainScene to primaryStage
*/
public Scene createAdminMenu()
{
// Main menu for operator
HBox buttonsBox = new HBox(2);
buttonsBox.setAlignment(Pos.CENTER);
Button addProduct = new Button("Add product");
Button removeCoins = new Button("Remove coins");
//Button quit = new Button("Quit");
buttonsBox.getChildren().addAll(addProduct, removeCoins, backButton);
addProduct.setOnAction(e -> primaryStage.setScene(createAddProductWindow()));
removeCoins.setOnAction(e -> primaryStage.setScene(createRemoveCoinsWindow()));
BorderPane borderPane = new BorderPane();
borderPane.setBottom(buttonsBox);
borderPane.setCenter(new Text("Hello please select an option: "));
Scene mainScene = new Scene(borderPane, 500, 300);
return mainScene;
}
/**
* createLoginWindow() creates the Login Scene
* @returns Scene
*/
public Scene createLoginWindow()
{
// Login menu
HBox loginBox = new HBox(2);
loginBox.setAlignment(Pos.CENTER);
Button loginButton = new Button("Login");
//Button backButton = new Button("Back");
loginBox.getChildren().addAll(loginButton, backButton);
HBox loginBox2 = new HBox(2);
Label label = new Label("Enter Code: ");
TextField tfOperatorCode = new TextField();
loginBox2.getChildren().addAll(label, tfOperatorCode);
loginBox2.setAlignment(Pos.CENTER);
loginButton.setOnAction(e -> {
try
{
if(machine.login(tfOperatorCode.getText()))
{
primaryStage.setScene(createAdminMenu());
}
}
catch(IOException ex)
{
messageWindow("ERROR: Failed to load operator code\n" + ex.getMessage(), createLoginWindow());
}
});
BorderPane borderLogin = new BorderPane();
borderLogin.setBottom(loginBox);
borderLogin.setCenter(loginBox2);
Scene login = new Scene(borderLogin, 500, 300);
return login;
}
/**
* createShowProductsWindow() makes the showProductScene
* @returns Scene
*/
public Scene createShowProductsWindow()
{
// Show products window
String temp = "";
for(Item p : machine.getProductTypes())
temp += p.getProduct().toString() + "\n";
Text productsText = new Text(temp);
productsText.setFont(new Font(16));
ScrollPane sp = new ScrollPane();
sp.setContent(productsText);
HBox productsBox = new HBox();
//Button backButton = new Button("Back");
productsBox.setAlignment(Pos.CENTER);
productsBox.getChildren().add(backButton);
BorderPane borderProducts = new BorderPane();
borderProducts.setCenter(sp);
borderProducts.setBottom(productsBox);
Scene showProducts = new Scene(borderProducts, 500, 300);
return showProducts;
}
/**
* createInsertCoinWindow() creates the insertCoin Scene
* @returns Scene
*/
public Scene createInsertCoinWindow()
{
// Insert coin menu
String temp = "";
char ch = 'A';
for(Coin c : VendingMachineMenu.getCoins())
{
temp += ch + ") " + c + "\n";
ch += 1;
}
Text coinsText = new Text(temp);
coinsText.setFont(new Font(16));
ScrollPane sp = new ScrollPane();
sp.setContent(coinsText);
HBox coinBox = new HBox();
Button insertButton = new Button("Insert coin");
coinBox.setAlignment(Pos.CENTER);
coinBox.getChildren().addAll(insertButton, backButton);
HBox inputBox = new HBox();
Label inputLabel = new Label("Which coin: ");
TextField coinChoice = new TextField();
inputBox.getChildren().addAll(inputLabel, coinChoice);
inputBox.setAlignment(Pos.CENTER);
insertButton.setOnAction(e -> {
if(coinChoice.getText().length() == 1)
{
int index = coinChoice.getText().toUpperCase().charAt(0) - 'A';
if(index >= 0 && index < VendingMachineMenu.getCoins().length)
{
machine.addCoin(VendingMachineMenu.getCoins()[index]);
coinChoice.setText("");
messageWindow("Coin added.", createInsertCoinWindow());
}
}
});
BorderPane borderInsert = new BorderPane();
borderInsert.setCenter(sp);
borderInsert.setTop(inputBox);
borderInsert.setBottom(coinBox);
Scene insertCoin = new Scene(borderInsert, 500, 300);
return insertCoin;
}
/**
* createBuyWindow() creates the Buy scene
* @returns Scene
*/
public Scene createBuyWindow()
{
String temp = "";
char ch = 'A';
for(Item p : machine.getProductTypes())
{
temp += ch + ") " + p.toString() + "\n";
ch += 1;
}
Text coinsText = new Text(temp);
coinsText.setFont(new Font(16));
ScrollPane sp = new ScrollPane();
sp.setContent(coinsText);
HBox box = new HBox();
Button buyButton = new Button("Buy");
box.setAlignment(Pos.CENTER);
box.getChildren().addAll(buyButton, backButton);
HBox inputBox = new HBox();
Label inputLabel = new Label("Which Product: ");
TextField choice = new TextField();
inputBox.getChildren().addAll(inputLabel, choice);
inputBox.setAlignment(Pos.CENTER);
buyButton.setOnAction(e -> {
if(choice.getText().length() == 1)
{
int index = choice.getText().toUpperCase().charAt(0) - 'A';
if(index >= 0 && index < machine.getProductTypes().length)
{
try
{
machine.buyProduct(machine.getProductTypes()[index]);
choice.setText("");
messageWindow("Product bought.", createBuyWindow());
}
catch (VendingException ex)
{
messageWindow(ex.getMessage(), createBuyWindow());
}
}
}
});
BorderPane borderInsert = new BorderPane();
borderInsert.setTop(inputBox);
borderInsert.setCenter(sp);
borderInsert.setBottom(box);
Scene buyProduct = new Scene(borderInsert, 500, 300);
return buyProduct;
}
/**
* createAddProductWindow() creates the addProduct Scene
* @returns Scene
*/
public Scene createAddProductWindow()
{
// Add product menu --- text fields for different information about product
//Scene addProduct; 3 labels 3 text boxes
//return addProduct;
Label firstLabel = new Label("Enter product name: ");
Label secondLabel = new Label("Enter price of product: ");
Label thirdLabel = new Label("Enter quantity: ");
TextField name = new TextField();
TextField price = new TextField();
TextField quantity = new TextField();
HBox addProductBox = new HBox();
addProductBox.setAlignment(Pos.CENTER);
Button addButton = new Button("Add product");
Button backAdminButton = new Button("Back");
addProductBox.getChildren().addAll(addButton,backAdminButton);
GridPane grid = new GridPane();
grid.add(firstLabel,0,0);
grid.add(secondLabel,0,1);
grid.add(thirdLabel,0,2);
grid.add(name,1,0);
grid.add(price,1,1);
grid.add(quantity,1,2);
grid.setAlignment(Pos.CENTER);
backAdminButton.setOnAction(e -> primaryStage.setScene(createAdminMenu()));
addButton.setOnAction(e -> {
if(name.getText().length() > 0 && price.getText().length() > 0 && quantity.getText().length() > 0)
{
machine.addProduct(new Product(name.getText(),Double.parseDouble(price.getText())), Integer.parseInt(quantity.getText()));
name.setText("");
price.setText("");
quantity.setText("");
messageWindow("Product added.", createAddProductWindow());
}
});
BorderPane borderAddProduct = new BorderPane();
borderAddProduct.setCenter(grid);
borderAddProduct.setBottom(addProductBox);
Scene addProducts = new Scene(borderAddProduct, 500, 300);
return addProducts;
}
/**
* createRemoveCoinsWindow() creates the removeCoin Scene
* @returns Scene
*/
public Scene createRemoveCoinsWindow()
{
String temp = "Removed: " + machine.removeMoney();
Text rCoinsText = new Text(temp);
rCoinsText.setFont(new Font(16));
ScrollPane sp = new ScrollPane();
sp.setContent(rCoinsText);
HBox removeCoinsBox = new HBox();
removeCoinsBox.setAlignment(Pos.CENTER);
Button okButton = new Button("OK");
removeCoinsBox.getChildren().add(okButton);
okButton.setOnAction(e -> primaryStage.setScene(createAdminMenu()));
BorderPane borderCoinsRemove = new BorderPane();
borderCoinsRemove.setCenter(sp);
borderCoinsRemove.setBottom(removeCoinsBox);
Scene removeCoins = new Scene(borderCoinsRemove, 500, 300);
return removeCoins;
}
/**
* messageWindow() displays a message to the user
* @param msg - string message
* @param scene - is the scene its displaying to
*/
public void messageWindow(String msg, Scene scene)
{
HBox buttonBox = new HBox();
Button okButton = new Button("OK");
buttonBox.setAlignment(Pos.CENTER);
buttonBox.getChildren().add(okButton);
Text msgText = new Text(msg);
BorderPane msgPane = new BorderPane();
msgPane.setCenter(msgText);
msgPane.setBottom(buttonBox);
okButton.setOnAction(e -> primaryStage.setScene(scene));
Scene s = new Scene(msgPane, 500, 300);
primaryStage.setScene(s);
}
}