using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public static void Main()
var inventoryManagement = new Program();
inventoryManagement.CheckOut(1, 49);
public ObservableCollection<Item> Inventory { get; set; }
public Order Orders { get; set; }
Inventory = new ObservableCollection<Item>();
Inventory.CollectionChanged += InventoryChanged;
public void InitialiseInventory()
Inventory.Add(new Item() { ItemId = 1, Name = "Ball Point Pen", Price = 5, StockInHand = 50 });
Inventory.Add(new Item() { ItemId = 2, Name = "Notebook", Price = 5, StockInHand = 20 });
Inventory.Add(new Item() { ItemId = 3, Name = "Pencil", Price = 5, StockInHand = 100 });
Inventory.Add(new Item() { ItemId = 4, Name = "Eraser", Price = 5, StockInHand = 30 });
private void CheckOut(int itemId, int units)
var item = this.Inventory.FirstOrDefault(x => x.ItemId == itemId);
if (item != null && item.StockInHand > units)
item.StockInHand = item.StockInHand - units;
private void InventoryChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
if (e.Action == NotifyCollectionChangedAction.Remove)
foreach (Item item in e.OldItems)
item.PropertyChanged -= ItemPropertyChanged;
else if (e.Action == NotifyCollectionChangedAction.Add)
foreach (Item item in e.NewItems)
item.PropertyChanged += ItemPropertyChanged;
public void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "StockInHand")
var item = sender as Item;
if (item == null) return;
if (item.StockInHand < 5)
Orders.RefillRequest(item.ItemId, 100);
public class Item : INotifyPropertyChanged
public int ItemId { get; set; }
public string Name { get; set; }
public int Price { get; set; }
get { return stockInHand; }
this.stockInHand = value;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
public class Order : List<Order>
public void RefillRequest(int itemId, int orderCount)
if (!this.Any(x => x.ItemId == itemId))
this.Add(new Order() { ItemId = itemId, Units = orderCount });
Console.WriteLine("Reorder request is triggered for ItemId : " + itemId);