/// <summary> /// Returns the item profile, explaining where and how the item is stored in the DB. /// </summary> /// <param name="itemSearch">The item we're searching.</param> /// <returns>The item profile, and how it's managed in the DB.</returns> public InventoryItemProfile GetItemProfile(IIInventoryItem itemSearch) { // Ensure we're requiring information. if (itemSearch == null) { return(null); } // Build the dictionary of category/quantity combos. Dictionary <int, int> categoryDic = new Dictionary <int, int>(); for (var i = 0; i < this.cachedInventory.Count; i++) { // If this category has the item stored, add it with its quantity. if (this.cachedInventory[i].ContainsKey(itemSearch.Id)) { categoryDic.Add(i, this.cachedInventory[i][itemSearch.Id].Quantity); } } // Return the new profile we've calculated. return(new InventoryItemProfile() { Id = itemSearch.Id, InventoryByCategory = categoryDic }); }
/// <summary> /// Removes an item from the inventory collection, ensuring some actually does exist in the given category. /// </summary> /// <param name="itemToRemove">The item to remove from the inventory.</param> /// <param name="qtyToRemove">The quantity we're taking out of the inventory.</param> /// <param name="categoryToRemoveFrom">The category we're removing inventory from.</param> /// <returns>How many of this item exist in the category requested after the action.</returns> /// <exception cref="ArgumentNullException">Thrown when the inventory is set up to handle multiple categories, and the category was not given in the call.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when the category requested is outside the bounds managed in the inventory.</exception> public int RemoveItem(IIInventoryItem itemToRemove, int qtyToRemove = 1, int?categoryToRemoveFrom = null) { // Check the category input, to ensure it's valid. categoryToRemoveFrom = this.CalculateCategoryToUse(categoryToRemoveFrom); // Modify the collection, deleting the record if necessary. if (this.cachedInventory[categoryToRemoveFrom.Value].ContainsKey(itemToRemove.Id)) { int qtyRemaining = this.cachedInventory[categoryToRemoveFrom.Value][itemToRemove.Id].Quantity - qtyToRemove; if (qtyRemaining < 0) { // We didn't have enough quantity to remove this quantity. throw new InsufficientInventoryException(qtyToRemove, itemToRemove); } else if (qtyRemaining == 0) { // We need to actually remove this entry from the cache, we deleted it. this.cachedInventory[categoryToRemoveFrom.Value].Remove(itemToRemove.Id); } else { // Subtract the requested inventory from the inventory cache. this.cachedInventory[categoryToRemoveFrom.Value][itemToRemove.Id].Quantity -= qtyToRemove; } // Return the number remaining in the location. return(qtyRemaining); } else { throw new InsufficientInventoryException(qtyToRemove, itemToRemove); } }
/// <summary> /// Creates an element in the given category for the item requested in our inventory. This will be an empty element, with qty 0. /// </summary> /// <param name="itemToCreate">The item to creat in our inventory.</param> /// <param name="categoryNum">The category to add the item to.</param> private void CreateElementForItem(IIInventoryItem itemToCreate, int categoryNum) { // Don't create it, if it already exists. if (this.cachedInventory[categoryNum].ContainsKey(itemToCreate.Id)) { return; } this.cachedInventory[categoryNum].Add(itemToCreate.Id, new InventoryElement() { Item = itemToCreate, Quantity = 0 }); }
/// <summary> /// Adds an item to the inventory collection, by first seeing if it already exists in the given category. /// </summary> /// <param name="itemToAdd">The item to add to the inventory.</param> /// <param name="qtyToAdd">How many of this item to add to the inventory.</param> /// <param name="categoryToAddTo">Which category to add this inventory to - REQUIRED if there's more than one collection.</param> /// <returns>How many of this item exist in the category requested after the action.</returns> /// <exception cref="ArgumentNullException">Thrown when the inventory is set up to handle multiple categories, and the category was not given in the call.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when the category requested is outside the bounds managed in the inventory.</exception> public int AddItem(IIInventoryItem itemToAdd, int qtyToAdd = 1, int?categoryToAddTo = null) { // Check our category input - We either need to assign a value, throw an exception, or continue, depending. categoryToAddTo = this.CalculateCategoryToUse(categoryToAddTo); // Actually modify the collection, creating the entry if necessary. if (!this.cachedInventory[categoryToAddTo.Value].ContainsKey(itemToAdd.Id)) { this.CreateElementForItem(itemToAdd, categoryToAddTo.Value); } this.cachedInventory[categoryToAddTo.Value][itemToAdd.Id].Quantity += qtyToAdd; return(this.cachedInventory[categoryToAddTo.Value][itemToAdd.Id].Quantity); }
/// <summary> /// Instantiates a new instance of the <see cref="InsufficientInventoryException" /> class. /// </summary> /// <param name="qtyRequested">The quantity that was requested in this action.</param> /// <param name="itemRequested">The item was requested in this action.</param> public InsufficientInventoryException(int qtyRequested, IIInventoryItem itemRequested) { this.ItemName = itemRequested.ToString().ToString(); this.Quantity = qtyRequested; }