/// <summary> /// Removes <see cref="ShelfOrder"/> from the shelf /// </summary> /// <param name="order">Order to be removed</param> /// <returns>true, if order was removed, otherwise - false</returns> public bool Remove(ShelfOrder order) { lock (_syncLock) { return(Orders.Remove(order)); } }
/// <summary> /// Adds order to shelf. Element can not be added if shelf reached its MaxCapacity /// </summary> /// <param name="order">Order to be added</param> /// <returns>true, if order was added, otherwise - false</returns> public bool Add(ShelfOrder order) { lock (_syncLock) { if (Orders.Count >= MaxCapacity) { return(false); } Orders.Add(order); return(true); } }
/// <summary> /// Add order to shelves. Sets up 'courier pickup' and 'deterrioration' events. /// </summary> /// <param name="order">Order to add.</param> public virtual void AddOrder(Order order) { var shelf = shelves[order.Temp]; if (shelf == null) { throw new ArgumentException($"Shelf \"{order.Temp}\" was not initialized", nameof(order.Temp)); } var shelfOrder = new ShelfOrder(order, DateTime.Now); if (!shelf.Add(shelfOrder)) { shelf = AddToOverflowShelf(shelfOrder); } SetUpCourierTimer(shelf, shelfOrder); SetUpDeterriorationTimer(shelf, shelfOrder); shelfNotificationService.PublishOrderReceivedEvent(shelf, order); }
protected virtual void SetUpDeterriorationTimer(KitchenShelf shelf, ShelfOrder shelfOrder) { var timer = deterriorationTimerFactory.Create(shelf, shelfOrder); timer.Elapsed += (sender, args) => { lock (shelfOrder) { if (!shelfOrder.HasTriggeredEvent) { shelfOrder.HasTriggeredEvent = true; OnOrderDeterrioration(shelf, shelfOrder); } } }; timer.Start(); shelfOrder.AddTimer(timer); }
protected virtual void SetUpCourierTimer(KitchenShelf shelf, ShelfOrder shelfOrder) { var timer = courierTimerFactory.Create(shelf, shelfOrder); timer.Elapsed += (sender, args) => { lock (shelfOrder) { if (!shelfOrder.HasTriggeredEvent) { shelfOrder.HasTriggeredEvent = true; OnCourierArrival(shelfOrder, shelf, timer); } } }; timer.Start(); shelfOrder.AddTimer(timer); }
protected virtual KitchenShelf AddToOverflowShelf(ShelfOrder shelfOrder) { var shelf = shelves[AppSettings.Instance.AppConfig.Shelves.OverflowShelfName]; lock (shelf) { if (shelf.Orders.Count == shelf.MaxCapacity) { var removedShelfOrder = shelf.RemoveRandom(); lock (removedShelfOrder) { if (!removedShelfOrder.HasTriggeredEvent) { removedShelfOrder.HasTriggeredEvent = true; shelfNotificationService.PublishOrderDiscardedFromOverflowShelfEvent(shelfOrder.Order); } } } shelf.Add(shelfOrder); } return(shelf); }
protected virtual void OnOrderDeterrioration(KitchenShelf shelf, ShelfOrder shelfOrder) { shelfOrder.StopAllTimers(); RemoveOrderFromShelf(shelf, shelfOrder); shelfNotificationService.PublishOrderDeterrioratedEvent(shelfOrder.Order); }
protected virtual void OnCourierArrival(ShelfOrder shelfOrder, KitchenShelf shelf, Timer timer) { shelfOrder.StopAllTimers(); shelf.Remove(shelfOrder); shelfNotificationService.PublishOrderReceivedByCourierEvent(shelfOrder.Order, (int)timer.Interval / 1000); }
protected virtual void RemoveOrderFromShelf(KitchenShelf shelf, ShelfOrder order) { shelf.Remove(order); }