So I have created a class that creates a map of keys that are string of names of dishes, each key has a set of string values that are the ingredients within the dish. I have managed to get all keys-value pairs to print, but now I want a method that takes a string as an argument and then if that string matches a key, print out the key-value pair, and if not, display an message saying no such key was found.
Here is my attempt:
public void printMapValue(String a) {
if (recipes.containsKey(a)) {
System.out.println("The ingredients for " + a + " Are: " + ingredients);
} else {
System.out.println("That string does not match a record");
}
}
Here is my full class code so far as well, which all works as intended up until the printMapVale()
method
public class Recipe {
Map<String, Set<String>> recipes;
public Recipe() {
this.recipes = new HashMap<>();
}
public void addData() {
Set<String> ingredients = new HashSet<>();
ingredients.add("Rice");
ingredients.add("Stock");
recipes.put("Risotto", ingredients);
ingredients = new HashSet<>();
ingredients.add("Bun");
ingredients.add("Patty");
ingredients.add("Cheese");
ingredients.add("Lettuce");
recipes.put("Burger", ingredients);
ingredients = new HashSet<>();
ingredients.add("Base");
ingredients.add("Sauce");
ingredients.add("Cheese");
ingredients.add("Pepperoni");
recipes.put("Pizza", ingredients);
}
public void printMap() {
for (String recipeKey : recipes.keySet()) {
System.out.print("Dish : " + String.valueOf(recipeKey) + " Ingredients:");
for (String dish : recipes.get(recipeKey)) {
System.out.print(" " + dish + " ");
}
System.out.println();
}
}
public void printMapValue(String a) {
if (recipes.containsKey(a)) {
System.out.println("The ingredients for " + a + " Are: " + recipes.keySet(a));
} else {
System.out.println("That string does not match a record");
}
}
}