2015-10-22 65 views
-2

我寫了一個程序來模擬在快餐店點餐。用戶輸入項目,然後輸入數量,並重復,直到完成。它顯示項目數量,小計,銷售稅(0.09%),然後顯示總額。包含輸入檢查的最佳方式是什麼?在main()中,如果用戶沒有輸入整數,菜單將重新顯示,如果他們選擇了一個超出範圍的整數,它將顯示「無效輸入」。對quantity()方法進行輸入檢查的最佳方法是什麼? 我試過如果(input.hasNextDouble)數量()方法內......但程序將不會繼續運行。Java快餐菜單(帶方法)

import java.text.DecimalFormat; 
import java.text.NumberFormat; 
import java.util.Scanner; 
public class Menu { 
public double subTotal; 
public static int numberOfItems; 
public static double runningTotal; 
public static String nameOfItem; 
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(foodItem, nameOfItem); 
    return itemPrice; 
} 
public static double quantity(double foodItem, String nameOfItem) { 
    System.out.println("Enter quantity of "+ nameOfItem); 
    double quantity = input.nextDouble(); 
    subTotal(itemPrice, quantity); 
    return quantity; 
} 
public static double subTotal(double itemPrice, double quantity) { 
    double subTotal = itemPrice*quantity; 
    System.out.println("Subtotal: "+ subTotal); 
    runningTotal += subTotal; 
    numberOfItems += quantity; 
    return subTotal; 
} 
public static double salesTax(double runningTotal) { 
    double salesTax = runningTotal*.09; 
    return salesTax; 
} 
public static void done(){ 
    NumberFormat f = new DecimalFormat("#,##0.00"); 
    ordering = false; 
    System.out.println("Number of items: "+ numberOfItems+ 
     "\tSubtotal: $"+ f.format(runningTotal)+ 
     "\nSales tax: $"+ f.format(salesTax(runningTotal))+ 
     "\tTotal: $"+ f.format((runningTotal+salesTax(runningTotal)))); 
} 
public static void main(String[] args) { 
    int menuOption; 
    int foodItem = 0; 
    input = new Scanner(System.in); 
    double runningTotal=0; 
    int numberOfItems =0; 
    do{ 
     menu(); 
     if (input.hasNextInt()) { //run if user enters an int 
      menuOption = input.nextInt(); 
      switch(menuOption){ //only int's 1-4 are valid 
      case 1: 
       foodItem = 1; 
       nameOfItem = "burgers"; 
       itemPrice(foodItem); 
       break; 
      case 2: 
       foodItem = 2; 
       itemPrice(foodItem); 
       nameOfItem = "fries"; 
       break; 
      case 3: 
       foodItem = 3; 
       itemPrice(foodItem); 
       nameOfItem = "sodas"; 
       break; 
      case 4: 
       done(); 
       break;  
      default: 
       System.out.println("Invalid option.");  
     } 
     } 
     else { //if user doesn't enter an int, clear input 
      input.next(); 
     } 
    } while(ordering); 
} 
} 
+3

是否有一個理由,爲什麼你決定後另一個呢? http://stackoverflow.com/questions/33272802/java-fast-food-menu-using-methods –

+0

@sparky你是對的。我不知道如何處理這些帖子。 – YoungHobbit

+0

我試圖刪除它......但它說我不能。對不起,這是我在stackoverflow上的第一天。 –

回答

0

你可以這樣做:

while(true) { 
     try { 
      Scanner input = new Scanner(System.in); 
      System.out.println("Enter quantity: "); 
      //System.out.println("Enter quantity of "+ nameOfItem); 
      double quantity = input.nextDouble(); 
     } catch (InputMismatchException e) { 
      // TODO Auto-generated catch block 
      System.out.println("Invalid Input . Try once again"); 
      continue; 
     } 
     } 

N.B:雖然它是一個不好的做法捕捉運行時異常