我有代码工作来获得成本,但我怎么写这个来获得名称?

时间:2015-05-03 21:13:25

标签: java

我将此输出到一个类似收据的文件,但我不确定是否有更好的方法来重写成本以包含名称。

    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";

1 个答案:

答案 0 :(得分:0)

toppings.getSelected().stream()
            .map(t -> t.getName() + " - " + t.getPrice())
            .collect(Collectors.joining("\n"));