我的头衔并不是最好的,但我不确定该如何命名。无论哪种方式,我都有一个案例切换功能...
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个字符。如果不是,我想忽略该条目并从菜单开始。我遇到的问题是,即使条形码无效且未设置,程序也会继续询问项目名称。
如何跳过所有内容,然后再次打印菜单?
最佳答案
设置器setBarCode()
应该(a)成功,或者(b)指示失败(因为我们在Java中,所以可能使用IllegalArgumentException
),而不是默默地失败。如果您使用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
)方法中,在调用设置器并进行相应反应之前进行测试,然后在设置器中放置一个assert
。因此,更像这样:// 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;
}