Java快餐菜单(使用方法)

时间:2015-10-22 03:15:34

标签: java methods menu

我正在编写一个显示快餐菜单的程序。用户选择一个项目,然后输入该项目的数量,并且可以继续选择具有特定数量的项目,直到完成为止。我必须使用几种方法。我遇到的麻烦是计算一个总计。这是我的第一个Java类,所以我只知道基础知识。我把运行总计放在while循环中,所以它会继续添加一个小计,但是当我调用done()时,runningTotal为0.在使用多种方法时,跟踪运行总计的最佳方法是什么?此外,我对我的代码中的批评或清理持开放态度。谢谢。

import java.util.Scanner;

public class Menu {
    public double subTotal;
    public static double runningTotal;
    private static double itemPrice;
    static boolean ordering = true;
    static Scanner input = new Scanner(System.in);

    public static void menu() {
        System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
    }

    public static double itemPrice(int foodItem) {
        if (foodItem == 1) {
            // burger= $2.00
            System.out.println("You've ordered a burger");
            itemPrice = 2.00;
        }
        if (foodItem == 2) {
            // fries = $1.50
            System.out.println("You've ordered fries");
            itemPrice = 1.50;
        }
        if (foodItem == 3) {
            // soda = $1.00
            System.out.println("You've ordered a soda");
            itemPrice = 1.00;
        }
        quantity();
        return itemPrice;
    }

    public static double quantity() {
        System.out.println("Enter quantity");
        double quantity = input.nextDouble();
        subTotal(quantity, itemPrice);
        return quantity;
    }

    public static double subTotal(double quantity, double itemPrice) {
        double subTotal = quantity * itemPrice;
        System.out.println("Subtotal: " + subTotal);
        return subTotal;
    }

    public static void done(double runningTotal) {
        ordering = false;
        System.out.println(runningTotal);
        System.out.println("Enjoy your meal");
    }

    public static void main(String[] args) {
        int menuOption;
        int foodItem = 0;
        input = new Scanner(System.in);
        do {
            double runningTotal = 0;
            menu();
            menuOption = input.nextInt();
            switch (menuOption) {
            case 1:
                foodItem = 1;
                itemPrice(foodItem);
                break;
            case 2:
                foodItem = 2;
                itemPrice(foodItem);
                break;
            case 3:
                foodItem = 3;
                itemPrice(foodItem);
                break;
            case 4:
                done(runningTotal);
                break;
            default:
                System.out.println("Invalid option.");
            }
        } while (ordering);
        {
            subTotal(quantity(), itemPrice(foodItem));
            runningTotal = runningTotal + subTotal(quantity(), itemPrice(foodItem));
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您正在重置while循环中的double runningTotal=0;。另外,itemPrice返回的价格需要添加到runningTotal变量;

这是您的主要方法应该是这样的。用户完成后,您无需调用subTotal方法。

public static void main(String[] args) {
  int menuOption;
  int foodItem = 0;
  input = new Scanner(System.in);
  double runningTotal=0;
  do{
    menu();
    menuOption = input.nextInt();
    switch(menuOption){
      case 1:
        foodItem = 1;
        runningTotal += itemPrice(foodItem);
        break;
      case 2:
        foodItem = 2;
        runningTotal += itemPrice(foodItem);
        break;
      case 3:
        foodItem = 3;
        runningTotal += itemPrice(foodItem);
        break;
      case 4:
        done(runningTotal);
        break;
      default:
        System.out.println("Invalid option.");
    }
  } while(ordering);
  System.out.println("Total amount: " + runningTotal);
}

输出:

Welcome 
1. Burger ($2.00) 
2. Fries ($1.50)
3. Soda ($1.00) 
4. Done
1
You've ordered a burger
Enter quantity
2
Subtotal: 4.0
Welcome 
1. Burger ($2.00) 
2. Fries ($1.50)
3. Soda ($1.00) 
4. Done
2
You've ordered fries
Enter quantity
1
Subtotal: 1.5
Welcome 
1. Burger ($2.00) 
2. Fries ($1.50)
3. Soda ($1.00) 
4. Done
4
3.5
Enjoy your meal
Total amount: 3.5

答案 1 :(得分:0)

每次迭代都会将runningTotal重置为0。我附上了一个工作实例。我将总计算移动到您的subTotal方法,在该方法中添加每个subTotal。

import java.util.Scanner;
public class menu {
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
public static void menu(){
    System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
    if (foodItem == 1) {
        //burger= $2.00
        System.out.println("You've ordered a burger");
        itemPrice = 2.00;
    }
    if (foodItem == 2) {
        //fries = $1.50
        System.out.println("You've ordered fries");
        itemPrice = 1.50;
    }
    if (foodItem == 3) {
        //soda = $1.00
        System.out.println("You've ordered a soda");
        itemPrice = 1.00;
    }
    quantity();
    return itemPrice;
}
public static double quantity() {
    System.out.println("Enter quantity");       
    double quantity = input.nextDouble();
    subTotal(quantity, itemPrice);
    return quantity;
 }
public static double subTotal(double quantity, double itemPrice) {
    double subTotal = quantity*itemPrice;
    System.out.println("Subtotal: "+ subTotal);
    runningTotal += subTotal;
    return subTotal;
}
public static void done(){
    ordering = false;
    System.out.println(runningTotal);
    System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
    int menuOption;
    int foodItem = 0;
    input = new Scanner(System.in); 
    do{
        double runningTotal=0;
        menu();
        menuOption = input.nextInt();    
        switch(menuOption){
            case 1:
                foodItem = 1;
                itemPrice(foodItem);
                break;
            case 2:
                foodItem = 2;
                itemPrice(foodItem);
                break;
            case 3:
                foodItem = 3;
                itemPrice(foodItem);
                break;
            case 4:
                done();
                break;      
            default:
                System.out.println("Invalid option.");
        }

    } while(ordering); {
}
}
}

答案 2 :(得分:0)

这将带来确切的输出。

    import java.util.Scanner;
    public class Resteraunt 
    {
    public double subTotal;
    public static double runningTotal;
    private static double itemPrice;
    static boolean ordering = true;
    static Scanner input = new Scanner(System.in);
    static double j=0.0;
    public static void main(String[] args) {
    int menuOption;
    int foodItem = 0;
    input = new Scanner(System.in);
    double runningTotal=0;
    while(ordering) 
    {
    menu();
    menuOption = input.nextInt();
    switch(menuOption){
    case 1:
    foodItem = 1;
    runningTotal += ItemPrice(foodItem);
    break;
    case 2:
    foodItem = 2;
    runningTotal += ItemPrice(foodItem);
    break;
    case 3:
    foodItem = 3;
    runningTotal += ItemPrice(foodItem);
    break;
    case 4:
    done(runningTotal);
    break;
    default:
    System.out.println("Invalid option.");
    }
    } 
    System.out.println("Total amount: $" + runningTotal);
    }
    public static void menu() {
    System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda 
    ($1.00) \n4. Done");
    }

    public static double ItemPrice(int foodItem) {
    if (foodItem == 1) {
        // burger= $2.00
        System.out.println("You've ordered a burger");
        itemPrice = 2.00;
    }
    if (foodItem == 2) {
        // fries = $1.50
        System.out.println("You've ordered fries");
        itemPrice = 1.50;
    }
    if (foodItem == 3) {
        // soda = $1.00
        System.out.println("You've ordered a soda");
        itemPrice = 1.00;
    }
    quantity();
    return j;
}

public static double quantity() {
    System.out.println("Enter quantity");
    double quantity = input.nextDouble();
    subTotal(quantity, itemPrice);
    return quantity;
}

public static double subTotal(double quantity, double itemPrice) {
    double subTotal = quantity * itemPrice;
    System.out.println("Subtotal: $" + subTotal);
    j=subTotal;
    return subTotal;
}

public static void done(double runningTotal) {
    ordering = false;
    System.out.println("Enjoy your meal");
}
}