我的标题并不完全是最好的,但我不知道如何命名我正在尝试执行的操作。无论哪种方式,我有一个病例开关...忽略无效条目
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个字符。如果不是,我想忽略该条目并从菜单重新开始。我遇到的问题是,即使条形码无效且未设置,程序也会继续询问商品名称。
如何跳过所有这些,然后再次打印菜单?