2013-04-23 70 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更多的信息,以便您可以区分三种情况:坏条形码;成功;因为需要更多信息而失败。