-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVending_Machine.cpp
More file actions
245 lines (224 loc) · 7.41 KB
/
Vending_Machine.cpp
File metadata and controls
245 lines (224 loc) · 7.41 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
#include<bits/stdc++.h>
using namespace std;
enum class ItemType{COKE, PEPSI, JUICE, WATER};
enum class CoinType{ONE=1, TWO=2, FIVE=5, TEN=10};
int cointoValue(CoinType c){
switch(c){
case CoinType::ONE: return 1;
case CoinType::TWO: return 2;
case CoinType::FIVE: return 5;
case CoinType::TEN: return 10;
default: return 0;
}
}
class Item{
ItemType type;
double price;
public:
Item(double p, ItemType t): price(p), type(t) {}
ItemType getType() const { return type; }
double getPrice() const { return price; }
void setPrice(double p){price = p;}
void setType(ItemType t){ type = t; }
};
class ItemShelf{
vector<Item> items;
ItemType type;
bool isSoldOut;
int capacity;
public:
ItemShelf(ItemType type,double p): capacity(10), isSoldOut(false), type(type) {
for(int i=0;i<capacity;i++) items.push_back(Item(p,type));
}
void addItem(const Item &item){
if(items.size() < capacity && item.getType() == type){
items.push_back(item);
isSoldOut = false;
}
if(items.size() == capacity){ isSoldOut = true; }
}
bool dispenseItem(){
if(!items.empty()){
items.pop_back();
if(items.empty()) isSoldOut = true;
return true;
}
return false;
}
bool isFull() const { return items.size() == capacity; }
int getItemCount() const { return items.size(); }
bool soldOut() const { return isSoldOut; }
ItemType getType() const { return type; }
};
class CashInventory{
map<CoinType,int> cashMap;
public:
CashInventory(){
cashMap[CoinType::ONE] = 10;
cashMap[CoinType::TWO] = 10;
cashMap[CoinType::FIVE] = 10;
cashMap[CoinType::TEN] = 10;
}
void addCash(CoinType c,int count){
cashMap[c] += count;
}
bool hasSufficientChange(int amount){
int total = 0;
for(auto &p : cashMap){
total += cointoValue(p.first) * p.second;
}
return total >= amount;
}
map<CoinType,int> dispenseChange(int amount){
if(!hasSufficientChange(amount)) return {};
map<CoinType,int> toDispense;
int rem = amount;
vector<CoinType> denominations = {CoinType::TEN, CoinType::FIVE, CoinType::TWO, CoinType::ONE};
int i=0;
while(rem > 0 && i < (int)denominations.size()){
CoinType denom = denominations[i];
int val = cointoValue(denom);
int available = cashMap[denom];
int needed = rem / val;
int toGive = min(available, needed);
if(toGive > 0){
toDispense[denom] = toGive;
rem -= toGive * val;
cashMap[denom] -= toGive;
}
i++;
}
return toDispense;
}
};
class Inventory{
vector<ItemShelf> shelf;
int capacity;
public:
Inventory(int cokeshelf,int pepsishelf,int juiceshelf,int watershelf){
capacity = cokeshelf + pepsishelf + juiceshelf + watershelf;
for(int i=0;i<cokeshelf;i++) shelf.push_back(ItemShelf(ItemType::COKE,40.0));
for(int i=0;i<pepsishelf;i++) shelf.push_back(ItemShelf(ItemType::PEPSI,35.0));
for(int i=0;i<juiceshelf;i++) shelf.push_back(ItemShelf(ItemType::JUICE,30.0));
for(int i=0;i<watershelf;i++) shelf.push_back(ItemShelf(ItemType::WATER,20.0));
}
bool addItem(const Item &item){
for(auto &s : shelf){
if(s.getType() == item.getType() && !s.isFull()){
s.addItem(item);
return true;
}
}
return false;
}
bool dispenseItem(ItemType type){
for(auto &s : shelf){
if(s.getType() == type && !s.soldOut()){
return s.dispenseItem();
}
}
return false;
}
bool hasItem(ItemType type){
for(auto &s : shelf){
if(s.getType() == type && !s.soldOut()) return true;
}
return false;
}
void availableItems(){
cout<<"Available items:\n";
vector<int> counts(4,0);
for(auto &s : shelf){
counts[static_cast<int>(s.getType())] += s.getItemCount();
}
cout<<"COKE : "<<counts[0]<<" | Cost : 40.0\n";
cout<<"PEPSI: "<<counts[1]<<" | Cost : 35.0\n";
cout<<"JUICE: "<<counts[2]<<" | Cost : 30.0\n";
cout<<"WATER: "<<counts[3]<<" | Cost : 20.0\n";
}
};
class VendingMachine{
Inventory inventory;
CashInventory cashInventory;
int currentBalance;
public:
VendingMachine(int cokeshelf,int pepsishelf,int juiceshelf,int watershelf)
: inventory(cokeshelf,pepsishelf,juiceshelf,watershelf), currentBalance(0) {}
void insertCoin(CoinType c){
cashInventory.addCash(c,1);
currentBalance += cointoValue(c);
cout<<"Inserted coin: "<<cointoValue(c)<<", Current balance: "<<currentBalance<<"\n";
}
bool selectItem(ItemType type){
if(!inventory.hasItem(type)){
cout<<"Selected item is sold out\n";
return false;
}
double price = 0.0;
switch(type){
case ItemType::COKE: price = 40.0; break;
case ItemType::PEPSI: price = 35.0; break;
case ItemType::JUICE: price = 30.0; break;
case ItemType::WATER: price = 20.0; break;
default: price = 0.0; break;
}
if(currentBalance < price){
cout<<"Insufficient balance. Please insert more coins.\n";
return false;
}
// dispense item
if(inventory.dispenseItem(type)){
currentBalance -= price;
cout<<"Dispensed item of type "<<static_cast<int>(type)<<"\n";
// dispense change if any
if(currentBalance > 0){
auto change = cashInventory.dispenseChange(currentBalance);
if(!change.empty()){
cout<<"Dispensed change:\n";
for(auto &p : change){
cout<<cointoValue(p.first)<<" x "<<p.second<<"\n";
}
currentBalance = 0;
} else {
cout<<"Unable to dispense full change. Please contact support.\n";
}
}
return true;
}
return false;
}
void cancelTransaction(){
if(currentBalance > 0){
auto change = cashInventory.dispenseChange(currentBalance);
if(!change.empty()){
cout<<"Transaction cancelled. Dispensed refund:\n";
for(auto &p : change){
cout<<cointoValue(p.first)<<" x "<<p.second<<"\n";
}
currentBalance = 0;
} else {
cout<<"Unable to dispense full refund. Please contact support.\n";
}
} else {
cout<<"No balance to refund.\n";
}
}
void showAvailableItems(){
inventory.availableItems();
}
int getCurrentBalance() const { return currentBalance; }
};
int main(){
VendingMachine vm(5,5,5,5);
vm.insertCoin(CoinType::ONE);
vm.insertCoin(CoinType::FIVE);
vm.insertCoin(CoinType::FIVE);
vm.showAvailableItems();
vm.selectItem(ItemType::COKE);
vm.cancelTransaction();
vm.insertCoin(CoinType::TEN);
vm.insertCoin(CoinType::TEN);
vm.selectItem(ItemType::WATER);
vm.showAvailableItems();
return 0;
}