gpt4 book ai didi

java - 如何以某种格式打印出HashMap的内容?

转载 作者:行者123 更新时间:2023-11-30 04:06:51 26 4
gpt4 key购买 nike

我不完全确定如何做到这一点,这是我的代码:

public class PizzaMenu
{
static Map<String,Pizza> namedPizzas= new HashMap<String,Pizza>();

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
}
}

然后我会在另一个类中调用System.out.println(PizzaMenu.printMenu())。我希望实现的格式如下:

/*
* PizzaName
* Details
*
* Next PizzaName in menu
* Details
*
* Next PizzaName in menu
* Details
*
*
*
*/

对于这种类型的操作,我是否使用了错误的数据结构,或者有没有办法实现这一点?

这是 Pizza 类的结构(对格式不当表示歉意):

public class Pizza
{
private double cost;
private Boolean veg;
private PizzaBase base;
private List<PizzaTopping> toppings = new ArrayList<PizzaTopping>();

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<PizzaTopping> getToppings()
{
return this.toppings;
}

public String getToppingsInfo()
{
String toppingInfo = "\n";
PizzaTopping t;
for (int i = 0; i<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<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<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 && 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

}

}

最佳答案

试试这个:

String menuString="";
for (Map.Entry<String, Pizza> pizzaItem : namedPizzas.entrySet()) {
menuString += pizzaItem.getKey() + "\n";
menuString += "\t" + pizzaItem.getValue().getInfo() + "\n\n";
}

关于java - 如何以某种格式打印出HashMap的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com