-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVendingMachineMenu.java
141 lines (124 loc) · 3.22 KB
/
VendingMachineMenu.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
import java.util.*;
import java.io.IOException;
/**
A menu from the vending machine.
*/
public class VendingMachineMenu
{
private Scanner in;
private static Coin[] coins;
/**
Constructs a VendingMachineMenu object
*/
public VendingMachineMenu()
{
in = new Scanner(System.in);
}
/**
* Runs the vending machine system.
* @param machine the vending machine
* @throws IOException
*/
public void run(VendingMachine machine)
throws IOException
{
boolean more = true;
while (more)
{
System.out.println("S)how products I)nsert coin B)uy A)dmin Menu Q)uit");
String command = in.nextLine().toUpperCase();
if (command.equals("S"))
{ /*
getProductTypes() returns an array of products that doesn't contain duplicates
*/
for (Item p : machine.getProductTypes())
System.out.println(p.getProduct());
}
else if (command.equals("I")) //allows one coin be inserted at a time
{
machine.addCoin((Coin) getChoice(coins));
}
else if (command.equals("B"))
{
try
{
Item p = (Item) getChoice(machine.getProductTypes());
machine.buyProduct(p);
System.out.println("Purchased: " + p.getProduct().getName());
}
catch (VendingException ex)
{
System.out.println(ex.getMessage());
}
}
else if(command.equals("A")){
boolean fin = true;
System.out.print("Please enter your password\n");
String password = in.nextLine();
if(machine.login(password)){
while(fin){
System.out.println("A)dd Product R)emove coins B)ack");
command = in.nextLine().toUpperCase();
if (command.equals("A")){
System.out.println("Description:");
String description = in.nextLine();
System.out.println("Price:");
double price = in.nextDouble();
System.out.println("Quantity:");
int quantity = in.nextInt();
in.nextLine(); // read the new-line character
machine.addProduct(new Product(description, price), quantity);
}
else if(command.equals("R")){
System.out.println("Removed: " + machine.removeMoney());
}
else if(command.equals("B")){
fin = false;
}
}
}else
System.out.println("Wrong password");
}
else if (command.equals("Q"))
{
more = false;
}
}
WriteToFiles.writeStock(machine.getProducts());
WriteToFiles.writeCoins(machine.coins.getSet());
}
/**
* getChoice() shows you a list of objects with Letters as index
* @param Object Array
* @return Object picked by user
*/
private Object getChoice(Object[] choices)
{
while (true)
{
char c = 'A';
for (Object choice : choices)
{
System.out.println(c + ") " + choice);
c++;
}
String input = in.nextLine();
int n = input.toUpperCase().charAt(0) - 'A';
if (0 <= n && n < choices.length)
return choices[n];
}
}
/**
* setCoinArray() sets the values of coins array with the values from the ArrayList
* @param ArrayList of type Coin
*/
public static void setCoinArray(ArrayList<Coin> c)
{
coins = new Coin[c.size()];
for(int i = 0;i < c.size();i++)
coins[i] = c.get(i);
}
public static Coin [] getCoins(){
return coins;
}
}