我将此输出到一个类似收据的文件,但我不确定是否有更好的方法来重写成本以包含名称。
public double getToppingCost()
{
double toppingCost = 0.0;
if (creamCheese.isSelected())
toppingCost += CREAM_CHEESE;
if (butter.isSelected())
toppingCost += BUTTER;
if (peachJelly.isSelected())
toppingCost += PEACH_JELLY;
if (blueberryJam.isSelected())
toppingCost += BLUEBERRY_JAM;
if (nutella.isSelected())
toppingCost += NUTELLA;
return toppingCost;
}
编辑:这是我的声明。我需要编写方法,以便我可以将名称和名称输出到单独的文件,有点像收据。
public final double CREAM_CHEESE = 0.50;
public final double BUTTER = 0.25;
public final double PEACH_JELLY = 0.75;
public final double BLUEBERRY_JAM = 0.75;
public final double NUTELLA = 0.50;
public final String CHEESE = "Cream Cheese";
public final String BUT = "Butter";
public final String JELLY = "Peach Jelly";
public final String JAM = "Blueberry Jam";
public final String NUT = "Nutella";
答案 0 :(得分:0)
toppings.getSelected().stream()
.map(t -> t.getName() + " - " + t.getPrice())
.collect(Collectors.joining("\n"));