我的標題並不完全是最好的,但我不知道如何命名我正在嘗試執行的操作。無論哪種方式,我有一個病例開關...忽略無效條目
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個字符。如果不是,我想忽略該條目並從菜單重新開始。我遇到的問題是,即使條形碼無效且未設置,程序也會繼續詢問商品名稱。
如何跳過所有這些,然後再次打印菜單?