-
Notifications
You must be signed in to change notification settings - Fork 1
/
CocktailMachine.cpp
276 lines (249 loc) · 7.46 KB
/
CocktailMachine.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "CocktailMachine.h"
#include <fstream>
bool CocktailMachine::check_ingredients(string name)
{
for (unsigned int i = 1; i <= all_ingredients.size(); i++)
{
if (name == all_ingredients[i - 1]->getName())
return false;
}
return true;
}
void CocktailMachine::check_input(unsigned int &choice)
{
cout << "Entry > ";
cin >> choice;
while (!cin) // if choice is no number
{
try
{
throw "- Wrong input! Enter a number -";
}
catch (const char* str)
{
cout << str << endl;
}
cin.clear();
cin.ignore();
cout << "Entry > ";
cin >> choice;
}
}
void CocktailMachine::add_cocktail()
{
vector<Ingredient> ingredients;
string cocktailName = "";
string ingredientName = "";
unsigned int amount = 0;
bool check = true;
unsigned int choice = 1;
cout << "== CocktailMix | Add Cocktail ==" << endl;
string tmp = "";
cout << "Cocktail: ";
cin.ignore();
getline(cin, tmp);
if (tmp != "")
cocktailName = tmp;
while (check)
{
cout << "0 - Back" << endl;
cout << "1 - Add ingredient" << endl;
check_input(choice);
if (choice == 0)
check = false;
if (choice == 1)
{
cout << "Ingredient: ";
cin.ignore();
getline(cin, tmp);
if (tmp != "")
ingredientName = tmp;
cout << "Amount: ";
getline(cin, tmp);
if (tmp != "")
amount = atoi(tmp.c_str());
ingredients.push_back(Ingredient(ingredientName, amount));
if (check_ingredients(ingredientName)) // check of duplicate (ingredientName)
all_ingredients.push_back(new Ingredient(ingredientName)); // import all ingredients once
}
if (choice >= 2)
cout << "- Wrong input -" << endl;
}
cocktails.push_back(new Cocktail(cocktailName, ingredients));
system("clear");
}
void CocktailMachine::edit_cocktail(unsigned int cocktail_number)
{
vector<Ingredient> ingredients;
string cocktailName = "";
string ingredientName = "";
unsigned int amount = 0;
bool check = true;
unsigned int choice = 1;
cout << "== CocktailMix | Edit Cocktail ==" << endl;
string tmp = "";
cout << "Cocktail Name: ";
cin.ignore();
getline(cin, tmp);
if (tmp != "")
{
cocktailName = tmp;
cocktails[cocktail_number]->setName(cocktailName);
}
while (check)
{
cout << "0 - Back" << endl;
cout << "1 - Add ingredient" << endl;
check_input(choice);
if (choice == 0)
check = false;
if (choice == 1)
{
cout << "Ingredient: ";
cin.ignore();
getline(cin, tmp);
if (tmp != "")
ingredientName = tmp;
cout << "Amount: ";
getline(cin, tmp);
if (tmp != "")
amount = atoi(tmp.c_str());
ingredients.push_back(Ingredient(ingredientName, amount));
cocktails[cocktail_number]->setIngredients(ingredients);
if (check_ingredients(ingredientName)) // check of duplicate (ingredientName)
all_ingredients.push_back(new Ingredient(ingredientName)); // import all ingredients once
}
if (choice >= 2)
cout << "- Wrong input -" << endl;
}
system("clear");
}
void CocktailMachine::delete_cocktail(unsigned int cocktail_number)
{
for (int i = cocktail_number; i < cocktails.size() - 1; i++)
{
cocktails[i] = cocktails[i + 1];
}
cocktails.pop_back();
}
vector<Dispenser*> CocktailMachine::getDispensers() const
{
return this->dispensers;
}
vector<Cocktail*> CocktailMachine::getCocktails() const
{
return this->cocktails;
}
vector<Ingredient*> CocktailMachine::getIngredients() const
{
return this->all_ingredients;
}
bool CocktailMachine::check_cocktail(unsigned int cocktail_number)
{
unsigned int counter_ingredients = 0;
for (int i = 0; i < cocktails[cocktail_number]->getIngredients().size(); i++) // count ingredients in cocktail
counter_ingredients++;
unsigned int counter = 0;
for (int i = 0; i < dispensers.size(); i++)
{
for (int k = 0; k < cocktails[cocktail_number]->getIngredients().size(); k++)
{
if (dispensers[i]->getIngredient()->getName() == cocktails[cocktail_number]->getIngredients()[k].getName()) // compare dispenser ingredients with cocktail ingredients
counter++;
if (counter_ingredients == counter) // dispenser has all ingredients
return true;
}
}
return false;
}
CocktailMachine::CocktailMachine()
{
all_ingredients.push_back(new Ingredient("Free")); // first field of all_ingredients is "Free"
ifstream ingredientsRead;
ingredientsRead.open("cocktails.txt", ios::in);
if (ingredientsRead)
{
string cocktailName = "";
while (getline(ingredientsRead, cocktailName, ':')) // import cocktail name
{
string ingredientName = "";
string ingredientAmount = "";
vector<Ingredient> ingredients;
while (getline(ingredientsRead, ingredientName, ';')) // import ingredient name
{
getline(ingredientsRead, ingredientAmount, ';'); // import ingredient amount
unsigned int amount = stoi(ingredientAmount);
ingredients.push_back(Ingredient(ingredientName, amount));
if (check_ingredients(ingredientName)) // check of duplicate (ingredientName)
all_ingredients.push_back(new Ingredient(ingredientName)); // import all ingredients once
if (ingredientsRead.peek() == '\n')
{
ingredientsRead.get(); // Next line in data
break;
}
}
cocktails.push_back(new Cocktail(cocktailName, ingredients));
}
ingredientsRead.close();
}
else
{
cout << "Reading (cocktails.txt) is not possible!" << endl;
cout << endl;
}
ifstream dispensersRead;
dispensersRead.open("dispenser.txt", ios::in);
if (dispensersRead)
{
string number;
while (getline(dispensersRead, number, ':')) // import number of dispenser
{
unsigned int nr = stoi(number);
string name;
getline(dispensersRead, name, '\n'); // import dispenser ingredient
if (name.size() > 0)
dispensers.push_back(new Dispenser(nr, new Ingredient(name)));
else
dispensers.push_back(new Dispenser(nr, new Ingredient("Free")));
}
}
else
{
cout << "Reading (dispenser.txt) is not possible!" << endl;
cout << endl;
}
dispensersRead.close();
}
CocktailMachine::~CocktailMachine()
{
ofstream ingredientsWrite;
ingredientsWrite.open("cocktails.txt", ios::out);
for (unsigned int i = 0; i < cocktails.size(); i++)
{
// export cocktail name
ingredientsWrite << cocktails[i]->getName() << ":"; // inserts character (:) into the stream
for (unsigned int k = 0; k < cocktails[i]->getIngredients().size(); k++)
{
// export ingredients name
ingredientsWrite << cocktails[i]->getIngredients()[k].getName() << ";"; // inserts character (;) into the stream
ingredientsWrite << cocktails[i]->getIngredients()[k].getAmount() << ";"; //Req01
}
ingredientsWrite.put('\n'); // inserts character (interlace) into the stream
}
ingredientsWrite.close();
ofstream dispensersWrite;
dispensersWrite.open("dispenser.txt", ios::out);
for (unsigned int i = 0; i < dispensers.size(); i++)
{
dispensersWrite << dispensers[i]->getNumber() << ":"; // export dispenser number
dispensersWrite << dispensers[i]->getIngredient()->getName() << "\n"; // export dispenser ingredient
}
dispensersWrite.close();
// delete pointer (cocktails, dispensers and all_ingredients)
for (unsigned int i = 0; i < dispensers.size(); i++)
delete dispensers[i];
for (unsigned int i = 0; i < cocktails.size(); i++)
delete cocktails[i];
for (unsigned int i = 0; i < all_ingredients.size(); i++)
delete all_ingredients[i];
}