Rapidly hitting snags - comparing items in list (c++ or obj-c)
My aim is to put this into obj-c but at this stage I"m trying to build it in c++ so here we go
... Okay, some minor amount of advice needed for this one.
I'm am trying to write a class that holds a few things for an "item" and I was trying to figure out a way to have a list of conditions in which this item is to be used. An analogy might be this:
I have some instances of the class "recipe" called:
FrenchToast
Omlette
RockyFuel
ToadInTheHole
Each of these would require certain ingredients
FrenchToast (eggs, milk, butter, bread)
Omlette (eggs, milk, mushrooms, cheese)
RockyFuel (eggs)
ToadInTheHole (eggs, bread, butter)
Now, say I provide an input like I have eggs, butter and bread I'd like to be given the list of htings that I could make (i.e. FrenchToast, RockyFuel and ToadInTheHole)
The best thing I've thought of so far (while on a plane) was to write them as binary numbers with the ingredients being 1 or 0.
Eggs milk butter bread mushrooms cheese would be
111100 for FrenchToast
110011 for omlette
100000 for RockyFuel
101100 for ToadInTheHole
so, if I said I had the following in my "pantry"
101100 I could do an & operator to say
if (frenchtoast.ingredients & pantry.ingredients ) == frenchToast.ingredients)
// yes, can make this
else
// cannot make this
BUT here is where I get worried about the future of my program. What if I add a new ingredient at some point, say tomatoes. How do I keep it flexible to expand it to another digit? When I read in a "Master List" of ingredients, I could do bitwise operation accross a string but could I just keep adding to my list like above?
Perhaps I've gone completely batty and missed an obvious way of storing these things (like an array? linked list?)
Sorry for the complexity of the question, just thought I'd put it out there!!
Cheers,
cbrad.)
Either this is hard, I'm stupid ... or both.
I've written three files (in two weeks) and I think I've gone down the wrong path already.
Ingredient.h
Ingredient.cpp
IngredietnDriver.cpp
- I have a class Ingredient and I can instantiate different ingredients like eggs, milk, butter etc. So by my understanding I have an object for each ingredient that I need.
- I've included the linked list project that we built with Wibit.net and in the driver I create a linked list called "Pantry" this then holds all my ingredients that I currently have
- So, the next step (I think) would be to make another linkedlist object called FrenchToast, Omlette, RockyFuel and ToadInTheHole and then add ingredients to those but my first question would be, is this where I create a new class and have the linked list in that class called "recipe" then every "recipe" I instantiate would have a list in it for ingredients?
- Once I have a "pantry" and a "recipe" I will need to compare them somehow but that is well off at this stage.
Code so that you know I have "something" ...
//Ingredient.h
#include <iostream>
#include <string>
using namespace std;
class ingredient
{
private:
string _ingredientName;
string _ingredientDescription;
public:
ingredient();
ingredient(string);
ingredient(string,string);
~ingredient();
void setIngredientName(string);
void setIngredientDescription(string);
string getIngredientName();
string getIngredientDescription();
friend std::ostream &operator <<(std::ostream&, ingredient&);
friend std::istream &operator >>(std::istream&, ingredient&);
};//Ingredient.cpp
#ifndef INGREDIENT_CLASS
#define INGREDIENT_CLASS
#include "Ingredient.h"
ingredient :: ingredient()
{
this->_ingredientName = "";
this->_ingredientDescription = "";
}
ingredient :: ingredient(string ingredientName, string ingredientDescription)
{
this->setIngredientName(ingredientName);
this->setIngredientDescription(ingredientDescription);
}
ingredient :: ingredient(string ingredientName)
{
this->setIngredientName(ingredientName);
this->setIngredientDescription(" ");
}
ingredient :: ~ingredient()
{
}
void ingredient :: setIngredientName(string name)
{
this->_ingredientName = name;
}
void ingredient :: setIngredientDescription(string desc)
{
this->_ingredientDescription = desc;
}
string ingredient::getIngredientName()
{
return this->_ingredientName;
}
string ingredient::getIngredientDescription()
{
return this->_ingredientDescription;
}
std::ostream &operator<<(std::ostream& stream, ingredient& str)
{
stream << str.getIngredientName();
return stream;
}
std::istream &operator>>(std::istream& stream, ingredient& str)
{
std::string tempString = "";
stream >> tempString;
str.setIngredientName(tempString.c_str());
return stream;
}
#endif
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include "linkedList.cpp"
#include "ingredient.cpp"
using namespace std;
int main()
{
srand(time(NULL));
linkedlist<ingredient> pantry;
//todo: add a quantity somewhere in the class
pantry.add(ingredient("eggs", "chicken things"));
pantry.add(ingredient("milk", "from cows"));
pantry.add(ingredient("cheese",""));
pantry.add(ingredient("butter"));
int i;
for(i = 0; i < pantry.count(); i++)
{
cout << pantry[i].getIngredientName() << endl;
}
return 0;
}
Cheers,
cbrad.)
No worries buddy!
Let's take a look here.. You're on the right track.. let's basically move your driver into it's own recipe class.
Then that will contain a linked list of ingredients:
//Recipe.h
#include <iostream>
#include <string>
#include "linkedList.cpp"
#include "ingredient.cpp"
using namespace std;
class recipe
{
private:
string _recipeName;
linkedlist<ingredient> _ingredients;
public:
recipe();
recipe(string);
~recipe();
void setRecipeName(string);
string getRecipeName();
void addIngredient(ingredient);
void addIngredients(linkedlist<ingredient>);
void printIngredients();
};
Then the imp:
//Recipe.cpp
#include "Recipe.h"
recipe::recipe()
{
this->_recipeName = "";
}
recipe::~recipe()
{
}
recipe::recipe(string recipeName)
{
this->setRecipeName(recipeName);
}
void recipe::setRecipeName(string name)
{
this->_recipeName = name;
}
string recipe::getRecipeName()
{
return this->_recipeName;
}
void recipe::addIngredient(ingredient name)
{
this->_ingredients.add(name);
}
void recipe::addIngredients(linkedlist<ingredient> ingredients)
{
int i;
for(i = 0; i < ingredients.count(); i++)
{
this->_ingredients.add(ingredients[i].getIngredientName());
}
}
void recipe::printIngredients()
{
int i;
for(i = 0; i < _ingredients.count(); i++)
{
cout << _ingredients[i].getIngredientName() << endl;
}
}
This is all basically what you already have..
And then the driver:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include "linkedList.cpp"
#include "ingredient.cpp"
#include "recipe.cpp"
using namespace std;
int main()
{
srand(time(NULL));
linkedlist<ingredient> pantry;
//todo: add a quantity somewhere in the class
pantry.add(ingredient("eggs", "chicken things"));
pantry.add(ingredient("milk", "from cows"));
pantry.add(ingredient("cheese",""));
pantry.add(ingredient("butter"));
int i;
for(i = 0; i < pantry.count(); i++)
{
cout << pantry[i].getIngredientName() << endl;
}
recipe frenchToast("French Toast");
string eggs = "french eggs";
frenchToast.addIngredient(eggs);
frenchToast.printIngredients();
return 0;
}
Once that's all built out with how you want the recipe.. then you add a method to recipe and a method to recipe that takes a pantry linked list as a param and compare it to the ingredients.
Youre almost there!! Hopefully this little bump helps!
Bryan
Forum Rules/Features
Welcome to the WiBit.net forums! Check out our terms and features:
Hot Topic(s)
Annoy your friends
Is this something you think someone else would enjoy? Is it not for you, but do you know someone who is down with this type of content? Well share away:
Tweet
The Blog
Don't Let XML Make You a Cheater
This blog is a reminder that cheating in software development can get you into big trouble. Sometimes developers get really really lazy, OR are pressured to write something using overly simplified data structures. Almost every time this happens you are bitten in the butt! Sometimes the problems show up immediately and other times it may take months or years (especially in integrated systems).
- 1 of 78
- ››























Nice project! I like it...
Here is what I think you should do...
Your items that belong to the "recipe" should be stored using a data structure that is expandable. For example, instead of creating a class for each recipe you should create a parent class that contains all the code to add / delete an item, overload your operators for comparisons, etc... and then inherit this class into your specific recipes. As for storing your items, I beleive you can use a collection of some sort (LinkedList for example) to "add" an item. Consider creating a class or structure for each of your items to make sure they are unique.
If you design it this way, you can add and remove items from recipies easily, and you only have to code your operator overloading once (define that ==, && means), AND since you are using a super class you will have the polymorphism factor, which again gives you the ability to write your code once and apply it to multiple classes.
Let me know if this is helpful... If you quit, I'll see if I can put some code together for you but I think you can do this!