-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvendingMachine.cpp
More file actions
113 lines (104 loc) · 3.35 KB
/
vendingMachine.cpp
File metadata and controls
113 lines (104 loc) · 3.35 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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
struct Drink{
std::string name;
double price;
int quantity;
Drink(){
name = "";
price = 0;
quantity = 0;
}
Drink(std::string n, double p, int q){
name = n;
price = p;
quantity = q;
}
};
Drink drinks[6] = {Drink("Coke", 1.00, 5), Drink("Sprite", 1.00, 5), Drink("Water", 1.25, 5), Drink("Gatorade", 1.00, 5), Drink("Ginger Ale", 1.25, 5), Drink("Iced Tea", 1.50, 5)};
int choice;
double payment;
double total;
double change;
bool isVending = true;
void getDrink(int total, Drink drinks[], int choice){
if (total < drinks[choice].price){
std::cout << "Insufficient amount, please try again.\n";
}
else if (drinks[choice].quantity == 0){
std::cout << "There is no " + drinks[choice].name + " left.\n";
}
else{
drinks[choice].quantity--;
total -= drinks[choice].price;
if (total > 0){
std::cout << "Your change is: " << total << ".\n";
}
std::cout << "Enjoy your drink!\n";
}
}
int main(){
while (isVending){
std::cout << "Enter your payment amounts in value (0.01, 0.05, etc) up to 5.00. Enter -1 to exit.\n";
std::vector<double> vec = {0.01, 0.05, 0.10, 0.25, 1.00, 5.00};
while (true){
std::cin >> payment;
if (payment <= -1.0){
break;
}
else if (payment > 5.0){
std::cout << "Cannot accept anything greater than 5 dollars.\n";
}
else if (std::find(vec.begin(), vec.end(), payment) == vec.end()){
std::cout << "Not a valid form of payment.\n";
}
else{
total += payment;
std::cout << "Amount: " + std::to_string(total) + ".\n";
}
}
std::cout << "Hello, what drink would you like today?\n";
std::cout << "0 - Coke, $" << drinks[0].price << ".\n";
std::cout << "1 - Sprite, $" << drinks[1].price << ".\n";
std::cout << "2 - Water, $" << drinks[2].price << ".\n";
std::cout << "3 - Gatorade, $" << drinks[3].price << ".\n";
std::cout << "4 - Ginger Ale, $" << drinks[4].price << ".\n";
std::cout << "5 - Iced Tea, $" << drinks[5].price << ".\n";
std::cout << "6 - Leave\n";
std::cin >> choice;
switch(choice){
case 0:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 1:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 2:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 3:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 4:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 5:
getDrink(total, drinks, choice);
total = 0.0;
break;
case 6:
std::cout << "Have a nice day!";
isVending = false;
break;
default:
std::cout << "Not a valid operation, please try again";
}
}
}