比萨订购计划

时间:2016-03-16 19:56:39

标签: java methods constructor

public class PizzaEx {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   char letter;
   String input;
   int sizeD;
   int pizzaCount=1;
   Pizza pieOne;

   do{
       sizeD = getValidSize();
       input = JOptionPane.showInputDialog(null, "What type of topping do you wish to order? " +
                                                 "\n Green Pepper" +
                                                 "\n Mushroom"+
                                                 "\n Sausage"+
                                                 "\n Pepperoni"+
                                                 "\n Plain");

       pieOne = new Pizza(sizeD, input);
       System.out.println(pieOne);
       System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() +"."+"\n");
       input = JOptionPane.showInputDialog(null, "Do you wish to continue?\n "+
                                                 "'y' or 'Y' for YES\n"+
                                                 "'n' or 'N' for NO\n");
       letter = input.charAt(0);

           pizzaCount = pizzaCount +1;
   }
   while (letter == 'Y'|| letter == 'y');

   System.exit(0);
}

private static int getValidSize()
{
    int d;
    String input;
    do{
        input = JOptionPane.showInputDialog(null, "What size of pizza do you wish to order? "+
                                                  "\n 9 inch"+
                                                  "\n 12 inch"+
                                                  "\n 16 inch");
        d = Integer.parseInt(input);
    } while (!(d==9 || d==12 || d==16));
    return d;
}

所以上面是我的主要课程

public class Pizza {



    private int diameter;
    private int numOfPizza;
    private double price;
    private String tops;

Pizza(int sizeD, String input) {
    diameter = sizeD;
    tops = input;

}

public int getDiameter(){
    return diameter;
}

/**
 *
 * @param pizzaCount
 * @return
 */
public int getPizzaCount(){
    return numOfPizza;
}
public double getPrice(){
    return price;
}
public String getToppings(){
    return tops;
}
public void setDiameter(int sizeD){
    if (sizeD == 9)
        diameter = 9;
    else if ( sizeD == 12)
        diameter = 12;
    else if (sizeD == 15)
        diameter = 15;
    else
        diameter = 0;
    }    
public void setPizzaCount(int pizzaCount){
    numOfPizza = pizzaCount;
}
public void setPrice(double total){
    price = total;
}
public void setToppings(String input){
    if ("green pepper".equalsIgnoreCase(input))
        tops = "Green Pepper";
    else if ("mushroom".equalsIgnoreCase(input))
        tops = "Mushroom";
    else if ("sausage".equalsIgnoreCase(input))
        tops = "Sausage";
    else if ("pepperoni".equalsIgnoreCase(input))
        tops = "Pepperoni";
    else 
        tops = "Plain";
}
private double calculatePrice(int sizeD, String input){
    double total;
    if (sizeD == 9 && (tops).equalsIgnoreCase("plain")) 
        total = 5.95;
    else if (sizeD == 9)
        total = 6.95;
    else if (sizeD == 12  && (tops).equalsIgnoreCase("plain") )
        total = 7.95;
    else if (sizeD == 12)
        total = 8.95;
    else if (sizeD == 16 && (tops).equalsIgnoreCase("plain"))
        total = 9.95;
    else if (sizeD == 16)
        total = 10.95;
    else 
        total = 0.0;

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

当我进行打印输出时,即使我设置了pizzaCount = 1,它仍然说披萨的数量是= 0。此外,当它要求打顶时,如果我输入除有效顶部选项{"green peppers", "mushroom", "sausage", "pepperoni", "plain"}之外的任何字符串,它会将字符串计为顶部,并且当它应该是不是{"green peppers", "mushroom", "sausage", "pepperoni"}的任何内容时将收取顶部的费用应该考虑"plain"

这不是家庭作业或测试问题。这是我的教授发布的一些额外练习,不是为了成绩。我只想要一些帮助来澄清为什么String tops没有被分配方法setToppings()正在调用的值。

6 个答案:

答案 0 :(得分:1)

您始终获得0 getNumOfPizza()的原因是因为您永远不会增加int numOfPizza,只会在pizzaCount中增加main

至于topping,即使您输入了无效的字符串,也会因为您在calculatePrice中的逻辑而导致填充费用的原因是!equalsIgnoreCase("plain") 。换句话说,除"plain"之外的任何内容都将被视为顶部。事实上,这种方法的逻辑是不必要的复杂,我建议你简化一些if语句:

private double calculatePrice(int sizeD, String input){
        if(!(tops).equalsIgnoreCase("plain")) {
            total = 1;
        } else {
            total = 0;
        }

        if(sizeD == 9) {
            total += 5.95;
        }

        else if(sizeD == 12) {
            total += 7.95;
        }

        else if(sizeD == 16) {
            total += 9.95;
        }

        return total;
    }

答案 1 :(得分:0)

您的班级Pizza包含您使用private int numOfPizza;访问的字段pieOne.getPizzaCount()。由于该字段尚未已初始化(并且它是原始int ),因此其默认值为0。一种可能的解决方法,

private int numOfPizza = 1;

确保您正在考虑您感兴趣的计数;本地countPizza计数。修复后,您还应该更改

pizzaCount = pizzaCount +1;

类似

pizzaCount += pieOne.getPizzaCount();

答案 2 :(得分:0)

您有以下构造函数:

Pizza(int sizeD, String input) {
    diameter = sizeD;
    tops = input;

}

正如您所看到的,它不会从setToppings运行您的逻辑。

它也没有设置numOfPizza,也没有调用setPizzaCount()方法。所以这个类变量仍为0

答案 3 :(得分:0)

您似乎有两个不同的字段来容纳比萨饼的数量。披萨类应仅保存有关个别比萨饼的数据,而另一类应保存有关比萨饼数量的数据。在您的主类中,您将pizzaCount初始化为1,但然后尝试从pizza numOfPizza字段中获取数字。

编辑另外,在另一张纸条上,您的主要课程中有太多内容。你应该抽出一些东西并把它放在方法中。

答案 4 :(得分:0)

打印时:

System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() +"."+"\n");

您正在访问 pieOne 。让我们检查初始化:

Pizza pieOne = new Pizza(sizeD, input);

其中sizeD存储披萨大小(如9),input存储字符串。

函数pieOne.getPizzaCount()pieOne内检查并返回numOfPizza;

public int getPizzaCount(){
    return numOfPizza;
}

但是,由于您实际上从未将该值存储在对象中,因此它将返回零!

您可以在打印之前打电话给setPizzaCount()

希望它有所帮助。

答案 5 :(得分:0)

由于setToppings()方法已声明但从未被调用,因此每次用户输入的内容都会返回toppings的字符串。你需要在" tops"变量让它起作用。实际上,程序完全跳过该方法。

以下是您可以在程序中调用该方法的一种方法:

public String toString()
{
    //method call
    setToppings(tops);
    String pizzaString = "You have ordered a " + diameter + " inch pizza with " + tops + " toppings and a price of $" + calculatePrice(diameter, tops);
    return pizzaString;
}

}

你的披萨数量问题是同样的情况。从未调用过setPizzaCount()方法,因此从未设置过numOfPizza。由于int变量未实例化时自动默认为0,因此每次都会得到0值。此外,您可能需要考虑在添加一个比萨饼时使用增量(++)运算符。它的存在是为了帮助您更轻松地编写代码以及让其他人在增加数字时理解代码。

以下是一个例子:

do
    {
        sizeD = getValidSize();
        input = JOptionPane.showInputDialog(null, "What type of topping do you wish to order? " +
                "\n Green Pepper" +
                "\n Mushroom" +
                "\n Sausage" +
                "\n Pepperoni" +
                "\n Plain");
        pieOne = new Pizza(sizeD, input);
        //method call
        pieOne.setPizzaCount(pizzaCount);
        System.out.println(pieOne);
        System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() + "." + "\n");
        input = JOptionPane.showInputDialog(null, "Do you wish to continue?\n " +
                "'y' or 'Y' for YES\n" +
                "'n' or 'N' for NO\n");
        letter = input.charAt(0);
        pizzaCount++;
    }