0

i would like to have in my class a nested hastable to set the amount of ingredients. Please consider the following scenario.

Scenario:

One recipe has several ingredients:

public class Ingredient {

    private static int id;

    String name;



    public Ingredient(String name) {
        this.name = name;
    }
}

public class Recipe {

public static enum uom{
    ml,gr, unit, teaspoon, tablespoon,cup,coffeespoon
};

public String name;

public Hashtable<Ingredient,Hashtable<uom,Integer>> ingredients;
public String description;

public Recipe(String name, String description, Hashtable<Ingredient,Hashtable<uom,Integer>> ingredients) {

    this.name = name;
    this.description = description;
    this.ingredients = ingredients;
}


public static void main (String [] args) {
    Ingredient lemon = new Ingredient("lemon");

    Hashtable<Ingredient,Hashtable<Recipe.uom,Integer>> ingredient = null;

    ingredient.put(new Ingredient("Lemon"),new Hashtable(uom.unit,1));

    Recipe LemonPie = new Recipe("Lemon pie","blabla",ingredients);

}

}

Here in this scenario i want to include inside recipe the amount of each ingredients for that i believe hash table is the best approach. But how can i set a hashtable inside another (something like this):

{new Ingredient("Lemon") :  {"unit":1}}

where unit is a enum of class Recipe.

Hashtable<Ingredient,Hashtable<Recipe.uom,Integer>> ingredient = null;

ingredient.put(new Ingredient("Lemon"),new Hashtable(uom.unit,1));

It says that Hashtable (int,float) in Hashtable cannot be applied to (Recipe.uom,int)

Question: Taking this scenarion in consideration. How can i set a hashtable inside another taking as key a enum?

ePascoal
  • 2,362
  • 6
  • 26
  • 44
  • 1
    why do you need a hashtable to store the unit and ammout of ingredient? that could be simple properties in the ungredient class... – Timothy Truckle Nov 20 '16 at 17:05
  • `HashTable` is an "old" class, and it is now recommended to use `HashMap` instead, certainly if you don't need the synchronization. – Todd Sewell Nov 20 '16 at 17:05
  • @TimothyTruckle, i would like a list of existing ingredients of my recipes for that i will get duplicate ingredients since that in one recipe i could use 1 lemon and in another have 3 lemons. I think that a ingredient could have a nutrition like kcal, lipids, etc. Thats why i think the amount of ingredients depends on the recipe and that s why i believe that could be a property of a recipe. – ePascoal Nov 20 '16 at 17:12
  • @MrMartin Then I thing you're missing an abstraction. You might need a `Quantity` class having Ingredient, unit and ammount as members. And your Reciept should have a List (not a map) of this Quantities. – Timothy Truckle Nov 20 '16 at 17:16

3 Answers3

2

I'm going to use HashMap instead of HashTable in this answer, as the former is now the standard approach.

You have to construct the "nested" HashMap separately, using the put method:

Map<Recipe.uom, Integer> amount = new HashMap<>();
amount.put(Recipe.uom.unit, 1);
Ingredient lemon = new Ingredient("Lemon", amount);

I do agree with Timothy that this is not really a good design. I'd personally create another class/interface Amount that handles this stuff.

Todd Sewell
  • 1,444
  • 12
  • 26
1

you need to use the put() method on tat other Hashtable too:

Map<Recipe.uom,Integer> mass new HashMap<>();
mass.put(uom.unit,1);
ingredient.put(new Ingredient("Lemon"),mass);
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
0

Well i suppose this is a fair answer and it works for me..

In fact, in this case we could consider a third class named Portion, which is an scalable approach since it has the advantage to add methods or more attributes if tomorrow we want a "complex" portion but in my case this dictionary seems to fit my needs. So a nested HashTable/HashMap should be define like this:

    Ingredient lemon = new Ingredient("lemon");
    Ingredient powder = new Ingredient("powder");

    HashMap<Ingredient,HashMap<uom,Integer>> portion = new HashMap<Ingredient,HashMap<uom,Integer>>();

    portion.put(lemon,new HashMap<uom, Integer>(){{put(uom.unit,1);}});
    portion.put(powder,new HashMap<uom, Integer>(){{put(uom.cup,2);}});

    Recipe lemon_cake = new Recipe("lemon cake","bla bla",portion,"Mr Martin");
ePascoal
  • 2,362
  • 6
  • 26
  • 44