Java 忽略无效条目

Java 忽略无效条目,java,switch-statement,Java,Switch Statement,我的头衔并不是最好的,但我不知道如何命名我想做的事情。不管怎样,我有一个箱子开关 switch (input) { case "A": Item item = new Item(); System.out.print("Enter a barcode: "); barCode = scan.nextLine(); item.setBarCode(barCode);

我的头衔并不是最好的,但我不知道如何命名我想做的事情。不管怎样,我有一个箱子开关

    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;
    }
这只是一个例子。这样做的目的是,当用户选择将一个对象添加到我的数据结构时,它会发现所提到的条形码是否已经在使用中

如果是,它只是增加数据结构中对象的数量

如果条形码未使用,则在检查其有效性后。它将提示用户输入对象的名称,然后继续将其添加到我的数据结构中

现在的问题是在我输入条形码字符串并在其各自的对象类中调用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个字符。如果不是,我想忽略该条目并从菜单重新开始。我遇到的问题是,即使条形码无效且未设置,程序也会继续询问项目名称


如何跳过所有这些,然后再次打印菜单?

有两种策略可以实现这一点:

  • 将条形码有效性检查移到
    setBarCode
    方法之外,然后首先执行该测试(或修改
    setBarCode
    以返回指示条形码是否有效的
    boolean
  • 修改
    addItem
    以返回比
    boolean
    更具信息性的内容,以便可以区分三种情况:坏条形码;成功;失败,因为它需要更多信息

  • 有两种策略可以做到这一点:

  • 将条形码有效性检查移到
    setBarCode
    方法之外,然后首先执行该测试(或修改
    setBarCode
    以返回指示条形码是否有效的
    boolean
  • 修改
    addItem
    以返回比
    boolean
    更具信息性的内容,以便可以区分三种情况:坏条形码;成功;失败,因为它需要更多信息

  • setter
    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;
    
    但是,我建议您不要依赖setter的失败来判断正确性。从风格上讲,它“闻起来很有趣”。相反,我会将测试放在另一个(可能是
    静态
    )方法中,在调用setter并做出相应反应之前进行测试,然后在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;
    }
    

    setter
    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;
    
    但是,我建议您不要依赖setter的失败来判断正确性。从风格上讲,它“闻起来很有趣”。相反,我会将测试放在另一个(可能是
    静态
    )方法中,在调用setter并做出相应反应之前进行测试,然后在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;
    }