/// <summary>
        /// Add an amount of an item to the Location's invintory.
        /// </summary>
        /// <param name="itemName">The human readable name of the item.</param>
        /// <param name="amount">Amount of the item to add.</param>
        /// <returns>The new Stock Level.</returns>
        public int  AddInventory(string itemName, int amount)
        {
            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("Must add a positive amount of stock.");
            }

            ItemCount itemStocks;

            if (Stocks.TryGetValue(itemName, out itemStocks))
            {
                int newAmount = amount + itemStocks.Count;
                Stocks[itemName] = new ItemCount(newAmount, itemName);
                return(newAmount);
            }
            else if (StoreCatalogue.Instance.ItemExists(itemName))
            {
                Stocks[itemName] = new ItemCount(amount, itemName);
                return(amount);
            }
            else
            {
                //To be fair, this would already be thrown by the constructor for ItemCount
                throw new ItemNotFoundException("That item doesn't exist yet.");
            }
        }
示例#2
0
        /// <summary>
        /// Creates a new order for one item.
        /// </summary>
        /// <param name="l">The Location the order is being placed at.</param>
        /// <param name="c">The customer placing the order.</param>
        /// <param name="item">The item and amount being ordered.</param>
        public Order(Location l, Customer c, ItemCount item)
        {
            Time     = DateTime.UtcNow;
            Customer = c;
            OrderLoc = l;
            List <ItemCount> _items = new List <ItemCount>();

            _items.Add(item);
        }
 /// <summary>
 /// Remvoes the items from the stock.
 /// </summary>
 /// <remarks>
 /// Assumes the quantity is positive, and is intended to be subtracted.
 /// </remarks>
 /// <param name="ic">The Item-Count pair to be removed.</param>
 internal void RemovePurchasedStock(ItemCount ic)
 {
     if (this.CheckIfEnoughStock(ic.ThisItem.name, ic.Count))
     {
         ItemCount itemStocks;
         if (Stocks.TryGetValue(ic.ThisItem.name, out itemStocks))
         {
             int newAmount = itemStocks.Count - ic.Count;
             Stocks[ic.ThisItem.name] = new ItemCount(newAmount, ic.ThisItem.name);
         }
         else
         {
             throw new ItemNotFoundException($"Error: failed to find {ic.ThisItem.name} in the location's stocks");
         }
     }
 }
 /// <summary>
 /// Comprimise with the encapsulation to let the DB set stocks if something is out of synch.
 /// </summary>
 public void SetItemStock(string itemname, int amount)
 {
     Console.Error.WriteLine("Warning: directly setting a store's stock may cause inconsistancy, or something. ... it violates encapsulation.");
     if (Stocks.ContainsKey(itemname))
     {
         Stocks[itemname] = new ItemCount(amount, itemname);
     }
     else
     {
         if (StoreCatalogue.Instance.ItemExists(itemname))
         {
             Stocks[itemname] = new ItemCount(amount, itemname);
         }
         else
         {
             throw new ItemNotFoundException($"{itemname} cannot be found.");
         }
     }
 }
示例#5
0
        //must check stocks, and if item is already in order.
        /// <summary>
        /// Add or change the amount of an item being ordered.
        /// </summary>
        /// <remarks>
        /// Can throw an argument out of range exception if the amount would lead to a negative
        /// number of itemname being in the order. Can also throw a Not Enough Stock exception
        /// if there is not enough stock to be bought at the current location for the new ammount.
        /// </remarks>
        /// <exception cref="NotEnoughStockException">If there's not enough stock.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If there would be a negative amount in the order</exception>
        /// <param name="itemname">The name of the item</param>
        /// <param name="deltaAmount">The amount, + or -, to change the quantity of item in the order by.</param>
        public void EditOrderAmounts(string itemname, int deltaAmount)
        {
            ItemCount newcount;
            ItemCount oldcount;

            //find the old count for the item
            foreach (ItemCount ic in Items)
            {
                if (ic.ThisItem.name == itemname)
                {
                    oldcount = ic;

                    if (deltaAmount + oldcount.Count >= 0 &&
                        OrderLoc.CheckIfEnoughStock(itemname, deltaAmount + oldcount.Count))
                    {
                        //avoid duplicate entries for the item in the list of items.
                        _items.Remove(oldcount);

                        //Make sure this isn't removing an item from the order before adding the new
                        //ItemCount to the list of items in the order
                        if (deltaAmount > 0)
                        {
                            newcount = new ItemCount(oldcount.Count + deltaAmount, oldcount.ThisItem.name);
                            _items.Add(newcount);
                        }
                    }
                    else
                    {
                        if (deltaAmount + oldcount.Count < 0)
                        {
                            throw new ArgumentOutOfRangeException("Error: Would be buying a negative amount of the item.");
                        }
                        else
                        {
                            throw new NotEnoughStockException("Not enough stock to add this many of the item.");
                        }
                    }
                    break;
                }
            }
        }