//must double check stocks //then must add to the order histories of customer and location. //also must then change the stocks to reflect the change in stock. //POSSIBLE THING TO ADD, Event call somewhere to notify other orders that //stock has changed. /// <summary> /// MUST be called to place the order and add it to the customer and location's /// order history, and update the location's stocks. /// </summary> public void FinallizeOrder(int finalID = -999) { this.Time = DateTime.UtcNow; _orderCost = this.Cost; if (EnoughStockForAllItems()) { //consider surrounding in a try catch, //or something incase this fails. //Or, make a mutex for when we change stocks on the Location. foreach (ItemCount ic in Items) { OrderLoc.RemovePurchasedStock(ic); } //assign final ID if (finalID == -999) { _Id = Orders.Instance.GetAllOrders().Max(order => order.ID) + 1; } else { _Id = finalID; } Orders.Instance.AddOrders(this); } else { throw new NotEnoughStockException("Error: No longer enough stock for these items."); } }
/// <summary> /// Add an item to the order. /// </summary> /// <exception cref="NotEnoughStockException">If there's not enough stock</exception> /// <param name="item"></param> /// <param name="amount"></param> public void AddItem(IItem item, int amount) { if (OrderLoc.CheckIfEnoughStock(item.name, amount)) { this._items.Add(new ItemCount(amount, item.name)); } else { throw new NotEnoughStockException("not enough stock to buy " + item.name); } }
/// <summary> /// Check if theres enough stock at the location for all items in the order. /// </summary> /// <returns>True if there's enough stock for the items in the order at the location.</returns> private bool EnoughStockForAllItems() { //CHECK LOCATION STOCKS bool EnoughStock = true; //Required Functionality foreach (ItemCount ic in Items) { EnoughStock = EnoughStock && OrderLoc.CheckIfEnoughStock(ic.ThisItem.name, ic.Count); } return(EnoughStock); }
//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; } } }