-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvendingMachineLLD.cpp
More file actions
98 lines (89 loc) · 2.72 KB
/
vendingMachineLLD.cpp
File metadata and controls
98 lines (89 loc) · 2.72 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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <complex>
#include <array>
using namespace std;
class Items {
public:
string name;
int price;
int count;
};
void checkItemPrice(vector<Items>& inventory) {
cout << "Displaying items present in the inventory: \n";
for(auto item : inventory) {
if(item.count < 1)
continue;
cout << "Name: " << item.name << " - Price: " << item.price << " - Count: " << item.count << "\n";
}
cout << "\n";
}
int findInputPrice(vector<string>& denom, map<string, int>& denominations) {
int inputPrice = 0;
for(auto d: denom) {
inputPrice += denominations[d];
}
return inputPrice;
}
void buyItem(string itemName, vector<string>& denom, vector<Items>& inventory, map<string, int>& denominations) {
//check if item is available
int inputItemIndex = 0;
bool itemFound = false;
for(int itemIndex = 0;itemIndex < inventory.size();itemIndex++) {
if(itemName == inventory[itemIndex].name) {
itemFound = true;
if(inventory[itemIndex].count < 1) {
cout << itemName << " is not available\n\n";
return;
}
inputItemIndex = itemIndex;
}
}
if(!itemFound) {
cout << itemName << " not found\n";
return;
}
//find input price
int inputPrice = findInputPrice(denom, denominations);
//compare input price with actual item price
if(inputPrice < inventory[inputItemIndex].price) {
cout << "Transaction Failed\n";
cout << "Price of " << itemName << " is: " << inventory[inputItemIndex].price << "\n";
cout << "You have entered less price: " << inputPrice << "\n";
}
else {
inventory[inputItemIndex].count--;
cout << itemName << " purchased successfully\n";
cout << "Remaining balance: " << inputPrice - inventory[inputItemIndex].price << "\n";
}
cout << "\n";
}
int main() {
//fill the inventory
vector<Items> inventory;
inventory.push_back({"Coke",25,1});
inventory.push_back({"Soda",35,2});
inventory.push_back({"Pepsi",45,1});
map<string, int> denominations = {
{"c1", 1},
{"c2", 5},
{"c3", 10},
{"c4", 25}
};
//display items present in inventory
checkItemPrice(inventory);
//transaction
string itemName = "Fanta";
vector<string> denom = {"c2", "c3"};
buyItem(itemName, denom, inventory, denominations);
denom.clear();
denom.push_back("c4");
denom.push_back("c4");
itemName = "Soda";
buyItem(itemName, denom, inventory, denominations);
checkItemPrice(inventory);
return 0;
}