-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvendingMachine.cpp
More file actions
124 lines (105 loc) · 2.53 KB
/
vendingMachine.cpp
File metadata and controls
124 lines (105 loc) · 2.53 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
#include <iostream>
#include <string>
#include <map>
using namespace std;
class UniversityVendingMachine
{
public:
map<string, int> items;
map<string, string> itemCodes;
int moneyAvailable = 0;
UniversityVendingMachine()
{
itemCodes["A1"] = "cookie";
itemCodes["A2"] = "iPhone charger";
itemCodes["C5"] = "potato chips";
itemCodes["D4"] = "chewing gum";
items["cookie"] = 100;
items["iPhone charger"] = 1500;
items["potato chips"] = 200;
items["chewing gum"] = 150;
}
string getItemPrice(string x)
{
for(auto it = itemCodes.begin(); it != itemCodes.end(); it++)
{
if( it->first == x )
{
string ret = it->second + " - " + to_string(items[it->second]);
return ret;
}
}
return "Item is not available or the item number not valid";
}
void addMoney(int amount)
{
moneyAvailable += amount;
}
string buy(string item)
{
if(moneyAvailable < items[itemCodes[item]])
{
return "Please add enough \'sp\'";
}
else
{
moneyAvailable -= items[itemCodes[item]];
string ret = "Vend Successful!";
ret += "\n";
ret += "Balance: sp " ;
ret += to_string(moneyAvailable);
return ret;
}
}
};
void listMenu()
{
cout << "Menu" << endl;
cout << "1. getItemPrice" << endl;
cout << "2. addMoney" << endl;
cout << "3. buy" << endl;
cout << "Enter the operation number" << endl;
}
int main()
{
UniversityVendingMachine obj1;
listMenu();
int operation;
cin >> operation;
bool quit = false;
while (!quit)
{
if(operation == 1)
{
for(auto it = obj1.itemCodes.begin(); it != obj1.itemCodes.end(); it++)
{
cout << it->first << ": " << it->second << ". ";
}
cout << endl;
cout << "Enter ItemCode" << endl;
string ic;
cin >> ic;
cout << obj1.getItemPrice(ic) << endl;
listMenu();
cin >> operation;
}
else if(operation == 2)
{
cout << "Enter amount to be added: ";
int amt;
cin >> amt;
obj1.addMoney(amt);
listMenu();
cin >> operation;
}
else if (operation == 3)
{
cout << "Enter ItemCode to be bought: ";
string ic;
cin >> ic;
cout << obj1.buy(ic) << endl;
quit = true;
}
}
return 0;
}