2016-10-26 65 views
-1

我試圖通過printMenu(cart, scnr).的Java ArrayList和類

但是,例如「A」>代碼打印
輸入項目名稱: 輸入項目描述:

採取項目名稱之前!!!!

預先感謝您。

這是主類:

import java.util.Scanner; 

    public class ShoppingCartManager { 
     public static void main(String[] args) { 

      Scanner scnr = new Scanner(System.in); 

      String customerName = "none"; 
      String currentDate = "January 1, 2016"; 

      System.out.println("Enter Customer's Name: "); 
      customerName = scnr.nextLine(); 

      System.out.println("Enter Today's Date: "); 
      currentDate = scnr.nextLine(); 

      ShoppingCart cart = new ShoppingCart(customerName, currentDate); 

      System.out.println("Customer Name: " + cart.getCustomerName()); 
      System.out.println("Today's Date: " + cart.getDate()); 
      printMenu(cart, scnr); 

     } 

     public static void printMenu(ShoppingCart cart, Scanner scnr) { 
      ItemToPurchase cartItems = new ItemToPurchase(); 

      char userChoice = '?'; 
       System.out.println(""); 

       System.out.print("MENU" + "\na - Add item to cart" + "\nd - Remove item from cart" 
            + "\nc - Change item quantity" + "\ni - Output items' descriptions" 
            + "\no - Output shopping cart" + "\nq - Quit"); 

       System.out.println(""); 
       System.out.println(""); 
       System.out.println(""); 

       System.out.println("Choose an option: "); 

       userChoice = scnr.next().charAt(0); 


       boolean rePrintMenu = true; 

       do { 

       switch (userChoice) { 

       // Add item to cart. 
          case 'a': 

           System.out.println("ADD ITEM TO CART"); 

           System.out.println("Enter the item name: "); 
           cartItems.setName(scnr.nextLine()); 

          System.out.println("\n\nEnter the item description: "); 
           cartItems.setDescription(scnr.nextLine()); 

           System.out.println("\n\nEnter the item price: "); 
           cartItems.setPrice(scnr.nextInt()); 

           System.out.println("Enter the item quantity: "); 
           cartItems.setQuantity(scnr.nextInt()); 

           break; 


       // Remove item from cart     
          case 'd': 

           System.out.println("REMOVE ITEM FROM CART"); 
           System.out.println("Enter name of item to remove: "); 

           cart.removeItem(scnr.nextLine()); 

           break; 


       // Change item quantity   
          case 'c': 

           //ItemToPurchase newQuantity = new ItemToPurchase(); 

           System.out.println("CHANGE ITEM QUANTITY"); 
           System.out.println("Enter the item name: "); 
           // cart.modifyItem(scnr.nextLine()); 

           break; 


       // Output items' descriptions  
          case 'i': 

           System.out.println("OUTPUT ITEMS' DESCRIPTIONS"); 
           System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate()); 

           System.out.println(""); 
           System.out.println(""); 


           System.out.println("Item Descriptions"); 

           System.out.println(cart.printDescriptions()); 

           System.out.println(); 

           break; 

          case 'o': 
           System.out.println("OUTPUT SHOPPING CART"); 
System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate()); 
           System.out.println(cart.getNumItemsInCart()); 

           System.out.println(""); 
           System.out.println(""); 

           System.out.println(cart.printTotal()); 

           break; 

          case 'q': 
           rePrintMenu = false; 
           System.exit(0); 

           break; 

          } 
         } while (rePrintMenu);  
         return; 
      } 

    } 
enter code here 

這是我的購物車!

但我不知道爲什麼它不適用於main。另外,如何通過名稱刪除項目?

我想這個邏輯,但沒有奏效:

對於(){ 如果()

其他{}

我應該有布爾標誌?或計數?我迷路了。

import java.util.ArrayList; 

public class ShoppingCart { 

    private String customerName = "none"; 
    private String currentDate = "January 1, 2016"; 
    private ArrayList<ItemToPurchase> cartItems = new ArrayList<ItemToPurchase>(); 


    public ShoppingCart() { 

    } 


    public ShoppingCart(String userName, String userDate) { 
     customerName = userName; 
     currentDate = userDate; 
     return; 
    } 


    public String getCustomerName() { 
     return customerName; 
    } 


    public String getDate() { 
     return currentDate; 
    } 


    public void addItem(ItemToPurchase item) { 
     cartItems.add(item); 
     return; 
    } 

//fixMe  
    public void removeItem(String itemName) { 
     return; 
    } 
// fixMe 
     public void modifyItem(ItemToPurchase item) { 
      return; 
     } 

    public int getNumItemsInCart() { 
     int totalNumOfItem = 0; 
     for (int i = 0; i < cartItems.size(); ++i) { 

      totalNumOfItem += cartItems.get(i).getQuantity(); 
     } 
     return totalNumOfItem; 
    } 


    public int getCostOfCart() { 
     int cartCost = 0; 

     for (int i = 0; i < cartItems.size(); ++i) { 
      cartCost += cartItems.get(i).getQuantity(); 
      cartCost *= cartItems.get(i).getPrice(); 
     } 

     return cartCost; 
    } 


    public int printTotal() { 
     int total= 0; 

      for (int i = 0; i < cartItems.size(); ++i) { 
       if (cartItems.size()> 0) { 

       cartItems.get(i).printItemCost(); 

        total = (cartItems.get(i).getQuantity()) * (cartItems.get(i).getPrice()); 
       System.out.println("Total: $" + total); 
      } 


     else { 
      System.out.println("SHOPPING CART IS EMPTY"); 



    }} 
      return total; 
} 

    public String printDescriptions() { 
     String descriptions = ""; 
     for (int i = 0; i < cartItems.size(); ++i) { 
      descriptions = cartItems.get(i).printItemDescription(); 
     } 

      return descriptions; 
    } 

} 

這是ItemToPurchase類。

public class ItemToPurchase { 

    private String itemName = "none"; 
    private String itemDescription = "none"; 

    private int itemPrice=0; 
    private int itemQuantity=0; 


    public ItemToPurchase() { 

    } 


    public void setName(String userItemName) { 
     itemName = userItemName; 
     return; 
    } 


    public void setDescription(String userItemDescription) { 
     itemDescription = userItemDescription; 
     return; 
    } 


    public void setPrice(int userItemPrice) { 
     itemPrice = userItemPrice; 
     return; 
    } 


    public void setQuantity(int userItemQuantity) { 
     itemQuantity = userItemQuantity; 
     return; 
    } 


    public String getName() { 
     return itemName; 
    } 


    public String getDescription() { 
     return itemDescription; 
    } 


    public int getPrice() { 
     return itemPrice; 
    } 


    public int getQuantity() { 
     return itemQuantity; 
    } 


    public void printItemCost() { 
     System.out.print(itemName); 
     System.out.print(" " + itemQuantity + " @ $" + itemPrice); 
     System.out.println(" = $" + (itemQuantity * itemPrice)); 

     return ; 
    } 


    public String printItemDescription() { 
     String descriptionForItem = itemName + ": " + itemDescription; 
     System.out.println(descriptionForItem); 
     return descriptionForItem; 
    } 

} 
+0

嘗試給第一個輸入後的scanner.nextLine()... –

+0

同樣的問題。然後nextLine。然而,我試過使用兩臺掃描儀,它的工作原理,但兩臺掃描儀的行爲如何? – 12uedhdksh24

+0

檢查此鏈接..你會明白:http://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input –

回答

0

的問題是,nextInt()不消耗「\ n」,所以nextLine下一個()調用消耗它,然後它等待讀取輸入。

你需要在調用nextLine()之前使用'\ n';

有關詳細信息:

Java Scanner doesn't wait for user input

0

代碼:

userChoice = scnr.next().charAt(0); 

下一個()部分不讀所有的令牌。如果您調試代碼,您將看到輸入項目名稱爲空字符串「」 - scnr.next()的一部分。

改用此:

String userChoiceString = scnr.nextLine(); 
userChoice = userChoiceString.charAt(0); 

更多閱讀https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()