-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
301 lines (245 loc) · 11.6 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// Created on 7/14/24.
//
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <sstream>
#include "HashMap/HashMap.h"
#include "IOManager/IOManager.h"
#include "Recipe/Recipe.h"
using namespace std;
/// Welcome message (Printed 1 time)
void printWelcomeMessage() {
cout << right << setfill('-') << "+" << setw(90) << "+" << endl;
cout << "| Welcome to Food and Recipes Indexing Database Generating Environment (FRIDGE) |" << endl;
cout << right << setfill('-') << "+" << setw(90) << "+" << endl;
cout << "Please enter a minimum of 4 ingredients (separated by commas): " << endl;
}
/// Main menu (Printed n times, n = number of inputs not equal to 4)
void printMainMenu(bool implementation) {
cout << "Main menu" << endl;
cout << "1. Suggest a recipe" << endl;
cout << "2. Change ingredients on hand" << endl;
if (implementation)
cout << "3. Toggle DSA [Currently AVL Tree]" << endl;
else
cout << "3. Toggle DSA [Currently Linked List]" << endl;
cout << "4. Exit" << endl;
cout << endl;
cout << "Please make a selection (1-4): ";
}
/// Print Recipe Title Card
void printRecipeCard(AVLTree::TreeNode* tNode, LinkedList::LLNode* llNode, float percentage) {
// Card elements initialization
string recipeName, linkName;
string recipeIngredients = "Ingredients: ";
string link = " https://www.food.com/recipe/";
stringstream perSim;
perSim << "Percent similarity: " << fixed << setprecision(0) << percentage * 100 << "% ";
vector<string> ingredients;
int recipeId;
double topBarSize,bottomBarSize;
// Check which node to take in data from (linked list node or tree node)
if(tNode == nullptr && llNode != nullptr) {
recipeName = llNode -> recipeName;
ingredients = llNode -> ingredients;
recipeId = llNode -> recipeID;
} else if(llNode == nullptr && tNode != nullptr) {
recipeName = tNode -> recipeName;
ingredients = tNode -> ingredients;
recipeId = tNode -> recipeID;
}
// Ensure nodes are truly existent
if(tNode != nullptr || llNode != nullptr) {
// Write down ingredients into a single string for printing
for (int i = 0; i < ingredients.size(); i++) {
recipeIngredients += ingredients[i];
if (i < ingredients.size() - 1) { recipeIngredients += ", "; }
else { recipeIngredients += " "; }
}
// Link identification
for (char c: recipeName) {
if (c != ' ') { linkName += c; }
else { linkName += '-'; }
}
link += linkName + "-" + to_string(recipeId);
// Dynamic size allocation based on recipe size
double topLeftBarSize = max(recipeName.size(), link.size()) * 2.2;
double topRightBarSize = recipeIngredients.size() * 2.2;
// Final product printing
cout << "+";
cout << setw((topLeftBarSize / 2) + 1) << setfill('-');
cout << "+";
cout << setw((topRightBarSize / 2) + 1) << setfill('-');
cout << "+" << endl;
cout << "| ";
cout << left << setw((topLeftBarSize / 2) - 1) << setfill(' ') << recipeName;
cout << "|";
cout << right << setw(topRightBarSize / 2) << setfill(' ');
cout << recipeIngredients;
cout << "|" << endl;
cout << "|";
cout << left << setw(topLeftBarSize / 2) << setfill(' ') << " ";
cout << "|";
cout << right << setw(topRightBarSize / 2) << setfill(' ') << " ";
cout << "|" << endl;
cout << "|";
cout << left << setw(topLeftBarSize / 2) << setfill(' ') << link;
cout << "|";
cout << right << setw(topRightBarSize / 2) << setfill(' ') << perSim.str();
cout << "|" << endl;
cout << "+";
cout << right << setw((topLeftBarSize / 2) + 1) << setfill('-');
cout << "+";
cout << setw((topRightBarSize / 2) + 1) << setfill('-');
cout << "+" << endl;
}
}
/// Parses the ingredient string into an array for later use
vector<string> parseIngredients(string unparsedIngredients) {
/* Explanation: Run parsing operation until unparsed string is of size less than 0. Start by parsing string from
* start to comma, transforming the ingredient so that all characters are in lowercase, and then pushing back the
* ingredient to parsedIngredients. Lastly, the current ingredient is removed from the unparsedIngredients and the
* process is repeated. Conclude parsing by returning parsedIngredients.
* */
vector<string> parsedIngredients;
string ingredient;
while(!unparsedIngredients.empty()) {
ingredient = unparsedIngredients.substr(0, unparsedIngredients.find(","));
transform(ingredient.begin(), ingredient.end(), ingredient.begin(), ::tolower);
parsedIngredients.push_back(ingredient);
unparsedIngredients = unparsedIngredients.substr(unparsedIngredients.find(",") + 2,
unparsedIngredients.size() - ingredient.size());
}
sort(parsedIngredients.begin(), parsedIngredients.end());
return parsedIngredients;
}
int main() {
cout << "Please give program time to load database" << endl;
cout << "\n\n\n" << endl;
// Fill a vector with recipes
vector<Recipe*> recipes = IOManager::ReadIngredients("immaculate_ingredients.csv");
/// Hash Map initialization
// Initializes AVLTree version and LinkedList
HashMap hashMap;
// Fill HashMap
for (int i = 0; i < recipes.size(); i++) {
hashMap.insert(recipes[i]->name, recipes[i]->recipe_id, recipes[i]->ingredients, recipes[i]->ingredient_num);
}
// UI Variables
vector<string> parsedIngredients;
string userInput, unparsedIngredients; // false = linked list | true = AVL tree
// User interface printing
// Introduction and ingredient prompting
printWelcomeMessage();
getline(cin, unparsedIngredients);
parsedIngredients = parseIngredients(unparsedIngredients);
// Ensure user provides at least 4 ingredients or at most 18 ingredients
while(parsedIngredients.size() < 4 || parsedIngredients.size() > 18) {
cout << "Please ensure you are providing at LEAST 4 ingredients or at MOST 18 ingredients: " << endl;
getline(cin, unparsedIngredients);
parsedIngredients = parseIngredients(unparsedIngredients);
}
cout << endl;
// Process user input until request to leave
while(userInput != "4") {
printMainMenu(hashMap.getImplementation());
getline(cin, userInput);
cout << endl;
// Conditionals
if(userInput == "1") { // If usrInput = 1 -> Perform search operation on currently active DSA
bool closeSearch = false;
auto find = hashMap.search(parsedIngredients);
if (get<0>(find) == nullptr && get<1>(find) == nullptr) {
search:
cout << endl;
cout << "Executing closest match search because no exact match found" << endl;
auto closestMatches = hashMap.bestMatchSearch(parsedIngredients);
if (hashMap.getImplementation()) { //Avl Tree Best Search
cout << "Closest matches search executed in " << closestMatches.first.second.count() << " seconds." << endl;
AVLClosest:
if (!closestMatches.first.first.empty()) {
AVLTree::TreeNode* AVLmatch = closestMatches.first.first.top().second;
float percentage = closestMatches.first.first.top().first;
closestMatches.first.first.pop();
// call stefano's print closest match function
printRecipeCard(AVLmatch, nullptr, percentage);
cout << endl;
cout << "Would you like another recipe suggested? (y/n)" << endl;
getline(cin, userInput);
cout << endl;
if(userInput == "n") { cout << endl; continue; }
else if(userInput == "y" && !closestMatches.first.first.empty()) { goto AVLClosest; }
}
}
else { //Linked List Best Search
cout << "Closest matches search executed in " << closestMatches.first.second.count() << " seconds." << endl;
LLClosest:
if (!closestMatches.second.first.empty()) {
LinkedList::LLNode *LLmatch = closestMatches.second.first.top().second;
float percentage = closestMatches.second.first.top().first;
closestMatches.second.first.pop();
printRecipeCard(nullptr, LLmatch, percentage);
cout << endl;
cout << "Would you like another recipe suggested? (y/n)" << endl;
getline(cin, userInput);
cout << endl;
if(userInput == "n") { cout << endl; continue; }
else if (userInput == "y" && !closestMatches.second.first.empty()) { goto LLClosest; }
}
}
closeSearch = true;
}
else {
cout << fixed;
cout << "Here's a recipe you may like:" << endl;
if (get<0>(find) != nullptr) {
printRecipeCard(nullptr, get<0>(find), 1.0f);
cout << "Search executed in " << get<2>(find).count() << " seconds." << endl;
cout << endl;
}
else {
printRecipeCard(get<1>(find), nullptr, 1.0f);
cout << "Search executed in " << get<2>(find).count() << " seconds." << endl;
cout << endl;
}
}
if(!closeSearch){
cout << "Would you like another recipe suggested? (y/n)" << endl;
getline(cin, userInput);
if(userInput == "n") { cout << endl; continue; }
else if(userInput == "y" && !closeSearch) { goto search; }
} else {
cout << "No more closest matches" << endl;
cout << endl;
}
} else if(userInput == "2") { // If usrInput = 2 -> Perform ingredient change operation
changeIngredients: // Label - used to return to a point in the code without loops
cout << endl;
cout << "Please enter a minimum of 4 ingredients (separated by commas): " << endl;
getline(cin, unparsedIngredients);
parsedIngredients = parseIngredients(unparsedIngredients);
if(parsedIngredients.size() < 4) { goto changeIngredients; }
cout << endl;
cout << "Do you confirm these are the ingredients you want? (y/n)" << endl;
getline(cin, userInput);
cout << endl;
if(userInput == "y") { continue; }
else if(userInput == "n") { goto changeIngredients; }
} else if(userInput == "3") { // If usrInput = 3 -> Alternate between implementation
hashMap.changeImplementation();
cout << endl;
} else {
if(userInput != "4") {
cout << endl;
cout << "Please provide a proper answer" << endl;
cout << endl;
}
}
}
cout << endl;
cout << "Thank you for using FRIDGE! Please come back again!" << endl;
return 0;
}