Repository navigation
Expand file tree
/
Copy pathveningmachine.cpp
More file actions
241 lines (210 loc) · 6.29 KB
/
Copy pathveningmachine.cpp
File metadata and controls
241 lines (210 loc) · 6.29 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
#include <iostream>
#include <string>
using namespace std;
//class drink is parent class
class Drink {
protected:
string drink_name;
double price;
string type_of_drink;
public:
Drink(string name, double p) : drink_name(name), price(p), type_of_drink("") {} //contstructor with parameters
Drink(): drink_name(""), price(0.0), type_of_drink("") {} //these are default constructor
double GetPrice() { return price;} //getfunctions is created to access protected variables
string GetType() { return type_of_drink;}
string GetName() { return drink_name;}
};
class Water : public Drink //Derived Class water , it inherits from Drink class
{
public: //constructor
Water():Drink("Water", 1.00) {}
void options() // Display water options to user
{
cout<<"Choose the type of water you need:"<< endl;
cout<<"1- Sparkling Water"<< endl;
cout<<"2- Still Water"<<endl;
}
void setTypeOfDrink(int choice) // setting type on user choice
{
if (choice == 1)
{
type_of_drink = "Sparkling Water";
}
else
{
type_of_drink = "Still Water";
}
}
};
class Cola : public Drink { //Derived class cola
public :
Cola() : Drink("Cola", 1.80) {}
void options()
{
cout<<"Please choose the type of Cola"<<endl;
cout<<"1- Regular Cola"<<endl;
cout<<"2- Sugar-free Cola"<<endl;
}
void setTypeOfDrink(int choice)
{
if (choice == 1)
{
type_of_drink = "Regular Cola";
}
else
{
type_of_drink = "Sugar-free Cola";
}
}
};
class IcedTea: public Drink{ //Derived Class IcedTea
public:
IcedTea() : Drink("Iced Tea", 2.20) {}
void options()
{
cout<<"Please choose the flavor of Iced Tea"<<endl;
cout<<"1- Lemon Iced Tea"<<endl;
cout<<"2- Peach Iced Tea"<<endl;
}
void setTypeOfDrink(int choice)
{
if (choice == 1)
{
type_of_drink = "Lemon Iced Tea";
}
else
{
type_of_drink = "Peach Iced Tea";
}
}
};
class DrinksVendingMachine { //class for operations perfomred in vending machine
public:
double money; //money available in the vending machine
bool Ordersplaced; //flag to check if orders have been placed
string orders[3]; //array to store orders
double prices[3]; //array to store prices of orders
double totalspent;
DrinksVendingMachine() {
money = 5.00; // initial money in the vending machine
Ordersplaced = false;
totalspent=0.0;
}
void showavailableDrinks(){ //Display list of drinks available
cout<<endl;
cout<<"----Available Drinks----"<<endl;
cout<<"1- Water price= 1.00 Euro"<<endl;
cout<<"2- Cola price= 1.80 Euro"<<endl;
cout<<"3- Iced Tea price= 2.20 Euro"<<endl;
}
void showMenu(){
cout<<endl;
cout<<"----Main Menu----"<<endl;
cout<<"1- Show available Drinks"<<endl;
cout<<"2- Place Order"<<endl;
cout<<"3- Show Total and change"<<endl;
cout<<"4- Exit"<<endl;
cout<<"Your Choice:";
}
void placeOrder(){ //Main Menu options
if (Ordersplaced){
cout<<"You have already placed an order. Cannot place another one."<<endl;
return;
}
int DrinkChoice, typeChoice;
for(int i=0; i<3; i++){
cout<<endl;
cout<<"Placing order "<< (i+1) << ":" <<endl;
showavailableDrinks();
cout<<"Select your drink (1-3): ";
cin>> DrinkChoice;
double currentPrice = 0.0; // price of the selected drink
string currentType = ""; // type of the selected drink
if (DrinkChoice==1){
Water w;
w.options();
cout<<"Your Choice: ";
cin>> typeChoice;
w.setTypeOfDrink(typeChoice);
currentPrice = w.GetPrice();
currentType = w.GetType();
}
else if (DrinkChoice==2){
Cola c;
c.options();
cout<<"Your Choice: ";
cin>> typeChoice;
c.setTypeOfDrink(typeChoice);
currentPrice = c.GetPrice();
currentType = c.GetType();
}
else if (DrinkChoice==3){
IcedTea it;
it.options();
cout<<"Your Choice: ";
cin>> typeChoice;
it.setTypeOfDrink(typeChoice);
currentPrice = it.GetPrice();
currentType = it.GetType();
}
else{
cout<<"Invalid choice, try again."<<endl;
i--;
continue;
}
if (money >= currentPrice){ // check if the money limit has crossed or not
money -= currentPrice;
totalspent += currentPrice;
orders[i] = currentType;
prices[i] = currentPrice;
cout<<"You have ordered: " << currentType << " for " << currentPrice << " Euro."<<endl;
}
else{
cout<<"Insufficient funds for this drink. Order not placed."<<endl;
orders[i] = "no enough money! , skipping the order....";
prices[i] = 0.0;
}
}
Ordersplaced = true;// order completed flag
}
void showTotalAndChange(){ // this is to display order summary
if (!Ordersplaced){
cout<<"No orders placed yet!"<<endl;
return;
}
cout << "----Order Summary----" << endl;
for (int i = 0; i < 3; i++) {
cout << "Order " << (i + 1) << ": " << orders[i] << " - " << prices[i] << " Euro" << endl;
}
cout << "Total Spent: " << totalspent << " Euro" << endl;
cout << "Remaining Money: " << money << " Euro" << endl;
}
void run(){ //Main function to run the vending machine
int choice;
cout<<"Welcome this your Drinks Machine!, currently you have"<< money <<" Euro"<<endl;
do{ //main menu loop
showMenu();
cin>> choice;
if (choice ==1){
showavailableDrinks();
}
else if (choice ==2){
placeOrder();
}
else if (choice ==3){
showTotalAndChange();
}
else if (choice ==4){
cout<<"Exiting the program. Thank you!"<<endl;
}
else{
cout<<"Invalid choice, please try again."<<endl;
}
} while (choice !=4);
}
};
int main() { //Main - program start
DrinksVendingMachine vm;// creating vending machine object
vm.run();
return 0;
}