#include #include #include "recipe.h" #include "kitchen.h" #include "ingredient.h" using namespace std; // Helper functions void readRecipe(istream &istr, ostream &ostr, list &recipes); void addIngredients(istream &istr, ostream &ostr, Kitchen &kitchen); void printRecipe(istream &istr, ostream &ostr, list &recipes); void makeRecipe(istream &istr, ostream &ostr, list &recipes, Kitchen &kitchen); // The main loop parses opens the files for I/O & parses the input int main(int argc, char* argv[]) { // Check the number of arguments. if (argc != 3) { cerr << "Usage: " << argv[0] << " in-file out-file\n"; return 1; } // Open and test the input file. ifstream istr(argv[1]); if (!istr) { cerr << "Could not open " << argv[1] << " to read\n"; return 1; } // Open and test the output file. ofstream ostr(argv[2]); if (!ostr) { cerr << "Could not open " << argv[2] << " to write\n"; return 1; } // the kitchen & recipe list Kitchen kitchen; list recipes; // some variables to help with parsing char c; while (istr >> c) { if (c == 'r') { // READ A NEW RECIPE readRecipe(istr,ostr,recipes); } else if (c == 'a') { // ADD INGREDIENTS TO THE KITCHEN addIngredients(istr,ostr,kitchen); } else if (c == 'p') { // PRINT A PARTICULAR RECIPE printRecipe(istr,ostr,recipes); } else if (c == 'm') { // MAKE SOME FOOD makeRecipe(istr,ostr,recipes,kitchen); } else if (c == 'k') { // PRINT THE CONTENTS OF THE KITCHEN kitchen.printIngredients(ostr); } else { cerr << "unknown character: " << c << endl; exit(0); } } } void readRecipe(istream &istr, ostream &ostr, list &recipes) { int units; string name, name2; istr >> name; // build the new recipe Recipe r(name); while (1) { istr >> units; if (units == 0) break; assert (units > 0); istr >> name2; r.addIngredient(name2,units); } // add it to the list recipes.push_back(r); ostr << "Recipe for " << name << " added" << endl; } void addIngredients(istream &istr, ostream &ostr, Kitchen &kitchen) { int units; string name; int count = 0; while (1) { istr >> units; if (units == 0) break; assert (units > 0); istr >> name; // add the ingredients to the kitchen kitchen.addIngredient(name,units); count++; } ostr << count << " ingredients added to kitchen" << endl; } //PrintRecipe function iterates through the list of recipes //If the recipe is found, then it prints out the recipe, and exits the function //If all recipes are iterated through, and the function hasn't been exited, "No recipe..." message void printRecipe(istream &istr, ostream &ostr, list &recipes) { string name; istr >> name; list::iterator l_itr=recipes.begin(); for(l_itr;l_itr!=recipes.end();l_itr++) { if((*l_itr).getName()==name) { (*l_itr).printRecipe(istr,ostr); return; } } ostr << "No recipe for " << name << endl; } void makeRecipe(istream &istr, ostream &ostr, list &recipes, Kitchen &kitchen) { string name; istr >> name; bool haverecipe=false; //Iterate through the list to see if we have the recipe list::iterator l_itr=recipes.begin(); for(l_itr;l_itr!=recipes.end();l_itr++) //If recipe is found, break out of the loop if((*l_itr).getName()==name) { haverecipe=true; break; } //If recipe is not found, print message, exit function if(!haverecipe) { ostr << "Don't know how to make " << name << endl; return; } //Upon success, check if kitchen has enough ingredients. If it does, make the recipe if(RecipeIngredients(kitchen, *l_itr)) { ostr << "Made " << name << endl; removeIngredients(kitchen, *l_itr); } else { ostr << "Cannot make " << name << " need to buy:" << endl; needToBuy(ostr,kitchen,*l_itr); } }