Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The setter <code>setBarCode()</code> should either (a) succeed, or (b) indicate failure (probably using an <code>IllegalArgumentException</code> since we're in Java) rather than fail silently. If you were to use an <code>IllegalArgumentException</code>, this code would work nicely:</p> <pre><code>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; </code></pre> <p>However, I'd recommend you not rely on the failure of a setter for correctness. Stylistically, it "smells funny." Rather, I'd put the test in another (probably <code>static</code>) method, test <em>before</em> you call the setter and react accordingly, and then put an <code>assert</code> in the setter. So, more like this:</p> <pre><code>// 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload