-
Notifications
You must be signed in to change notification settings - Fork 0
/
chooseRecipes.cpp
166 lines (151 loc) · 5.08 KB
/
chooseRecipes.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
#include "chooseRecipes.hpp"
#include "mylib.h"
#include <iostream>
#include "location.h"
#include "grocerylist.h"
#include <fstream>
#include "mealschedule.hpp"
#include <filesystem>
#include <algorithm>
using namespace std;
void addUserRecipes(vector<string>& recipeList, vector<Recipe>& recipesChosen, MealSchedule& schedule, bool noSchedule)
{
if (!exists(config.rcp_meals)) return; //user specified no recipes
ifstream meals(config.rcp_meals);
string recipeName;
while (getline(meals, recipeName))
{
if (trim(recipeName) == "") continue;
//parse the current line of the input file
bool out = false, alreadyMade = false;
signed char addToDay = -1;
if (recipeName.substr(3, 1) == ":" && !noSchedule)
{
string day = recipeName.substr(0, 3);
for (int i = 0; i < 7; ++i)
{
if (day == MealSchedule::days[i]) //if the day the user specified for the recipe is the ith day of the week
{
recipeName = trim(recipeName.substr(4));
if (recipeName == "OUT") out = true;
addToDay = i;
break;
}
}
}
if (recipeName.substr(0, 1) == "(" &&
recipeName.substr(recipeName.size() - 1) == ")")
//if it's surrounded in brackets, which means that recipe is leftovers
{
alreadyMade = true;
recipeName = recipeName.substr(1, recipeName.size() - 2); //trim the brackets
}
if (find(recipeList.begin(), recipeList.end(), recipeName) == recipeList.end())
throw InvalidFileErr("`" + recipeName + "' is not a valid recipe.");
if (out)
{
schedule.out(addToDay);
}
else
{
Recipe recipe(recipeName, alreadyMade);
readInto(recipe);
if (schedule.canBeAdded(recipe, addToDay))
{
if (!noSchedule) schedule.add(recipe, addToDay);
recipesChosen.push_back(recipe);
}
}
}
}
bool chooseRecipes(vector<string>& recipeList, vector<Recipe>& recipesChosen, MealSchedule& schedule, bool allRecipes, bool noSchedule)
{
vector< vector<string>::size_type > recipesConsidered; //indices of the
//recipes already considered
vector<string>::size_type numRecipesLeft = recipeList.size(); //Keep track
// of how many recipes we have left to consider; when it reaches 0, we're done
//keep looping until 1) they enter s, 2) all recipes have been presented, or
//3) the schedule is complete.
while(numRecipesLeft-- > 0 && (!schedule.isDone() || noSchedule)) {
vector<string>::size_type recipeChosen; //the index in recipeList of the
//recipe chosen randomly
string recipeName; //the name of the recipe chosen randomly
string ans; //the user's response
do {
recipeChosen = ranInt(recipeList.size()) - 1; //pick a random next recipe
recipeName = recipeList[recipeChosen];
} while (find(recipesConsidered.begin(),
recipesConsidered.end(),
recipeChosen) != recipesConsidered.end()); /*ensure that
the same recipe is not considered twice. */
Recipe recipe(recipeName);
readInto(recipe);
if (config.rcp_ownedingredients != "")
{
GroceryList ownedIngredients;
{
ifstream fowned(config.rcp_ownedingredients);
fowned >> ownedIngredients;
}
recipe.printMissingIngredients(ownedIngredients);
}
if (allRecipes)
ans = "Y";
else if (!noSchedule && !schedule.canBeAdded(recipe))
//Don't ask the user for recipes that couldn't "fit" in the schedule anyway
ans = "N";
else {
bool alreadyChosen = false; //True if the random recipe has already been chosen for cooking
//Some recipes were already chosen by the user in the input file. Don't ask for those ones again.
for (vector<Recipe>::iterator r = recipesChosen.begin(); r != recipesChosen.end(); ++r)
{
if (r->name == recipeName)
{
alreadyChosen = true;
break;
}
}
if (alreadyChosen) ans = "N";
else
{
cout << "Use " << recipeName << "? (y/n, s to stop, recipe name to use that recipe) ";
cin >> ans;
}
}
if (ans == "S" || ans == "s" || cin.fail())
return true;
bool yes = (ans == "Y" || ans == "y"), no = (ans == "N" || ans == "n");
if (yes || no) recipesConsidered.push_back(recipeChosen);
if (no) {}
else
{
if (!yes) //entered either a specific recipe name, or something invalid
{
recipeName = ""; //Assume it's an error until we find otherwise
vector<string>::iterator pos;
if ((pos = find(recipeList.begin(), recipeList.end(), ans)) != recipeList.end())
{
vector<string>::size_type index = distance(recipeList.begin(), pos);
recipesConsidered.push_back(index);
recipeName = ans;
cout << "Adding " << recipeList[index] << ".\n"; //extra check that "index" is correct
//Have to actually use the new recipe rather than the randomly chosen one
recipe = Recipe(recipeName);
readInto(recipe);
}
else
{
cout << "Error:" << ans << " is not a recipe or a valid command.\n";
continue; //Error occurred; don't add the recipe.
}
}
recipesChosen.push_back(recipe);
if (!noSchedule)
{
schedule.add(recipe); //can't fail, because of the canBeAdded check above
cout << schedule;
}
}
}
return false;
}