Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print out the contents of a HashMap in a certain format?
    primarykey
    data
    text
    <p>I'm not entirely sure how I would do this, here is my code:</p> <pre><code>public class PizzaMenu { static Map&lt;String,Pizza&gt; namedPizzas= new HashMap&lt;String,Pizza&gt;(); public static void main(String[] args) { } public static void addItem(String name, Pizza pizza) { namedPizzas.put(name, pizza); } public String printMenu() { /* String menuString=""; for (Every menu item) { //Add name of menu item to menuString with carriage return //Add details of menu item (pizza.getInfo();) to menuString } */ //return menuString } } </code></pre> <p>I would then call <code>System.out.println(PizzaMenu.printMenu())</code> in another class. The sort of format I'm hoping to achieve is as follows:</p> <pre><code>/* * PizzaName * Details * * Next PizzaName in menu * Details * * Next PizzaName in menu * Details * * * */ </code></pre> <p>Am I maybe using the wrong data structure for this type of operation or is there a way of achieving this?</p> <p>Here is the structure of the Pizza class (apologies for poor formatting):</p> <pre><code>public class Pizza { private double cost; private Boolean veg; private PizzaBase base; private List&lt;PizzaTopping&gt; toppings = new ArrayList&lt;PizzaTopping&gt;(); public Pizza(PizzaBase base, PizzaTopping topping) //Constructor for pizza with 1 topping { setBase (base); toppings.add(topping); } public Pizza(PizzaBase base, PizzaTopping topping, PizzaTopping topping2) //Constructor for pizza with 2 toppings { setBase (base); toppings.add(topping); toppings.add(topping2); } public Pizza(PizzaBase base, PizzaTopping topping, PizzaTopping topping2, PizzaTopping topping3) //Constructor for pizza with 3 toppings { setBase (base); toppings.add(topping); toppings.add(topping2); toppings.add(topping3); } public double getCost() { return cost; } public void setCost(double cost) { this.cost = cost; } public PizzaBase getBase() { return base; } public void setBase(PizzaBase base) { this.base = base; } public List&lt;PizzaTopping&gt; getToppings() { return this.toppings; } public String getToppingsInfo() { String toppingInfo = "\n"; PizzaTopping t; for (int i = 0; i&lt;getToppings().size();i++) { t = toppings.get(i); toppingInfo=toppingInfo+t.getInfo(); } return toppingInfo; } public Boolean getVeg() { return veg; } public void setVeg(Boolean veg) { this.veg = veg; } public double calculateCost() { PizzaTopping p; //Loop through all ingredients and add their costs to total cost for (int i = 0; i&lt;toppings.size();i++) { p = toppings.get(i); cost+=p.getCost(); } cost+=base.getCost(); //Add pizza base cost to total cost return cost; } //Check if pizza is vegetarian depending upon its ingredients public Boolean isVeg() { Boolean toppingCheck =true; Boolean baseCheck = true; PizzaTopping t; //Temporary value used to stored toppings being compared in for loop //Check each topping and check if it's suitable for vegetarians for (int i =0; i&lt;toppings.size();i++) { while (toppingCheck == true) { t = toppings.get(i); if (t.getVeg()==false) { toppingCheck = false; } } } //Check base to see if it's suitable for vegetarians if (getBase().getVeg()==false) { baseCheck = false; } //Return value depending on if all ingredients are suitable for vegetarians if (toppingCheck == true &amp;&amp; baseCheck == true) { return true; } else return false; } public String getInfo() { String vegInfo; if (this.isVeg()==true) { vegInfo = "Yes"; } else vegInfo ="No"; return String.format("Toppings:%s\n"+"Base:\n%s"+"\nTotal Cost:\t£%.2f"+"\nSuitable for vegetarians: %s", getToppingsInfo(), getBase().getInfo(), calculateCost(), vegInfo); //Return list of toppings, Total Price, vegetarian } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload