2017-10-28 22 views
-2

こんにちは私のコードを稼働させるのは難しいですが、ほとんどの機能は動作していますが、メニューが分かれば問題になります。私がしようとしているのは次のとおりです。Javaのケース内で有効な選択を求めるプロンプトが表示され続ける

printMenu()メソッドを実装します。 printMenu()にはShoppingCartパラメータがあり、ショッピングカートを操作するためのオプションメニューが出力されます。各オプションは1文字で表されます。メソッド内でメニューを構築して出力します。

無効な文字が入力された場合は、引き続き有効な選択を求めるプロンプトが表示されます。ヒント:他のオプションを実装する前にQuitを実装してください。 main()メソッドのprintMenu()を呼び出します。ユーザーがqを入力して終了するまで、メニューの実行を続行します。

私はメニューの部分はすでに完成していますが、それは連続的にループします。コードは、メニューセクションの下にある: パブリッククラスItemToPurchase {

private String itemName; 
private int itemPrice; 
private int itemQuantity; 
private String itemDescription; 
public ItemToPurchase(){ 
    this.itemName = "none"; 
    this.itemPrice = 0; 
    this.itemQuantity = 0; 
    this.itemDescription = "none"; 

}

public ItemToPurchase(String itemName, int itemPrice, int itemQuantity, String 
itemDescription) 
{ 
    this.itemName = itemName; 
    this.itemPrice = itemPrice; 
    this.itemQuantity = itemQuantity; 
    this.itemDescription = itemDescription; 
} 


public String getName(){ 
    return itemName; 
} 

public void setName(String itemName){ 
    this.itemName = itemName; 
} 

public int getPrice(){ 
    return itemPrice; 
} 

public void setPrice(int itemPrice){ 
    this.itemPrice = itemPrice; 
} 

public int getQuantity(){ 
    return itemQuantity; 
} 

public void setQuantity(int itemQuantity){ 
    this.itemQuantity = itemQuantity; 
} 

public String getDescription(){ 
    return itemDescription; 
} 

public void setDescription(String itemDescription){ 
    this.itemDescription = itemDescription; 
} 


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

    // Prints the item name 
// and description 
public void printItemDescription(){ 
    System.out.println(this.itemName + ": "+ this.itemDescription); 
     } 



    } 


     **ShoppingCart** 
    import java.util.ArrayList; 
public class ShoppingCart { 
private String customerName; 
private String currentDate; 
private ArrayList<ItemToPurchase> cartItems = new ArrayList<>(); 

public ShoppingCart(){ 
    customerName = "none"; 
    currentDate = "1 January, 2016"; 
    //cartItemToPurchases = new ArrayList<ItemToPurchase>(); 
} 

public ShoppingCart(String customerName, String currentDate){ 
    this.customerName = customerName; 
    this.currentDate = currentDate; 
    //cartItemToPurchases = new ArrayList<ItemToPurchase>(); 
} 

public String getCustomerName(){ 
    return customerName; 
} 

public String getDate(){ 
    return currentDate; 
} 
public void setCustomerName(String customerName){ 
this.customerName = customerName; 
} 

public void setDate(String currentDate){ 
this.currentDate = currentDate; 
} 

public void addItem(ItemToPurchase itemToPurchaseToPurchase){ 
    if(cartItems != null){ 
     cartItems.add(itemToPurchaseToPurchase); 
    } 
} 

public void removeItem(String itemName){ 
    if(cartItems != null){ 
    for(ItemToPurchase itemToPurchase : cartItems){ 
    if(itemToPurchase.getName().equals(itemName)){ 
    cartItems.remove(itemToPurchase); 
       return; 
      } 
     } 
     System.out.println("Item not found in cart. Nothing removed."); 
    } 
} 

public void modifyItem(ItemToPurchase itemToPurchaseToPurchase){ 
    boolean itemFound = false; 
    if(cartItems != null){ 
    for(ItemToPurchase item : cartItems){ 
    if(item.getName().equals(itemToPurchaseToPurchase.getName())){ 
     itemFound = true; 

     if(!itemToPurchaseToPurchase.getDescription().equals("none")){ 
      item.setDescription(itemToPurchaseToPurchase.getDescription()); 
    } 
     if(itemToPurchaseToPurchase.getQuantity() != 0){ 
      item.setQuantity(itemToPurchaseToPurchase.getQuantity()); 
       } 
     if(itemToPurchaseToPurchase.getPrice() != 0){ 
      item.setPrice(itemToPurchaseToPurchase.getPrice()); 
       } 
      } 
     } 
     if(itemFound != true){ 
      System.out.println("Item not found in cart. Nothing modified."); 
     } 
    } 
    } 

    public int getNumItemsInCart(){ 
    int totalQuantity = 0; 
    if(cartItems != null){ 
    for(ItemToPurchase itemToPurchase : cartItems){ 
     totalQuantity += itemToPurchase.getQuantity(); 
     } 
    } 
    return totalQuantity; 
    } 

public int getCostOfCart(){ 
    int totalCostOfCart = 0; 
    if(cartItems != null){ 
    for(ItemToPurchase itemToPurchase : cartItems){ 
     totalCostOfCart += (itemToPurchase.getQuantity()* 
itemToPurchase.getPrice()); 
     } 
    } 
    return totalCostOfCart; 
} 

public void printTotal(){ 
    if(cartItems == null || cartItems.size() == 0){ 
     System.out.println("SHOPPING CART IS EMPTY."); 
     return; 
    } 
    System.out.println(customerName+"\'s"+" Shopping Cart - "+currentDate); 
    System.out.println("Number of Items: "+getNumItemsInCart()); 
    System.out.println();    
    for(ItemToPurchase itemToPurchase : cartItems){ 
     itemToPurchase.printItemCost();   
    } 
    System.out.println(); 
    System.out.println("Total: $"+getCostOfCart()); 
} 

public void printDescriptions(){ 
    if(cartItems == null || cartItems.size() == 0){ 
     System.out.println("SHOPPING CART IS EMPTY."); 
     return; 
    } 
    System.out.println(customerName+"\'s"+" Shopping Cart - "+currentDate); 
    System.out.println("\nItem Descriptions"); 
    for(ItemToPurchase itemToPurchase : cartItems){ 
     itemToPurchase.printItemDescription(); 
    } 
} 


} 
     **ShoppingCartManager** 
     import java.io.BufferedReader; 
     import java.io.IOException; 
     import java.io.InputStreamReader; 
     public class ShoppingCartManager{ 
     private static BufferedReader br = new BufferedReader(new 
     InputStreamReader(System.in)); 
     // Prints a menu of options 
    public static void printMenu(ShoppingCart shoppingCart) 
       throws IOException 
     { 
     char choice = ' '; 
     while (true) 
    { 
     System.out.println("\nMENU"); 
     System.out.println("a - Add item to cart"); 
     System.out.println("d - Remove item from cart"); 
     System.out.println("c - Change item quantity"); 
     System.out.println("i - Output items' descriptions"); 
     System.out.println("o - Output shopping cart"); 
     System.out.println("q - Quit"); 
     System.out.println(""); 
     System.out.println("Choose an option:"); 
     choice = br.readLine().charAt(0); 

     if (!((choice == 'a') || (choice == 'd') || 
       (choice == 'c') || (choice == 'i') 
       || (choice == 'o')|| (choice == 'q'))) 
     { 
      System.out.println("Choose an option:"); 
     choice = br.readLine().charAt(0); 
     continue; 
     } 
     else 
     { 
      switch (choice) 
      { 

      case 'a': 
       System.out.println("ADD ITEM TO CART"); 
       System.out.println("Enter the item name:"); 
       String itemName = br.readLine(); 
       System.out.println("Enter the item description:"); 
       String itemDesc = br.readLine(); 
       System.out.println("Enter the item price:"); 
       int itemPrice = Integer.parseInt(br.readLine()); 
       System.out.println("Enter the item quantity:"); 
       int itemQty = Integer.parseInt(br.readLine()); 
       ItemToPurchase it = new ItemToPurchase(itemName, 
         itemPrice, itemQty, itemDesc); 

       shoppingCart.addItem(it); 


       break; 
      // Remove item 
      case 'd': 
       System.out.println("REMOVE ITEM FROM CART"); 
       System.out.println("Enter name of item to remove:"); 
       itemName = br.readLine(); 

       shoppingCart.removeItem(itemName); 
       break; 
      // Change item quantity 
      case 'c': 


       System.out.println("CHANGE ITEM QUANTITY"); 
       System.out.println("Enter item name:"); 
       itemName = br.readLine(); 
       System.out.println("Enter the new quantity:"); 
       itemQty = Integer.parseInt(br.readLine()); 
       ItemToPurchase item = new ItemToPurchase(itemName, 0, itemQty, 
        "none"); 
       item.setName(itemName); 
       item.setQuantity(itemQty); 
       shoppingCart.modifyItem(item); 

       break; 
      // Output items' descriptions 
      case 'i': 
       System.out.println("OUTPUT ITEMS' DESCRIPTIONS"); 
       shoppingCart.printDescriptions(); 
       break; 
      // Output shopping cart 
      case 'o': 
       System.out.println("OUTPUT SHOPPING CART"); 
       shoppingCart.printTotal(); 
       break; 
      // Quit 
      case 'q': 
       br.close(); 
       System.exit(0); 
       } 
      } 
     } 
    } 
    public static void main(String args[]){ 

    try 
    { 
     System.out.println("Enter Customer's Name:"); 
     String customerName = br.readLine(); 
     System.out.println("Enter Today's Date:"); 
     String currentDate = br.readLine(); 
     System.out.println(); 

     ShoppingCart shoppingCart = new ShoppingCart(customerName, 
currentDate); 
     System.out.println("Customer Name: " + shoppingCart.getCustomerName()); 
     System.out.println("Today's Date: " + shoppingCart.getDate()); 

     printMenu(shoppingCart); 
    } 
    catch (IOException ioe) 
    { 
     System.out.println(ioe.toString()); 
    } 
    catch (Exception e) 
    { 
     System.out.println(e.toString()); 
    } 
} 

}

+0

を[最小の作成方法、完全で検証可能な例](https://stackoverflow.com/help/mcve)を参照してください。たとえば、[repl.it](https://repl.it/languages/java)で実行する必要があります。宣言されていないコードを削除した後、残ったコードは残っていました。 – aaron

+0

こんにちは私はプロジェクトコード全体を編集しました。私が抱えている問題は、名前を入力した後に与えられていないオプションを入力し、有効な選択を促し続ける予定の日付である場合です。 – Blaze101

+0

あなたの質問は明確ではありませんでした。私の答えを見てください。 – aaron

答えて

0

をItemToPurchase あなたの質問はかなり曖昧でした!とにかく。 これらの行をwhileループの外側に入れて、一度だけ印刷します。アスカーさんのコメントより引用

System.out.println("\nMENU"); 
    System.out.println("a - Add item to cart"); 
    System.out.println("d - Remove item from cart"); 
    System.out.println("c - Change item quantity"); 
    System.out.println("i - Output items' descriptions"); 
    System.out.println("o - Output shopping cart"); 
    System.out.println("q - Quit"); 
    System.out.println(""); 
    System.out.println("Choose an option:"); 
+0

元の投稿にコメントしてください。 – ChiefTwoPencils

+0

はい、それは終了しますが、無効なオプションが指定されている場合に有効な選択を求めるプロンプトが表示され続ける必要があります。 – Blaze101

+0

@ Blaze101ここで入力した入力を入力できますか? 私はそれを終了しませんが、無効な入力を入力するとプロンプトが表示されます – hsnsd

0

:私は使用しています

の入力は以下のとおりです。BlazeJune 1 2018fsq。私はfsを入力すると、そのだけで使用別のオプション

のメニュー印刷するかどうかを決定するための変数促すためと仮定し、メニュー全体印刷します

boolean shouldPrintMenu = true; 

while (true) { 
    if (shouldPrintMenu) { 
     // ... 
    } else { 
     shouldPrintMenu = true; 
    } 

    System.out.println("Choose an option:"); 
    choice = br.readLine().charAt(0); 

    if (!((choice == 'a') || (choice == 'd') 
     || (choice == 'c') || (choice == 'i') 
     || (choice == 'o') || (choice == 'q'))) 
    { 
     shouldPrintMenu = false; 
     continue; 
    } 
    // ... 
} 
+0

私はこれをしましたが、それでもメニューを提供します。 – Blaze101

+0

更新された回答を参照してください。 – aaron

+0

ありがとうございました。私はそれを試してみましたが、他の誰かが私の仕事をプロフェッショナルなものにするための簡単なソリューションを将来に投稿しましたが、このように使用します。 – Blaze101

関連する問題