public void addProduct(Product product, int quantity) { InventoryItem inventoryItem = inventoryRepository.findByProduct(product); if (inventoryItem == null) { inventoryItem = new InventoryItem(product); inventoryRepository.update(inventoryItem); } inventoryItem.replenish(quantity); }
@Transactional(readOnly = true) public boolean isProductAvailable(Product product, int quantity) { InventoryItem inventoryItem = inventoryRepository.findByProduct(product); if ((inventoryItem == null) || (inventoryItem.getQuantityOnHand() < quantity)) { return false; } return true; }
public void removeProduct(Product product, int quantity) { if (isProductAvailable(product, quantity)) { InventoryItem inventoryItem = inventoryRepository.findByProduct(product); inventoryItem.deplete(quantity); } else { throw new InventoryException( "Insufficient inventory to fulfil" + " request for " + product.getName() + " quantity " + quantity); } }