Restaurant Kitchen in C++ Homework Sample

You have been asked to help out a small restaurant with a system to store a list of recipes for dishes they can offer. You need to store the data using a set (not one of the sets provided by the standard library) to store whether the ingredient is used in a recipe. You also need to write functions to list all the ingredients that are in just one recipe, the ingredients that are not used in any recipe. For more C++ programming assignments contact us for details.

Solution:

dish.h

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<fstream>
using namespace std;
/*
class to hold the dish igredients
*/

class Dish
{
public:
Dish(string dish) {
setName(dish);
fillIngredients(dish);
canCook = 1;
}
bool getcanCook()
{
return canCook;
}
void setcanCook(bool can)
{

canCook = can;
}
vector<string> getIngredients()
{
return this->ingredients;
}
string getName() {
return this->name;
}
bool contains(string ingred)
{
for (int i = 0;i < ingredients.size();i++)
{
if (ingredients[i] == ingred)
return 1;

}

return 0;
}
private:
string name;
vector<string> ingredients;
bool canCook;

void fillIngredients(string dish)
{
/*fill the ingredients vector with the ingredients*/
string temp = “”;
// cout <<“\n### ” << dish << endl;
for (int i = name.size()+1;i < dish.size();i++)
{
if (dish[i] == ‘,’) // ingredient name is completed
{
this->ingredients.push_back(temp); // add it to the list
temp = “”; // free the string again
continue; //skip this time
}
temp += dish[i];// concatinate to the temp
}
this->ingredients.push_back(temp);
}

void setName(string dish)
{
string temp = “”;
for (int i = 0; dish[i] != ‘:’; i++)
{
temp += dish[i]; // hold the dish name in the temp
}
this->name = temp;
}
};

restaurant.h

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<fstream>
#include<algorithm>
#include”dish.h”
#include <iomanip>
#include<stdio.h>
#include”setFunctions.h”
using namespace std;

class Restaurant
{
public:

Restaurant(string fileName)
{
this->file.open(fileName.c_str());
if (this->file.good() == 0)
{
cout << “\nERROR !! FILE NOT FOUND\n”;
return;
}
outfile.open((fileName+”out.txt”).c_str(), fstream::out);
readFile();
fillVectorsWithZeros();
markSympols();
mark_remaining();
}
void write_to_file()
{
ingred_width += 1, dish_width += 1;
outfile<<” “;
outfile << setw(dish_width) << “Dish Name:”;
for (int i = 0; i < ingredients.size();i++)
outfile <<setw(ingred_width) << ingredients[i] << “”;
outfile << endl;
for (int i = 0;i < dishes.size(); i++)
{
if (dishes[i].getcanCook())
{
outfile << setw(dish_width) << dishes[i].getName() << “:”;
for (int j = 0; j < ingredients.size();j++)
{
outfile << setw(ingred_width) << dishes[i].contains(ingredients[j]) << “”;
}
outfile << endl;
}
}
for (int i = 0; i < 1000; i++)
outfile << “-“;
outfile << endl << ” “;
outfile << setw(dish_width) << “At Least One:”;
for (int i = 0;i < one_or_more.size(); i++)
outfile << setw(ingred_width) << one_or_more[i] << “”;
outfile << endl<< ” “;
outfile << setw(dish_width) << “More Than One:”;
for (int i = 0;i < one_or_more.size(); i++)
outfile << setw(ingred_width) << more_than_one[i] << “”;
outfile << endl<< ” “;

outfile << setw(dish_width) << “Exactly One:”;
for (int i = 0;i < one_or_more.size(); i++)
outfile << setw(ingred_width) << exactly_one[i] << “”;
outfile << endl<< ” “;

outfile << setw(dish_width) << “Not Used:”;
for (int i = 0;i < one_or_more.size(); i++)
outfile << setw(ingred_width) << not_used[i] << “”;
outfile << endl<< ” “;

}

private:
void mark_remaining() {
exactly_one = difference_(one_or_more, more_than_one);
not_used = not_(union_(one_or_more, more_than_one));
}
void fillVectorsWithZeros()
{
for (int i = 0;i < ingredients.size();i++)
{
ingred_width = max((int) ingredients[i].size(), ingred_width);
one_or_more.push_back(0);
more_than_one.push_back(0);
exactly_one.push_back(0);
not_used.push_back(0);
}
for (int i = 0; i < dishes.size();i++)
{
dish_width = max((int) dishes[i].getName().size(), dish_width);
}
}
int getIndex(string ingred)
{
for (int i = 0;i < ingredients.size();i++)
{
if (ingred == ingredients[i])
return i;
}
return -1;
}

vector<int> fillSympols2(vector<string> vec)
{
vector<int> ret;
int index;
for (int i = 0;i < vec.size();i++)
{
index = getIndex(vec[i]);
if (index == -1)
{
// one of the dish ingredients is not in the restaurant
ret.push_back(i);
}
else
{
if (one_or_more[index]) // check if the ingredient were used before
{
more_than_one[index] = 1; // mark it as used more than one
}
one_or_more[index] = 1; // mark it as used more or more times

}
}
return ret ;
}

void markSympols()
{
vector<string> tempHolder;
for (int current_dish = 0; current_dish < this->dishes.size(); current_dish++)
{
tempHolder = this->dishes[current_dish].getIngredients(); //
vector<int> ret = fillSympols2(tempHolder);
if ( ret.size())
{
dishes[current_dish].setcanCook(0);
for(int i =0;i<ret.size() ; i++)
outfile << “The dish ” << this->dishes[current_dish].getName() << ” contains ” << tempHolder[ret[i]] << ” which is not something we have.\n\n”;

}
}
}
void fillIngredients(string ingred)
{
/*fill the ingredients vector with the ingredients*/
string temp = “”;
for (int i = 0;i < ingred.size();i++)
{
if (ingred[i] == ‘,’) // ingredient name is completed
{
this->ingredients.push_back(temp); // add it to the list
temp = “”; // free the string again
continue; //skip this time
}
temp += ingred[i];// concatinate to the temp
}
this->ingredients.push_back(temp); // add it to the list
}

void readFile() {
string ingred;
getline(file, ingred);
fillIngredients(ingred);
while (getline(file,ingred)) {
dishes.push_back(Dish(ingred)); // fill the ingredients list
}
}
vector<string> ingredients;
vector<bool> one_or_more, more_than_one, exactly_one, not_used;
fstream file;
vector<Dish> dishes;
fstream outfile;
int ingred_width = -1;
int dish_width = -1;
};

setFunctions.h

#ifndef setFUNCTIONS
#define setFUNCTIONS

vector<bool> difference_(vector<bool> a, vector<bool> b);
vector<bool> union_(vector<bool> a, vector<bool> b);
vector<bool> not_(vector<bool> a);

#endif // !setFUNCTIONS

setFunctions.cpp

#include<iostream>
#include<vector>
#include<map>
#include<string>

using namespace std;

vector<bool> difference_(vector<bool> a, vector<bool> b)
{
vector<bool> ans;
/*get the difference of 2 sets*/
for (int i = 0;i < a.size();i++)
{
ans.push_back(a[i] && (!b[i]));
}

return ans;
}
vector<bool> union_(vector<bool> a, vector<bool> b)
{
/*
get union of 2 sets
*/
vector<bool> ans;
for (int i = 0;i < a.size();i++)
{
ans.push_back(a[i] || b[i]);
}
return ans;
}

vector<bool> not_(vector<bool> a)
{
/*
get not of a set
*/
vector<bool> ans;
for (int i = 0;i < a.size(); i++)
{
ans.push_back(!a[i]);
}

return ans;
}

main.cpp

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include”restaurant.h”
using namespace std;

int main()
{
string fileName;
cin >> fileName;
Restaurant test(fileName);
test.write_to_file();
return 0;
}