[Solved] How can I define something and save it in HashMap


You can define Ingredients as a class below:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Ingredients
{
  private Map<String, Double> aIngredientTypeToCost = new HashMap<>();



  public void put(String theType, Double theCost)
  {
    aIngredientTypeToCost.put(theType, theCost);
  }



  public Set<String> getAllIngredientsType()
  {
    return aIngredientTypeToCost.keySet();
  }

}

then in the main class you can create a map of type pizza to Ingredients.

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class pizza
{

  public static void main(String args[])
  {
    Map<String, Ingredients> aMap = new HashMap<>();

    Ingredients aHawaiPizza = new Ingredients();
    aHawaiPizza.put("Salami", 3.50);
    aHawaiPizza.put("Cheeze", 1.50);

    aMap.put("Hawai", aHawaiPizza);

    Ingredients aXyzPizza = new Ingredients();
    aXyzPizza.put("Pickles", 0.50);
    aXyzPizza.put("Thuna", 2.50);

    aMap.put("aXyzPizza", aXyzPizza);

    for( Entry<String, Ingredients> aType : aMap.entrySet() )
    {
      String aPizza = aType.getKey();
      Ingredients aIngredients = aType.getValue();

      System.out.println("A pizza " + aPizza + " has ingredients " + aIngredients.getAllIngredientsType());
    }

  }
}

0

solved How can I define something and save it in HashMap