2013-04-23 50 views
0

我的標題並不完全是最好的,但我不知道如何命名我正在嘗試執行的操作。無論哪種方式,我有一個病例開關...忽略無效條目

switch (input) { 
     case "A": 
      Item item = new Item(); 
      System.out.print("Enter a barcode: "); 
      barCode = scan.nextLine(); 
      item.setBarCode(barCode); 

      if (store.addItem(barCode)) { 
       System.out.println(store.stockedItems.get(barCode).getProductName() 
         + " has been added to the store's inventory"); 
      } 

      else { 
       item.setQuantity(1); 
       System.out.print("Enter the item's name: "); 
       productName = scan.nextLine(); 
       productName = productName.toLowerCase(); 
       item.setProductName(productName); 
       store.stockedItems.put(barCode, item); 

       System.out.println(store.stockedItems.get(barCode).getProductName() 
         + " has been added to the store's inventory"); 
      } 
      break; 
    } 

這只是一種情況。當用戶選擇A將一個對象添加到我的數據結構中時,它會發現所提及的條形碼是否已被使用。

如果是這樣,它只會增加數據結構中對象的數量。

如果條形碼未被使用並且在檢查其有效性之後。它會提示用戶輸入對象的名稱,然後繼續將其添加到我的數據結構中。

現在的問題是在我輸入條形碼串並調用各自的對象類的setter函數:

public void setBarCode(String code) { 
    if (!code.matches("[0-9]+") || code.length() != 12) { 
     System.out.println("The barcode entered is not in valid format. Entry ignored."); 
    } else { 
     barcode = code; 
    } 
} 

此功能只是確保它是一個數,長12個字符。如果不是,我想忽略該條目並從菜單重新開始。我遇到的問題是,即使條形碼無效且未設置,程序也會繼續詢問商品名稱。

如何跳過所有這些,然後再次打印菜單?

回答

1

的二傳手setBarCode()應該是(a)成功,或(b)表示失敗(可能使用IllegalArgumentException,因爲我們是在Java中),而不是默默地失敗。如果你使用的IllegalArgumentException,這個代碼將很好地工作:

boolean acceptable; 
try { 
    item.setBarCode(barCode); 
    acceptable = true; 
} 
catch(IllegalArgumentException e) { 
    acceptable = false; 
} 

if(acceptable) { 
     if(store.addItem(barCode)){ 
      System.out.println(store.stockedItems.get(barCode).getProductName() + " has been added to the store's inventory"); 
     } 
     else { 
      item.setQuantity(1); 
      System.out.print("Enter the item's name: "); 
      productName = scan.nextLine(); 
      productName = productName.toLowerCase(); 
      item.setProductName(productName); 
      store.stockedItems.put(barCode, item); 

      System.out.println(store.stockedItems.get(barCode).getProductName() + " has been added to the store's inventory"); 
     } 
} 

break; 

不過,我建議你不要依賴於正確性二傳手的失敗。在風格上,它「聞起來很有趣」。相反,我會在另一個(可能是static)方法中進行測試,在之前測試您調用setter並作出相應的反應,然後將assert放入setter中。所以,更多類似這樣:

// Somewhere up in your code -- Sorry, fixed up your regex 
private static final Pattern BARCODE=Pattern.compile("^\\d{12}$"); 
public static boolean isValidBarcode(String candidate) { 
    return BARCODE.matcher(candidate).matches(); 
} 

// Now your "real" code 
case "A": 

    Item item = new Item(); 
    System.out.print("Enter a barcode: "); 
    barCode = scan.nextLine(); 
    if(isValidBarCode(barCode)) { 
     item.setBarCode(barCode); 
     if(store.addItem(barCode)) { 
      System.out.println(store.stockedItems.get(barCode).getProductName() + " has been added to the store's inventory"); 
     } 
     else { 
      item.setQuantity(1); 
      System.out.print("Enter the item's name: "); 
      productName = scan.nextLine(); 
      productName = productName.toLowerCase(); 
      item.setProductName(productName); 
      store.stockedItems.put(barCode, item); 

      System.out.println(store.stockedItems.get(barCode).getProductName() + " has been added to the store's inventory"); 
     } 
    } 
    else { 
     System.out.println("That's not a valid bar code."); 
    } 
    break; 

// And, finally, your setBarCode() method 
public void setBarCode(String code) { 
    assert isValidBarCode(code); 
    barcode = code; 
} 
2

兩個策略可以爲這項工作:

  1. 移動檢查爲setBarCode方法外條形碼有效性,首先做的是測試(或修改setBarCode返回一個boolean指示條形碼是否有效)。
  2. 修改addItem返回比boolean更多的信息,以便您可以區分三種情況:壞條形碼;成功;因爲需要更多信息而失敗。