为配料增加价格

时间:2017-04-16 12:53:05

标签: java

所以我有一项任务,我已经为它创建了大部分代码,但我无法弄清楚如何为顶部添加价格。这不是我的主要代码,这是比萨饼大小的价格,但我还需要为配料增加更多的价格。我需要添加两个单独的价格,一个用于中型披萨,一个用于大披萨。

package assignment1;

public class Pizza {

private int diam;
private int numOfPizza;
private double price;
private String toppings;

Pizza(int size, String input) {
 diam = size;
 toppings = input;

}

public int getDiameter(){
    return diam;
}
public int getPizzaCount(){
    return numOfPizza;
}
public double getPrice(){
    return price;
}
public String getToppings(){
    return toppings;
}
public void setDiameter(int size){
if (size == 12)
    diam = 12;
else if ( size == 16)
    diam = 16;
else
    diam = 0;
}    
public void setPizzaCount(int pizzaCount){
    numOfPizza = pizzaCount;
}
public void setPrice(double total){
    price = total;
}  
public void setToppings(String input){
    if ("Ham".equalsIgnoreCase(input))
        toppings = "Ham";
    else if ("Mozarella".equalsIgnoreCase(input))
        toppings = "Mozarella" ;
    else if ("Olives".equalsIgnoreCase(input))
        toppings = "Olives";
    else if ("Pineapple".equalsIgnoreCase(input))
        toppings = "Pineapple";
    else if ("Spinach".equalsIgnoreCase(input))
        toppings = "Spinach";
    else 
        toppings = "Plain";
}
private double calculatePrice(int size, String input){
    int total;
    if(!(toppings).equalsIgnoreCase("plain")) {
        total = 1;
    } else {
        total = 0;
    }

    if(size == 12) {
        total += 4.00;
    }

    else if(size == 16) {
        total += 5.00;
    }

    return total;
} 
    public String toString(){
        String pizzaString ="You have ordered a "+diam + " inch pizza with 
"+toppings +" toppings and a price of £"+ calculatePrice(diam, toppings);
        return pizzaString;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建这样的东西,将价格设置为每个顶部。首先初始化总数,然后根据您添加的浇头增加总数。

class largeNum
{

public:
     void insertFront(int value);  // implemented via value.insert() and value.begin()

private:
    std::vector<int> value;
};