-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor_machine.cpp
224 lines (208 loc) · 6.45 KB
/
vendor_machine.cpp
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
#include <iostream>
#include <unordered_map>
using namespace std;
enum class State { //declaring the states
IDLE,
CHOOSE_DRINK,
COLLECT_COINS,
OUT,
REFILL,
CANCEL
};
class VendingMachine { //creating a class representing Vending Machine
public:
VendingMachine() : currentState(State::IDLE), selectedDrink(""), insertedAmount(0), totalCount(400) {}
void run() {
while (true) {
displayCurrentState();
handleState(currentState);
}
}
private:
State currentState;
string selectedDrink;
int insertedAmount;
int totalCount;
unordered_map<string, int> drinkStocks = {
{"PEPS", 50},
{"MDEW", 50},
{"DPEP", 50},
{"COKE", 50},
{"GATO", 50},
{"DCOK", 50},
{"MINM", 50},
{"TROP", 50}
};
unordered_map<string, int> drinkCosts = {
{"PEPS", 30},
{"MDEW", 30},
{"DPEP", 50},
{"COKE", 20},
{"GATO", 20},
{"DCOK", 30},
{"MINM", 25},
{"TROP", 30}
};
void displayCurrentState() { // this function displays the current State of the machine
cout<< "----------------------\n";
cout << "Current State: ";
switch (currentState) {
case State::IDLE:
cout << "IDLE\n";
cout << "----------------------\n";
displayDrinkMenu();
break;
case State::CHOOSE_DRINK:
cout << "CHOOSE_DRINK\n";
cout << "----------------------\n";
break;
case State::COLLECT_COINS:
cout << "COLLECT_COINS\n";
cout << "----------------------\n";
break;
case State::OUT:
cout << "OUT\n";
cout << "----------------------\n";
break;
}
}
void displayDrinkMenu() {
cout << "Drink Menu:\n";
cout << "1. Pepsi (PEPS) - Cost: 30 , Quantity: "<<drinkStocks["PEPS"]<<endl;
cout << "2. Mountain Dew (MDEW) - Cost: 30 , Quantity: "<<drinkStocks["MDEW"]<<endl;
cout << "3. Dr. Pepper (DPEP) - Cost: 50 , Quantity: "<<drinkStocks["DPEP"]<<endl;
cout << "4. Coke (COKE) - Cost: 20 , Quantity: "<<drinkStocks["COKE"]<<endl;
cout << "5. Gatorade (GATO) - Cost: 20 , Quantity: "<<drinkStocks["GATO"]<<endl;
cout << "6. Diet Coke (DCOK) - Cost: 30 , Quantity: "<<drinkStocks["DCOK"]<<endl;
cout << "7. Minute Maid (MINM) - Cost: 25 , Quantity: "<<drinkStocks["MINM"]<<endl;
cout << "8. Tropicana (TROP) - Cost: 30 , Quantity: "<<drinkStocks["TROP"]<<endl;
cout << "----------------------\n";
}
void handleState(State state) {
switch (state) {
case State::IDLE:
idle_state();
break;
case State::CHOOSE_DRINK:
select_drink();
break;
case State::COLLECT_COINS:
collect_coins();
break;
case State::OUT:
give_drink();
break;
case State::REFILL:
refill();
break;
case State::CANCEL:
cancel();
break;
}
}
string take_input(){
string input="";
cin>>input;
return input;
}
//-------------------------------
void idle_state(){
if(totalCount<=0){
cout<<"NO MORE DRINKS LEFT\nTo refill the machine\nType: REFILL\n";
string input= take_input();
if(input=="REFILL")
currentState=State::REFILL;
return;
}
cout<<"To buy a drink type: BUY"<<endl;
string input= take_input();
if(input=="BUY"){
currentState= State::CHOOSE_DRINK;
}
else{
cout<<"Invalid input !!!"<<endl;
}
}
//-------------------------------
int search_drink(string code){
string drink[]={"PEPS","MDEW","DPEP","COKE","GATO","DCOK","MINM","TROP"};
for(int i=0;i<8;i++){
if(drink[i]== (code)){
if( drinkStocks[code]>0)return 1;
else return 0;
}
}
return -1;
}
void select_drink(){
cout<<"To cancel type: CANCEL"<<endl;
cout<<"Enter the code for the drink:"<<endl;
string code = take_input();
if(code=="CANCEL"){
currentState=State::CANCEL;
return;
}
if(search_drink(code)==1){
selectedDrink=code;
currentState=State::COLLECT_COINS;
}
else if(search_drink(code)==0){
cout<<"The drink is out of stock. Please select another drink !!!"<<endl;
}
else{
cout<<"Invalid input !!!"<<endl;
}
}
//-------------------------------
void collect_coins(){
int amount;
cout<<"To cancel enter -1"<<endl;
cout<<"Enter the amount to pay: "<<endl;
cin>>amount;
if(amount==-1){
currentState=State::CANCEL;
return;
}
if(amount>=drinkCosts[selectedDrink]){
insertedAmount=amount;
currentState=State::OUT;
}
else{
cout<<"The amount entered is less than the required amount."<<endl;
cout<<"The required amount for drink "<<selectedDrink<<" is: "<<drinkCosts[selectedDrink]<<endl;
}
}
//-------------------------------
void give_drink(){
drinkStocks[selectedDrink]--;
totalCount--;
cout<<"Please collect the drink: "<<selectedDrink<<endl;
if(insertedAmount>drinkCosts[selectedDrink]){
cout<<"Collect the change of: "<<insertedAmount-drinkCosts[selectedDrink]<<endl;
}
cout<<"Transaction completed !!!"<<endl;
insertedAmount=0;
selectedDrink="";
currentState= State::IDLE;
}
//-------------------------------
void refill(){
for (auto& pair : drinkStocks) {
pair.second = 50;
}
totalCount=400;
cout << "Stocks refilled.\n";
currentState = State::IDLE;
}
//-------------------------------
void cancel(){ // cancelling the process
cout<< "Cancelling\n";
currentState= State::IDLE;
}
//-------------------------------
};
int main() {
VendingMachine vendingMachine;
vendingMachine.run();
return 0;
}