forked from Kitch05/Vending-Machine-with-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachineView.java
More file actions
1299 lines (1075 loc) · 52.7 KB
/
Copy pathVendingMachineView.java
File metadata and controls
1299 lines (1075 loc) · 52.7 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/*
* VendingMachineView contains the GUI for the vending machine
* It displays various user-friendly features
*/
public class VendingMachineView {
private JFrame frame;
private JLabel infoLabel;
private JDialog customOrderDialog;
private RoundedCornerButton[] increaseButtons, decreaseButtons, itemButtons, moneyButtons, buttons, replenishButtons, decreaseCartButtons, increaseCartButtons, cartButtons;
private JComboBox<String> billDropdown, menuDropdown;
private VendingMachine machine;
private JTextField[] itemPriceTextFields;
private String[] numberOfBills = {"5", "10", "15", "20", "25"};
private String[] menu = {"Patties", "Add-ons", "Condiments"};
/*
* Constructs a VendingMachineView object.
* Initializes various Jcomponents to display vending machine features.
*/
public VendingMachineView() {
this.itemButtons = new RoundedCornerButton[20];
this.infoLabel = new JLabel();
this.buttons = new RoundedCornerButton[18];
this.moneyButtons = new RoundedCornerButton[11];
this.decreaseCartButtons = new RoundedCornerButton[20];
this.increaseCartButtons = new RoundedCornerButton[20];
this.billDropdown = new JComboBox<>(numberOfBills);
this.menuDropdown = new JComboBox<>(menu);
this.decreaseButtons = new RoundedCornerButton[20];
this.increaseButtons = new RoundedCornerButton[20];
this.replenishButtons = new RoundedCornerButton[20];
this.itemPriceTextFields = new JTextField[20];
initializeFrame();
initializeButtons();
initializeView();
}
/**
* Updates the machine stored in the view
* @param machine the updated machine
*/
public void updateMachine(VendingMachine machine) {
this.machine = machine;
}
/**
* Initializes the JFrame and sets up the initial frame properties.
*/
private void initializeFrame() {
frame = new JFrame("Vending Machine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(750, 500);
infoLabel = new JLabel();
frame.add(infoLabel, BorderLayout.NORTH);
frame.setVisible(true);
}
/**
* Initializes the increaseButtons, decreaseButtons,
* decreaseCartButtons, increaseCartButtons, buttons with a sent font, font size
*/
private void initializeButtons() {
Font buttonFont = new Font("Helvetica", Font.PLAIN, 14);
for (int i = 0; i < 12; i++) {
increaseButtons[i] = new RoundedCornerButton();
decreaseButtons[i] = new RoundedCornerButton();
}
for (int i = 0; i < 15; i++){
decreaseCartButtons[i] = new RoundedCornerButton();
increaseCartButtons[i] = new RoundedCornerButton();
}
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new RoundedCornerButton();
String[] buttonTitles = {
"Make a Vending Machine", "Test a Machine", "Exit",
"Maintenance", "Replenish Money", "Replenish Stock", "Exit",
"Set Item Price", "Collect Payment", "Print Summary", "Exit Maintenance",
"Exit", "Regular Vending Machine", "Special Vending Machine", "Cart" , "Purchase Order",
"Cancel Order", "Save"
};
buttons[i].setText(buttonTitles[i]);
buttons[i].setFont(buttonFont);
buttons[i].setBackground(new Color(0x060505));
buttons[i].setOpaque(false);
buttons[i].setContentAreaFilled(false);
buttons[i].setBorderPainted(false);
buttons[i].setFocusPainted(false);
}
}
/**
* Initializes and displays the initial options for the vending machine main menu.
*/
public void initializeView() {
frame.getContentPane().removeAll();
Color backgroundColor = new Color(0xF6B94B);
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBackground(backgroundColor);
mainPanel.setBorder(BorderFactory.createLineBorder(new Color(73, 55, 22), 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
JLabel emptyLabel = new JLabel("");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weighty = 1.0;
mainPanel.add(emptyLabel, gbc);
JPanel vendingLabelPanel = new JPanel();
JLabel vendingLabel = new JLabel("The Burger Machine", SwingConstants.CENTER);
Font vendingLabelFont = loadCustomFont("Anybody-Medium.ttf", Font.BOLD, 48);
vendingLabel.setFont(vendingLabelFont);
vendingLabel.setForeground(new Color(73, 55, 22));
vendingLabelPanel.add(vendingLabel);
vendingLabelPanel.setBackground(new Color(249, 213, 147));
mainPanel.add(vendingLabelPanel, gbc);
gbc.gridy = 1;
mainPanel.add(emptyLabel, gbc);
JPanel buttonsPanel = new JPanel(new GridLayout(3, 1, 0, 10));
buttonsPanel.setBackground(new Color(192, 192, 192));
Font buttonFont = loadCustomFont("Anybody-Medium.ttf", Font.PLAIN, 15);
for (int i = 0; i < 3; i++) {
buttons[i].setFont(buttonFont);
buttons[i].setBackground(new Color(73, 55, 22));
buttonsPanel.add(buttons[i]);
}
gbc.gridy = 2;
gbc.weighty = 0.0;
mainPanel.add(buttonsPanel, gbc);
JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
separator.setPreferredSize(new Dimension(180, 2));
separator.setForeground(backgroundColor);
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(separator, gbc);
gbc.gridy = 4;
JLabel productionLabel = new JLabel("Ken and Joseph Production", SwingConstants.CENTER);
Font productionLabelFont = loadCustomFont("Anybody-Medium.ttf", Font.BOLD, 25);
productionLabel.setFont(productionLabelFont);
productionLabel.setForeground(new Color(73, 55, 22));
mainPanel.add(productionLabel, gbc);
for (int i = 5; i < 10; i++) {
gbc.gridy = i;
mainPanel.add(emptyLabel, gbc);
}
ImageIcon logoIcon = new ImageIcon("burgermachine.gif");
JLabel logoLabel = new JLabel(logoIcon);
logoLabel.setPreferredSize(new Dimension(300, 300));
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 10;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(logoLabel, gbc);
vendingLabelPanel.setBackground(backgroundColor);
buttonsPanel.setBackground(backgroundColor);
productionLabel.setBackground(backgroundColor);
frame.add(mainPanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(789, 369));
setupFrame();
}
/**
* Loads a custom font and if it cannot be found, it loads a default font
*
* @param fontFileName The filename
* @param style The font style (bold or plain)
* @param size The font size
* @return The custom or default Font
*
* */
private Font loadCustomFont(String fontFileName, int style, int size) {
try {
Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontFileName));
GraphicsEnvironment graphics = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphics.registerFont(customFont);
return customFont.deriveFont(style, size);
} catch (IOException | FontFormatException e) {
System.err.println("Error loading custom font: " + e.getMessage());
return new Font("Helvetica Neue", style, size);
}
}
/**
* Displays regular or special machine options
*/
public void displayVendingOptions() {
this.frame.getContentPane().removeAll();
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
innerPanel.setBackground(new Color(249,213,147));
outerPanel.setBackground(new Color(249,213,147));
buttons[12].setPreferredSize(new Dimension(180, 100));
buttons[13].setPreferredSize(new Dimension(180, 100));
innerPanel.add(buttons[12]);
innerPanel.add(buttons[13]);
outerPanel.add(innerPanel, BorderLayout.CENTER);
this.frame.add(outerPanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(391, 149));
setupFrame();
}
/**
* Creates and returns a panel with denominations of money as buttons.
*
* @param denominations an array of doubles that contains valid PHP danominations
* @return The JPanel with denominations of money as buttons
*/
private JPanel createMoneyButtonsPanel(double[] denominations) {
JPanel moneyButtonsPanel = new JPanel(new GridBagLayout());
moneyButtonsPanel.setBackground(new Color(6, 5, 5));
moneyButtons = new RoundedCornerButton[denominations.length];
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
for (int i = 0; i < denominations.length; i++) { //Initialize Money Buttons
double denomination = denominations[i];
moneyButtons[i] = new RoundedCornerButton(String.valueOf(denomination));
moneyButtons[i].setPreferredSize(new Dimension(100, 40));
moneyButtons[i].setFont(new Font("Helvetica", Font.PLAIN, 12));
moneyButtons[i].setBackground(new Color(192, 192, 192));
gbc.gridx = i % 2;
gbc.gridy = i / 2;
if (i == denominations.length - 1) {
gbc.gridwidth = 2;
} else {
gbc.gridwidth = 1;
}
moneyButtonsPanel.add(moneyButtons[i], gbc);
}
return moneyButtonsPanel;
}
/**
* Creates and returns a panel with maintenance and exit button.
*
* @return The JPanel containing the maintenance and exit button.
*/
private JPanel createMaintenanceExitPanel() {
JPanel maintenanceExitPanel = new JPanel(new BorderLayout());
Dimension buttonSize = new Dimension(110, 25);
JPanel exitPanel = new JPanel();
buttons[3].setPreferredSize(buttonSize);
buttons[3].setFont(new Font("Helvetica", Font.PLAIN, 12));
exitPanel.add(buttons[3]);
exitPanel.setBackground(Color.BLACK);
buttons[6].setPreferredSize(buttonSize);
buttons[6].setFont(new Font("Helvetica", Font.PLAIN, 12));
exitPanel.add(buttons[6]);
maintenanceExitPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.NORTH);
maintenanceExitPanel.add(exitPanel, BorderLayout.SOUTH);
maintenanceExitPanel.setBackground(Color.BLACK);
return maintenanceExitPanel;
}
/**
* Displays the vending machine with its testing features
*
* @param list An array of Items that are inside the vending machine
*/
public void displayTestVM(Item[] list) {
frame.getContentPane().removeAll();
Color backgroundColor = new Color(246, 185, 75);
JPanel mainPanel = new JPanel(new BorderLayout());
Item[] itemList = list;
itemButtons = createButtons("Buy", itemList);
JScrollPane itemsPanel = null;
if (machine instanceof SpecialMachine) { //Display testing features in special machine
cartButtons = createButtons("Add", itemList);
JPanel dropdownPanel = createDropDown(menuDropdown, menu);
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
topPanel.add(dropdownPanel);
JPanel cartButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttons[14].setPreferredSize(new Dimension(90, 20));
cartButtonPanel.add(buttons[14]);
topPanel.add(cartButtonPanel);
mainPanel.add(topPanel, BorderLayout.NORTH);
itemsPanel = createItemsPanel(itemButtons, cartButtons, itemList, this::createStockPriceCaloriesInfoPanel);
} else
itemsPanel = createItemsPanel(itemButtons, null, itemList, this::createStockPriceCaloriesInfoPanel);
JPanel maintenanceExitPanel = createMaintenanceExitPanel();
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add(Box.createVerticalStrut(20), BorderLayout.CENTER);
leftPanel.add(setupInsertMoneyPanel(), BorderLayout.CENTER);
leftPanel.add(maintenanceExitPanel, BorderLayout.SOUTH);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, itemsPanel);
splitPane.setDividerLocation(250);
int frameWidth = (300 * 3 + 300) + 100;
frame.add(splitPane, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(frameWidth, 650));
leftPanel.setBackground(backgroundColor);
maintenanceExitPanel.setBackground(backgroundColor);
itemsPanel.setBackground(backgroundColor);
mainPanel.setBackground(new Color(220, 230, 240));
mainPanel.add(splitPane, BorderLayout.CENTER);
frame.add(mainPanel, BorderLayout.CENTER);
setupFrame();
}
/**
* Sets up and returns the panel for inserting money with buttons for different denominations.
*
* @return The JPanel for inserting money.
*/
private JPanel setupInsertMoneyPanel() {
double[] moneyDenominations = {1000, 500, 200, 100, 50, 20, 10, 5, 1, 0.5, 0.25};
JPanel panel = new JPanel(new BorderLayout());
infoLabel = new JLabel("Money: P");
infoLabel.setFont(new Font("Helvetica", Font.PLAIN, 20));
infoLabel.setForeground(Color.white);
Border border = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border roundedBorder = BorderFactory.createCompoundBorder(
border, BorderFactory.createSoftBevelBorder(SoftBevelBorder.RAISED, Color.white, Color.white)
);
infoLabel.setBorder(roundedBorder);
infoLabel.setOpaque(true);
infoLabel.setBackground(new Color(6, 5, 5));
JPanel insertMoneyPanel = createMoneyButtonsPanel(moneyDenominations);
panel.add(infoLabel, BorderLayout.NORTH);
panel.add(Box.createVerticalStrut(20), BorderLayout.CENTER);
panel.add(insertMoneyPanel, BorderLayout.CENTER);
return panel;
}
/**
* Creates and returns a JScrollPane containing the shopping cart items for the special machine.
*
* @return JScrollPane containing the custom order in the shopping cart.
*/
public JScrollPane shoppingCart() {
JPanel inventoryPanel = new JPanel();
inventoryPanel.setLayout(new BoxLayout(inventoryPanel, BoxLayout.Y_AXIS));
Color color = new Color(255, 255, 255);
inventoryPanel.setBackground(color);
inventoryPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
if (machine instanceof SpecialMachine) {
SpecialMachine specialMachine = (SpecialMachine) machine;
int customOrderIndex = 0; //Items counter
for (Item item : machine.getItemList()) {
int counter = 0;
for (Item check : specialMachine.getCustomOrder()) {
if (check.getName().equals(item.getName()))
counter++;
}
if (counter != 0) {
JPanel itemPanel = new JPanel(new GridBagLayout());
itemPanel.setPreferredSize(new Dimension(400, 100));
itemPanel.setBackground(color);
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
ImageIcon itemImageIcon = new ImageIcon(item.getName() + ".png");
Image itemImage = itemImageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH);
JLabel imageLabel = new JLabel(new ImageIcon(itemImage));
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridheight = 4;
constraints.insets = new Insets(5, 15, 5, 15);
itemPanel.add(imageLabel, constraints);
JLabel nameLabel = new JLabel(item.getName());
nameLabel.setHorizontalAlignment(JLabel.LEFT);
nameLabel.setVerticalAlignment(JLabel.CENTER);
nameLabel.setPreferredSize(new Dimension(200, 30));
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 15);
itemPanel.add(nameLabel, constraints);
RoundedCornerButton minusButton = new RoundedCornerButton("-");
RoundedCornerButton plusButton = new RoundedCornerButton("+");
minusButton.setPreferredSize(new Dimension(40, 20));
plusButton.setPreferredSize(new Dimension(40, 20));
minusButton.setFont(new Font("Helvetica", Font.BOLD, 10));
plusButton.setFont(new Font("Helvetica", Font.BOLD, 10));
minusButton.setFocusPainted(false);
plusButton.setFocusPainted(false);
increaseCartButtons[customOrderIndex] = plusButton;
decreaseCartButtons[customOrderIndex] = minusButton;
JLabel quantityLabel = new JLabel("" + counter + "");
quantityLabel.setHorizontalAlignment(JLabel.CENTER);
quantityLabel.setVerticalAlignment(JLabel.CENTER);
JPanel quantityButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
quantityButtonPanel.setBackground(color);
quantityButtonPanel.setPreferredSize(new Dimension(100, 30));
quantityButtonPanel.add(decreaseCartButtons[customOrderIndex]);
quantityButtonPanel.add(quantityLabel);
quantityButtonPanel.add(increaseCartButtons[customOrderIndex]);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.WEST;
itemPanel.add(quantityButtonPanel, constraints);
double totalPrice = counter * item.getPrice();
double totalCalories = counter* item.getCalories();
JLabel totalPriceLabel = new JLabel("P" + totalPrice + " | Calories: " + totalCalories);
totalPriceLabel.setHorizontalAlignment(JLabel.LEFT);
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridheight = 2;
constraints.anchor = GridBagConstraints.WEST;
itemPanel.add(totalPriceLabel, constraints);
JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
separator.setBackground(new Color(0, 0, 0));
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(5, 15, 5, 15);
itemPanel.add(separator, constraints);
customOrderIndex++; //Increment items counter
inventoryPanel.add(itemPanel);
}
}
}
JPanel buttonsPanel = new JPanel(new GridLayout(2, 5, 5, 5));
buttonsPanel.add(buttons[15]);
buttonsPanel.add(buttons[16]);
JPanel containerPanel = new JPanel(new BorderLayout());
containerPanel.add(buttonsPanel, BorderLayout.NORTH);
containerPanel.add(inventoryPanel, BorderLayout.CENTER);
containerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JScrollPane scrollPane = new JScrollPane(containerPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return scrollPane;
}
/**
* Displays a JDialog of the custom order in the shopping cart.
*/
public void showCustomOrderInventoryDialog() {
JScrollPane customOrderPanel = shoppingCart();
customOrderDialog = new JDialog();
customOrderDialog.setTitle("Shopping Cart");
customOrderDialog.setSize(600, 400);
customOrderDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
customOrderDialog.add(customOrderPanel);
customOrderDialog.setLocationRelativeTo(null);
customOrderDialog.setVisible(true);
}
/**
* Displays maintenance features
*
* @param currentMoneyStored Current amount of money in the machine
*/
public void displayMaintenance(double currentMoneyStored) {
frame.getContentPane().removeAll();
JPanel infoPanel = new JPanel(new BorderLayout());
JLabel infoLabel = new JLabel("Current Money Stored: P" + currentMoneyStored, SwingConstants.CENTER);
infoLabel.setFont(new Font("Monospaced Bold", Font.PLAIN, 14));
infoPanel.add(infoLabel, BorderLayout.EAST);
JPanel mainPanel = new JPanel(new GridLayout(3, 2, 10, 10));
mainPanel.setBackground(new Color(249,213,147));
Dimension buttonSize = new Dimension(250, 150);
for (int i = 4; i <= 10; i++) {
if (i != 6) {
buttons[i].setPreferredSize(buttonSize);
mainPanel.add(buttons[i]);
}
}
frame.add(infoPanel, BorderLayout.NORTH);
frame.add(mainPanel, BorderLayout.CENTER);
setupFrame();
}
/**
* Displays a gif and a label with a specified time
*
* @param gifPath String for the file name of the gif
* @param label String for the label
* @param durationInSeconds The duration in seconds for how long the message will last
*/
public void displayMessage(String gifPath, String label, int durationInSeconds) {
JDialog message = createGIF(gifPath, label);
messageDuration(message, durationInSeconds);
}
/**
* Creates and returns a JDialog displaying a GIF with a label
*
* @param gifPath String for the file name of the gif
* @param label String for the label
* @return The JDialog.
*/
private JDialog createGIF(String gifPath, String label) {
JDialog message = new JDialog(frame);
message.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
message.setLayout(new BorderLayout());
message.setSize(300, 300);
message.setLocationRelativeTo(frame);
ImageIcon gifIcon = new ImageIcon(gifPath); //gif
JLabel gifLabel = new JLabel(gifIcon, SwingConstants.CENTER);
message.add(gifLabel, BorderLayout.CENTER);
JLabel messageLabel = new JLabel(label, SwingConstants.CENTER);
message.add(messageLabel, BorderLayout.SOUTH);
return message;
}
/**
* Sets the duration in seconds for how long the message will last
*
* @param message The label
* @param durationInSeconds The duration in seconds for how long the message will last
*/
private void messageDuration(JDialog message, int durationInSeconds) {
message.setVisible(true);
Timer timer = new Timer(durationInSeconds * 1000, e -> {
message.dispose();
});
timer.setRepeats(false);
timer.start();
}
/**
* Displays replenish money maintenance feature, where the money inside the machine can be set
*/
public void displayReplenishMoney() {
frame.getContentPane().removeAll();
double moneystored = machine.displayMoneyStored();
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(49, 37, 15));
infoLabel = new JLabel("Money: P " + moneystored);
infoLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
infoLabel.setForeground(Color.WHITE);
topPanel.add(infoLabel);
JPanel replenishMoneyPanel = createMoneyButtonsPanelWithAdjustment();
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new BorderLayout());
JPanel exitPanel = createExitPanel();
centerPanel.add(replenishMoneyPanel, BorderLayout.CENTER);
centerPanel.add(exitPanel, BorderLayout.SOUTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.setBackground(new Color(249,213,147));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.add(mainPanel);
setupFrame();
}
/**
* Creates and returns a JPanel containing money denominations array with a pair of
* increase and decrease buttons for each denomination
*
* @return The JPanel containing the money denomination buttons.
*/
private JPanel createMoneyButtonsPanelWithAdjustment() {
JPanel moneyButtonsPanel = new JPanel(new GridLayout(2, 6, 10, 10));
moneyButtonsPanel.setBackground(new Color(240, 240, 240));
double[] customOrderDenominations = {1000, 500, 200, 100, 50, 20, 10, 5, 1, 0.5, 0.25, 0.1};
for (int i = 0; i < customOrderDenominations.length; i++) {
JPanel denominationPanel = new JPanel(new BorderLayout());
denominationPanel.setBackground(new Color(255, 255, 255));
denominationPanel.setBorder(BorderFactory.createLineBorder(new Color(150, 150, 150), 1));
denominationPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
denominationPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(150, 150, 150), 1),
BorderFactory.createEmptyBorder(10, 10, 10, 10)
));
denominationPanel.setOpaque(true);
denominationPanel.setBackground(new Color(220, 220, 220));
RoundedCornerButton decreaseButton = createMoneyAdjustmentButton("-", "-" + i); //decrease button
denominationPanel.add(decreaseButton, BorderLayout.WEST);
RoundedCornerButton increaseButton = createMoneyAdjustmentButton("+", "+" + i); //increase button
denominationPanel.add(increaseButton, BorderLayout.EAST);
decreaseButtons[i] = decreaseButton;
increaseButtons[i] = increaseButton;
JLabel denominationLabel = new JLabel(String.valueOf(customOrderDenominations[i]));
denominationLabel.setFont(new Font("Helvetica", Font.BOLD, 14));
denominationLabel.setHorizontalAlignment(SwingConstants.CENTER);
denominationLabel.setVerticalAlignment(SwingConstants.CENTER);
denominationPanel.add(denominationLabel, BorderLayout.CENTER);
moneyButtonsPanel.add(denominationPanel);
}
TitledBorder titledBorder = BorderFactory.createTitledBorder("Replenish Money");
titledBorder.setTitleFont(new Font("Helvetica", Font.BOLD, 16));
titledBorder.setTitleColor(Color.DARK_GRAY);
moneyButtonsPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(20, 20, 20, 20),
titledBorder
));
return moneyButtonsPanel;
}
/**
* Adjusts the decrease and increase buttons
*/
private RoundedCornerButton createMoneyAdjustmentButton(String text, String actionCommand) {
RoundedCornerButton button = new RoundedCornerButton(text);
button.setBackground(new Color(54, 92, 160));
button.setForeground(Color.BLACK);
button.setPreferredSize(new Dimension(40, 30));
button.setMargin(new Insets(5, 12, 5, 12));
button.setActionCommand(actionCommand);
return button;
}
/**
* Creates and returns a JPanel that has a dropdown menu for selecting different options.
*
* @param dropdown The JComboBox for the dropdown menu.
* @param options The array of strings that displays different options.
* @return The JPanel that has the dropdown menu.
*/
private JPanel createDropDown(JComboBox<String> dropdown, String[] options) {
JPanel billsButtonsPanel = new JPanel(new GridBagLayout());
billsButtonsPanel.setBackground(new Color(240, 240, 240));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
dropdown.setPreferredSize(new Dimension(100, 20));
dropdown.setFont(new Font("Tahoma", Font.PLAIN, 14));
dropdown.setBackground(new Color(240, 240, 240));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
billsButtonsPanel.add(dropdown, gbc);
return billsButtonsPanel;
}
/**
* Creates and returns a JScrollPane that displays the items in the vending machine with different JComponents to interact with them.
*
*
* @param buttons An array of JComponents that can buy, replenish stock, or set the price of an item
* @param cButtons An optional array of JComponents that can make an item be added to the cart
* @param itemList An array of Items that will be displayed on the panel
* @param infoPanelGenerator An optional function that displays info about the item
* @return The JScrollPane displays the items in the vending machine.
*/
private JScrollPane createItemsPanel(JComponent[] buttons, JComponent[] cButtons, Item[] itemList, Function<Item, JPanel> infoPanelGenerator) {
JPanel itemsPanel = new JPanel(new GridBagLayout());
itemsPanel.setBackground(new Color(246, 185, 75));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
int itemsPerRow = 3;
for (int i = 0; i < itemList.length; i++) {
Item item = itemList[i];
JPanel itemPanel = new JPanel(new BorderLayout());
itemPanel.setPreferredSize(new Dimension(300, 380));
itemPanel.setBackground(new Color(246, 185, 75));
itemPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
itemPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
if (buttons != null) {
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.setOpaque(false);
buttonPanel.add(buttons[i]);
if (cButtons != null)
buttonPanel.add(cButtons[i]);
itemPanel.add(buttonPanel, BorderLayout.NORTH);
} else {
JLabel itemNameLabel = new JLabel(item.getName());
itemNameLabel.setHorizontalAlignment(JLabel.CENTER);
itemNameLabel.setVerticalAlignment(JLabel.CENTER);
itemPanel.add(itemNameLabel, BorderLayout.NORTH);
}
JPanel imagePanel = new JPanel(new BorderLayout());
imagePanel.setBackground(new Color(246, 185, 75));
String imagePath = item.getName() + ".png"; //image of the item
ImageIcon itemIcon = new ImageIcon(imagePath);
JLabel itemLabel = new JLabel(itemIcon);
itemLabel.setHorizontalAlignment(JLabel.CENTER);
itemLabel.setVerticalAlignment(JLabel.CENTER);
imagePanel.add(itemLabel, BorderLayout.CENTER);
itemPanel.add(imagePanel, BorderLayout.CENTER);
if (infoPanelGenerator != null) {
JPanel infoPanel = infoPanelGenerator.apply(item);
itemPanel.add(infoPanel, BorderLayout.SOUTH);
}
int row = i / itemsPerRow;
int column = i % itemsPerRow;
gbc.gridx = column;
gbc.gridy = row;
gbc.gridwidth = 1;
itemsPanel.add(itemPanel, gbc);
}
JScrollPane scrollPane = new JScrollPane(itemsPanel);
return scrollPane;
}
/**
* Creates and returns a JPanel that has the name, stock, price, and calories information for an item with icons.
*
* @param item The Item that will be displayed with information
* @return The JPanel that has the name, stock, price, and calories information for an item with icons.
*/
private JPanel createStockPriceCaloriesInfoPanel(Item item) {
JPanel infoPanel = new JPanel(new GridLayout(4, 2, 5, 2));
infoPanel.setBackground(new Color(246, 185, 75));
JLabel nameLabel = new JLabel(item.getName());
nameLabel.setFont(new Font("Helvetica", Font.BOLD, 16));
nameLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel stockIcon = new JLabel(new ImageIcon("stock_icon.png"));
JLabel stockLabel = new JLabel("Stock: " + item.getStock().size());
JLabel priceIcon = new JLabel(new ImageIcon("price_icon.png"));
JLabel priceLabel = new JLabel("Price: P" + item.getPrice());
JLabel caloriesIcon = new JLabel(new ImageIcon("calories_icon.png"));
JLabel caloriesLabel = new JLabel("Calories: " + item.getCalories());
Font detailsFont = new Font("Helvetica", Font.PLAIN, 12);
stockLabel.setFont(detailsFont);
priceLabel.setFont(detailsFont);
caloriesLabel.setFont(detailsFont);
infoPanel.add(nameLabel);
infoPanel.add(new JLabel());
infoPanel.add(stockIcon);
infoPanel.add(stockLabel);
infoPanel.add(priceIcon);
infoPanel.add(priceLabel);
infoPanel.add(caloriesIcon);
infoPanel.add(caloriesLabel);
return infoPanel;
}
/**
* Creates and returns a JPanel that has the initial and final inventory for an item with icons.
*
* @param item The Item that will be displayed with information
* @return The JPanel that has the initial and final inventory for an item with icons.
*/
private JPanel createInitialFinalInventoryInfoPanel(Item item) {
int temp = 0;
Item[] itemList = machine.getItemList();
for (int i = 0; i < itemList.length; i++){
if(item == itemList[i]){
temp = i;
}
}
JPanel infoPanel = new JPanel(new GridLayout(2, 2, 5, 2));
infoPanel.setBackground(new Color(246, 185, 75));
JLabel initialInventoryIcon = new JLabel(new ImageIcon("initial_inventory_icon.png"));
JLabel initialInventoryLabel = new JLabel("Initial Inventory: " + machine.getInitialInventory().get(temp));
JLabel finalInventoryIcon = new JLabel(new ImageIcon("final_inventory_icon.png"));
JLabel finalInventoryLabel = new JLabel("Final Inventory: " + machine.getFinalInventory().get(temp));
Font detailsFont = new Font("Helvetica", Font.PLAIN, 12);
initialInventoryLabel.setFont(detailsFont);
finalInventoryLabel.setFont(detailsFont);
infoPanel.add(initialInventoryIcon);
infoPanel.add(initialInventoryLabel);
infoPanel.add(finalInventoryIcon);
infoPanel.add(finalInventoryLabel);
return infoPanel;
}
/**
* Displays replenish stock maintenance feature, where the stock of an item inside the machine can be set
*/
public void displayReplenishStock() {
frame.getContentPane().removeAll();
Color bgcolor = new Color(249,213,147);
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(49, 37, 15));
JLabel titleLabel = new JLabel("Replenish Stock");
titleLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
titleLabel.setForeground(Color.WHITE);
topPanel.add(titleLabel);
Item[] itemList = machine.getItemList();
replenishButtons = createButtons("Replenish", itemList);
JScrollPane itemsScrollPane = createItemsPanel(replenishButtons, null, itemList, this::createStockPriceCaloriesInfoPanel);
JPanel exitPanel = createExitPanel();
exitPanel.setBackground(bgcolor);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(itemsScrollPane, BorderLayout.CENTER);
mainPanel.add(exitPanel, BorderLayout.SOUTH);
mainPanel.setBackground(bgcolor);
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.add(mainPanel);
setupFrame();
}
/**
* Displays set item price maintenance feature, where the price of an item can be set
*/
public void displaySetItemPrice() {
frame.getContentPane().removeAll();
Item[] itemList = machine.getItemList();
Color bgcolor = new Color(249,213,147);
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(49, 37, 15));
JLabel titleLabel = new JLabel("Set Item Price");
titleLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
titleLabel.setForeground(Color.WHITE);
topPanel.add(titleLabel);
JTextField[] priceTextFields = createEditableItemPriceFields(itemList); //textfields
JScrollPane itemsScrollPane = createItemsPanel(priceTextFields, null, itemList, this::createStockPriceCaloriesInfoPanel);
JPanel exitPanel = new JPanel(new FlowLayout());
exitPanel.setBackground(bgcolor);
buttons[11].setFont(new Font("Helvetica", Font.PLAIN, 14));
buttons[11].setPreferredSize(new Dimension(100, 30));
exitPanel.add(buttons[11]);
buttons[17].setFont(new Font("Helvetica", Font.PLAIN, 14));
buttons[17].setPreferredSize(new Dimension(100, 30));
exitPanel.add(buttons[17]);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(itemsScrollPane, BorderLayout.CENTER);
mainPanel.add(exitPanel, BorderLayout.SOUTH);
mainPanel.setBackground(bgcolor);
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.add(mainPanel);
setupFrame();
frame.pack();
}
/**
* Creates and returns an array of text fields to set item prices.
*
* @param itemList The array of Items to have text fields for
* @return An array of JTextFields for setting item prices.
*/
private JTextField[] createEditableItemPriceFields(Item[] itemList) {
itemPriceTextFields = new JTextField[itemList.length];
for (int i = 0; i < itemList.length; i++) {
JTextField priceField = new JTextField();
priceField.setFont(new Font("Helvetica", Font.PLAIN, 12));
priceField.setPreferredSize(new Dimension(100, 20));
itemPriceTextFields[i] = priceField;
}
return itemPriceTextFields;
}
/**
* Creates and returns an array of RoundedCornerButtons with a label.
*
* @param labelPrefix The label of the button
* @param itemList The array of Items to create buttons for
* @return An array of RoundedCornerButton objects with a label.
*/
private RoundedCornerButton[] createButtons(String labelPrefix, Item[] itemList) {
Font buttonFont = new Font("Helvetica", Font.PLAIN, 14);
RoundedCornerButton[] buttons = new RoundedCornerButton[itemList.length];
for (int i = 0; i < itemList.length; i++) {
RoundedCornerButton button = new RoundedCornerButton(labelPrefix);
button.setPreferredSize(new Dimension(100, 40));
button.setFont(buttonFont);
button.setBackground(new Color(246, 185, 75));
button.setForeground(Color.BLACK);
buttons[i] = button;
}
return buttons;
}
/**