-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeList.cpp
More file actions
367 lines (330 loc) · 9.78 KB
/
RecipeList.cpp
File metadata and controls
367 lines (330 loc) · 9.78 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "IIKH.h"
#include "trim.h"
#include <deque>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Class for managing recipe objects in list form
RecipeList::RecipeList(string _filename) {
filename = _filename;
fileRead(filename);
}
void RecipeList::fileRead(string filename) {
ifstream myfile;
myfile.open(filename);
if (myfile.is_open()) {
while (getline(myfile, beforeParse)) {
// Skip to next line if there is "//"
if (beforeParse.find("//") != string::npos) {
continue;
}
parseLine(beforeParse);
Recipe recipe(afterParse);
// Put in RecipeDB
// Main
if (recipe.menuSize == "main") {
mainRecipeDB.push_back(recipe);
recipeDB.push_back(recipe);
}
// Side
else if (recipe.menuSize == "side") {
sideRecipeDB.push_back(recipe);
recipeDB.push_back(recipe);
}
else {
cout << "TYPE ERROR\n";
}
}
myfile.close();
}
else {
cout << "FILE OPEN ERROR";
}
}
void RecipeList::fileWrite(string filename, string newInput) {
ofstream myfile;
myfile.open(filename, ios::app); // To write on the last line of file
if (myfile.is_open()) {
myfile << "//" << newInput << endl;
myfile.close();
}
else {
cout << "FILE OPEN ERROR";
}
}
void RecipeList::parseLine(string beforeParse) {
stringstream ss(beforeParse); // Use beforeParse as a stream
string token;
while (getline(ss, token, '<')) { // Read token by parsing '<'
size_t endPos = token.find('>'); // Find the location of '>'
if (endPos != string::npos) { // Get string before '>'
token = token.substr(0, endPos);
token = trim(token);
afterParse[parseIndex++] = token;
}
}
parseIndex = 0;
}
// Change queue of ingredients, steps, hashtags to string
string RecipeList::putInString(Recipe recipe, string input) {
// Initialization
string ingredients;
string steps;
string hashtags;
// Ingredients
if (input == "ingredients") {
for (int i = 0; i < recipe.ingredientIndex; i++) {
// For non-first word
if (i > 0) {
ingredients += ", "; // Add ", "
}
ingredients += recipe.ingredients[i];
}
return ingredients;
}
// Steps
else if (input == "steps") {
for (int i = 0; i < recipe.stepIndex; i++) {
if (i > 0) {
steps += "\n"; // Add "\n"
}
steps += recipe.steps[i];
}
return steps;
}
// Hashtags
else if (input == "hashtags") {
for (int i = 0; i < recipe.hashtagIndex; i++) {
if (i > 0) {
hashtags += " "; // Add " "(blank)
}
hashtags += recipe.hashtags[i];
}
return hashtags;
}
else {
cout << "INPUT ERROR";
return "";
}
}
void RecipeList::printRecipes(const deque<Recipe>& recipes, const string& type) {
// Initialization
string ingredients;
string steps;
string hashtags;
for (int i = 0; i < recipes.size(); i++) {
ingredients = putInString(recipes[i], "ingredients");
steps = putInString(recipes[i], "steps");
hashtags = putInString(recipes[i], "hashtags");
cout << "\n[" << type << "]\n";
cout << "TYPE: " << recipes[i].menuSize << endl;
cout << "MENU: " << recipes[i].foodName << endl;
cout << "INGREDIENTS: " << ingredients << endl;
cout << "COOKING TIME: " << recipes[i].cookingTime << endl;
cout << "STEPS\n";
cout << steps << endl;
cout << "CALORIES: " << recipes[i].calorie << "kcal" << endl;
cout << "HASHTAGS: " << hashtags << endl;
}
}
// Print all recipe
void RecipeList::printAll() {
printRecipes(mainRecipeDB, "main");
printRecipes(sideRecipeDB, "side");
}
// Print main recipe
void RecipeList::printMainDB() {
printRecipes(mainRecipeDB, "main");
}
// Print side recipe
void RecipeList::printSideDB() {
printRecipes(sideRecipeDB, "side");
}
// Print name of recipe
void RecipeList::printRecipesTitle(const deque<Recipe>& recipes, const string& type) const {
if (type == "All") {
cout << "[All]MENU\n";
}
else {
if (type == "main") {
cout << "[MAIN]MENU\n";
}
else if (type == "side") {
cout << "[SIDE]MENU\n";
}
else {
cout << "Error\n";
return;
}
}
for (int i = 0; i < recipes.size(); i++) {
cout << recipes[i].foodName << endl;
}
}
// Print name of all recipe
void RecipeList::printAllTitles() {
printRecipesTitle(recipeDB, "All");
}
// Print name of all main recipe
void RecipeList::printMainTitles() {
printRecipesTitle(mainRecipeDB, "main");
}
// Print name of all side recipe
void RecipeList::printSideTitles() {
printRecipesTitle(sideRecipeDB, "side");
}
bool found = false; // Find or not
// For search by name
void RecipeList::searchName(deque<Recipe> recipeDB, string keyword) {
deque<Recipe> miniRecipeDB;
found = false; // Initialization
for (int i = 0; i < recipeDB.size(); i++) {
if (recipeDB[i].foodName == keyword) {
found = true; // We found it!
miniRecipeDB.push_back(recipeDB[i]);
}
}
if (found) {
printRecipes(miniRecipeDB, "SEARCH RESULT");
}
else {
int againSearch = 0;
cout << "NO RECIPE FOR " << keyword << endl << endl;
cout << "PRESS 1 TO FIND ANOTHER MENU, PRESS 0 TO QUIT: ";
cin >> againSearch;
if (againSearch == 1) {
string otherkeyword;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "MENU NAME TO SEARCH FOR: ";
getline(cin, otherkeyword);
searchName(recipeDB, otherkeyword);
}
else if (againSearch == 0) {
return;
}
else {
cout << "WRONG NUMBER CHOICE";
return;
}
}
}
// For search by the others
void RecipeList::search(deque<Recipe> recipeDB, string type, string keyword) {
deque<Recipe> miniRecipeDB;
// Initialization
found = false;
string ingredients;
string steps;
string hashtags;
for (int i = 0; i < recipeDB.size(); i++) {
ingredients = putInString(recipeDB[i], "ingredients");
steps = putInString(recipeDB[i], "steps");
hashtags = putInString(recipeDB[i], "hashtags");
// By ingredient
if (type == "INGREDIENT") {
for (int j = 0; j < recipeDB[i].ingredientIndex; j++) {
if (recipeDB[i].ingredients[j].find(keyword) != string::npos) {
found = true;
miniRecipeDB.push_back(recipeDB[i]);
break;
}
}
}
// By hashtag
else if (type == "HASHTAG") {
for (int j = 0; j < recipeDB[i].hashtagIndex; j++) {
if (recipeDB[i].hashtags[j] == keyword) {
found = true;
miniRecipeDB.push_back(recipeDB[i]);
break;
}
}
}
// By calorie
else if (type == "CALORIE") {
int recipeCal = stoi(recipeDB[i].calorie);
int keywordCal = stoi(keyword);
if (recipeCal <= keywordCal) {
found = true;
miniRecipeDB.push_back(recipeDB[i]);
continue;
}
}
else {
cout << "TYPE ERROR";
}
}
if (found) {
printRecipes(miniRecipeDB, "SEARCH RESULT");
int i = 0;
cout << "\nPRESS 1 TO FIND ADDITIONAL " << type << ", PRESS 0 TO QUIT: ";
cin >> i;
if (i == 1) {
string otherkeyword;
cout << "ADDITIONAL " << type << ": ";
cin >> otherkeyword;
search(miniRecipeDB, type, otherkeyword);
}
else if (i == 0) {
return;
}
else {
cout << "WRONG NUMBER CHOICE";
return;
}
}
else {
int i = 0;
cout << "NO RECIPE FOR " << keyword << endl << endl;
cout << "PRESS 1 TO FIND ANOTHER " << type << ", PRESS 0 TO QUIT: ";
cin >> i;
if (i == 1) {
string otherkeyword;
cout << type << " TO SEARCH FOR: ";
cin >> otherkeyword;
search(recipeDB, type, otherkeyword);
}
else if (i == 0) {
return;
}
else {
cout << "WRONG NUMBER CHOICE";
return;
}
}
}
// User add a new recipe
void RecipeList::add(string newRecipe, string filename) {
fileWrite(filename, newRecipe);
stringstream ss(newRecipe);
string token;
string afterAddParse[7];
while (getline(ss, token, '<')) {
size_t endPos = token.find('>');
if (endPos != string::npos) {
token = token.substr(0, endPos);
token = trim(token);
afterAddParse[parseIndex] = token;
cout << afterAddParse[parseIndex] << endl;
parseIndex++;
}
}
parseIndex = 0;
Recipe Nrecipe(afterAddParse);
cout << Nrecipe.menuSize << endl;
// Main
if (Nrecipe.menuSize == "main") {
mainRecipeDB.push_back(Nrecipe);
recipeDB.push_back(Nrecipe);
}
// Side
else if (Nrecipe.menuSize == "side") {
sideRecipeDB.push_back(Nrecipe);
recipeDB.push_back(Nrecipe);
}
else {
cout << "TYPE ERROR\n";
}
}