• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add recipe and their ingredients?  RSS feed

 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how to add recipe and it has another array list for ingredients and steps...I am not sure if I am on the right path.

 
mark cortez
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ares you tying to make a 2d array?
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is 2D array. But want to use recipe.add(ri); as well as how to add ingredients one by one for each recipe?

Thanks in advance for your help.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things:

* Does your code compile? The code you posted won't. The constructor is misspelled.

* You don't have an add() method in Recipe. Are you asking how to created it? What have you tried?

* ingredients and Steps need to be instance variables like title and prepTime.

* Steps should be steps by convention.

* You probably want an addIngredients() and an addSteps() method.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ingredients and steps variables are only in your Recipe constructor.
Should they be attributes of your Recipe class?


I think what you want to do here is step away from the computer and come up with a design/plan for how this is going to work. On paper. Draw a picture, a timeline whatever - but get it clear in your mind what you are trying to accomplish. THEN come back and write some code :-)

Do you want a User to enter in these recipes, or is it meant to read the values from somewhere?

How do you set the steps into the Recipe?
There are a couple of ways
#1 - Give the Recipe the entire list of steps: setSteps(listOfSteps)
#2 -Have a method "addStep(step)". The Recipe then takes that step, and adds it into its list of Steps (Builder pattern might be useful)

Similar for ingredients.

hope this helps some,
Stefan
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you all, previous code was not compiled, wanted to give structure, so you guys can show me how to create recipe and add their ingredients and steps. Than will create addRecipe/addIngredients/addSteps. I am stuck.. Below is not my final code but did all setter and getters as well as constructor with recipe as well as 1 array list one for ingredient. Do I have to create separate class for ingredients? Thank you all for your help.

 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark cortez wrote:Ares you tying to make a 2d array? . . .
No, because there ain't no such thing. What you showed is an array of arrays which is better than a 2D array. Java® does not support 2D arrays, though other languages might.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 questions:

I have Recipe class and it has below fields, now I am not sure if I have to have Ingredient class of ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>(); OR ArrayList<String> ingredients = new ArrayList< String >();?

How to populate both the array list ? As it is Arraylist within Arraylist.


e.g


 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither nor.
Don't declare your List as ArrayList. Declare it as List.
Don't use Strings. Write:-
List<Ingredient> ingredients = new ArrayList<>();
The <> bit only works in Java7+.
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you declare your variables in constructor?
Keep them as instance variables. Remember, constructor is like a special method, so, the scope of visibility would be limited to the constructor. With constructor probably you want them to initialize assign with values only.

Why your variables which are meant to be a member/field variables are public?
Probably you want them to keep as private, and make some methods which are called setters and getters to set them and later get.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now how can I create method addRecipeWithIngredient();? As well as how to populate list within list?


 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to populate both the array list ? As it is Arraylist within Arraylist.

This is where what Campbell said about lists of lists is important. You want a List of something. What is that something?


The something is a List of something, so

In your case, it's a list of list of Ingredients, so

How do you populate this? You add a list of Ingredients to you list of lists. I'm going to illustrate with a list of list of ints:

 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tell us in simple language what addRecipeWithIngredient is supposed to do.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to add recipe and their ingredients in the List. I have List inside List.

(1) Title = "Omlet"
PrepTime = "2"
iDescription= "Egg"
setAmount ="1"
iDescription ="Bread"
Amount ="2"
(2) Title = "Nachos"
PrepTime = "2"
iDescription= "Avacado"
setAmount ="4"
iDescription ="Chips"
Amount ="1"
iDescription ="Lemon"
Amount ="1"
(3) Title = "Cake"
PrepTime = "2"
iDescription= "Egg"
setAmount ="2"
iDescription ="BakingSode"
Amount ="1"
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ket Shah wrote:Now how can I create method addRecipeWithIngredient();? As well as how to populate list within list?

My advice: Do ONE thing at a time.

Your classes are looking OK, except that Recipe.prepTime should be a numeric field. (It'd be nice if Ingredient.amount was too, but "amounts" can be quite tricky, so maybe a String is fine for now).

But get them working first. Test them up, down, sideways, forwards and backwards, and make sure that you can add any Ingredient to any Recipe whenever you want to ... and - MUCH more importantly - that you can't add an Ingredient when you shouldn't.

For example: Do you want to allow:
  myRecipe.add( new Ingredient("Carrot", 5) );
  myRecipe.add( new Ingredient("Something else", 3) );
  myRecipe.add( new Ingredient("Carrot", 46) );
  myRecipe.add( new Ingredient("Carrot", 73) );
  ...
?

If not: how do you prevent it? (Hint: what makes one Ingredient "equal" to another?)

Work that stuff out and test the hell out of it before you start adding new things.

Also, heed what Knute's saying: creating an "empty" Ingredient and then setting its description and amount just doesn't make any sense. Do it all at once.

HIH

Winston
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Ingredient has a description and an amount. A Recipe has a title, prep time, and a list of Ingredients.

I would create a list of Ingredients, add all Ingredients to that list, then create a Recipe object and add the Ingredients list. I'm not sure why you would need a list of lists. You might have a list of Recipes.
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . .
This is where what Campbell said about lists of lists is important. You want a List of something.
. . .
I didn't realise we needed a List of Lists.

If you declare and initialise the List as
List<List<ingerdients>> recipes = new ArrayList<>();
you can add different sorts of List:-
recipes.add(new ArrayList<Ingredient>());
recipes.add(new LinkedList<Ingredient>());

I do not know whether you can use the <> operator in those cases: try it and see.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I would create a list of Ingredients, add all Ingredients to that list, then create a Recipe object and add the Ingredients list...

Hmmm. Not sure I agree with that - although it's certainly one way to do it.

PersonalIy, I would do it the same way as Ket - ie, a Recipe (basically) IS the Ingredients. No need to create a separate List of them.

@Ket: One possibility is to have your Recipe.add() method return the Recipe it's called on, viz:which allows you to code something like:which looks very similar to what you wrote above, don't you think?

It's a style used in something called a fluent interface, which can be very user friendly if written properly - however, they're also easy to get wrong, so beware.

Winston
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please see my Main Class, It is correct to add recipe and ingredients? I am on the right track? if yes how to now display the recipe and their ingradeints. I have got many response but none of them is telling how to create a List. I want to store recipe and their ingredients. So can anyone tell me what should I use, List or List in List?

 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the way you have it new, ingredients is public, so you could just use:

But ideally, you don't want your instance variables public. Make them private, then create a addIngredient(Ingredient ingredient) method that adds to ingredients for you.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It expects string. myNachosIngredient1 is type of Ingredient

 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no one is able to answer my question. How can I relate recipe list to the ingredient list? Please help.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't completely understand your question. Posting your full code would probably help.

Is your question, "How can I enter Strings and update a List<Ingredients>"? You would have to make some connection between String and Ingredient, but why? You have

item is a String? Why not make it an Ingredient?
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add Recipe and their respective ingredients as below. I want to use list of list, so I have to define List<List<Ingredients>> = ArrayList<>(); in recipe constructor? or in the Main class?

(1) Title = "Omlet" - Recipe
PrepTime = "2" - Recipe

iDescription= "Egg" - Ingredient
setAmount ="1" - Ingredient
iDescription ="Bread" - Ingredient
Amount ="2" - Ingredient

(2) Title = "Nachos" - Recipe
PrepTime = "2" - Recipe

iDescription= "Avacado" - Ingredient
setAmount ="4" - Ingredient

iDescription ="Chips" - Ingredient
Amount ="1" - Ingredient
iDescription ="Lemon" - Ingredient
Amount ="1" - Ingredient

(3) Title = "Cake" - Recipe
PrepTime = "2" - Recipe

iDescription= "Egg" - Ingredient
setAmount ="2" - Ingredient
iDescription ="BakingSode" - Ingredient
Amount ="1"- Ingredient

So below constructor is ok to add above recipe and ingredient values?

 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're guessing and dealing with too many classes at the same time.
Implement one class, then test it, then start with other.

1. Your Recipe class should represent one specific recipe. You don't have any members of that class, where you should have recipeName or recipeTitle or similar (that should be private access modifier). Same with prepTime, which better would read as preparationTimeInMinutes (clearer right away, isn't it?).
2. Do not shorten veriable names of the price of readability. With 2 variables maybe you'd manage to remember what these mean, but with more, probably you'd start confusing yourself and your code reader.
3. Why your Recipe constructor does not accept any parameters? The same with Ingrediens class constructor.

 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Liutauras Vilda , I have tried all different ways if you read previous messages you will get it. I have simple question I want to add Recipe and their respective ingredients as below. I want to use list of list, so I have to define List<List<Ingredients>> = ArrayList<>(); in recipe constructor? or in the Main class? Can you give me high-level structure? how to define Ingredient list in Recipe list?

(1) Title = "Omlet" - Recipe
PrepTime = "2" - Recipe

iDescription= "Egg" - Ingredient
setAmount ="1" - Ingredient
iDescription ="Bread" - Ingredient
Amount ="2" - Ingredient

(2) Title = "Nachos" - Recipe
PrepTime = "2" - Recipe

iDescription= "Avacado" - Ingredient
setAmount ="4" - Ingredient

iDescription ="Chips" - Ingredient
Amount ="1" - Ingredient
iDescription ="Lemon" - Ingredient
Amount ="1" - Ingredient

(3) Title = "Cake" - Recipe
PrepTime = "2" - Recipe

iDescription= "Egg" - Ingredient
setAmount ="2" - Ingredient
iDescription ="BakingSode" - Ingredient
Amount ="1"- Ingredient
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need List of Lists? Ingredients will have ingredients? Technically it is true, but probably you don't want to go down to molecule level.

You have class Recipe (which represents singular recipe), recipe has its own name, preparation time (in minutes, hours), and list of ingredients are needed to prepare it. Forget the main class for now.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, Please let me know other options, so that I can store the values of recipe and their corresponding ingredients. Than later once I am done setting the initial recipe and their ingredients will have to add the recipe, remove recipe too.

 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ket Shah wrote:@Liutauras Vilda, Please let me know other options, so that I can store the values of recipe and their corresponding ingredients. Than later once I am done setting the initial recipe and their ingredients will have to add the recipe, remove recipe too.
You don't need other options. What is wrong with the way what others and myself mentioned?

Imagine you have a classes: RecipeBook, Recipe, Ingredient.
Your RecipeBook will contain list of Recipes
Your Recipe will contain list of Ingredients

So, what you need to do?
1. Implement Ingredient class
2. Implement Recipe class
3. Implement RecipeBook class (if it were a requirement)
4. Implement Main class which starts reading the book
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ket Shah wrote:. . .
So below constructor is ok to add above recipe and ingredient values?
. . .
No.

You are alowing the class to be instantiated with its fields pointing to null. That is poor practice, I am afraid. Only give the class a constructor which initialises all fields to “real” values. Get rid of that no‑arguments constructor.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, I have already done, please see below. I am able to add recipe as well as ingredients. Now how I will know which recipe as which ingredients? How I can maintain relationship? As well as currently I have just added one ingredient for each recipe I want to add more ingredients.

 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ket Shah wrote:. . .
No. Get rid of those no‑arguments constructors.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, I have already done, please see below. I am able to add recipe as well as ingredients. Now how I will know which recipe has which ingredients? How I can maintain relationship? As well as currently I have just added one ingredient for each recipe I want to add more ingredients.

@Campbell Ritchie, Got rid of those no‑arguments constructors.

 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have method:

Everyone can easily change prepTime directly via object reference, because:


Remember one thing, all instance variables should be private, unless you really need differently.
What if I think preparation time is -4.0 minutes or hours. That is perferctly fine?

Everything goes down to the same problem, you're in a hurry. Desperately trying to finish up this task, but it takes longer that way. Take short break and get back to the problem later, will look again, make some notes on a piece of paper. Revise what has been mentioned in this thread already. You should have pretty much everything you need to finish.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, please don't get me wrong but if you see I am actively working on this thread since 2nd November. I am just stuck and not sure how to maintain the relationship between recipe and ingredients. I will do my unit testing once I am done with the base structure. If you go through all the messages everyone is coming and advising me for small stuff and I agree it is important but no one is helping me for my real problem.
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read again Winston's post (<- link), then Knute's, then Campbell's, and then again Winston's. It seems everything has been explained in those 4 posts. It appears you missed them. Do not rush, slow down a bit, you miss important things you've been told already there.
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, I did it few times and that is not helping. Below is my updated ReceipeBook class. Can you show me how can I now dispaly Recipe and their respective ingredients? I can display individually. As all recipe and than all ingredients.

 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could look similar to (it is super simplified and shortened):
 
Ket Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda, thank you so much. Now I am able to related recipe and their ingredients. I have added few more too. Any idea how I will display all recipes and their respective ingredients?

 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ket Shah wrote:Now I am able to related recipe and their ingredients. I have added few more too. Any idea how I will display all recipes and their respective ingredients?

Become an author, publish the book with all recipes in it and let someone to read it (i'm not joking here).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!