public bool TrySellItem(Part5_SalesPerson person, string item) { bool success = false; int newStockLevel = _stock.AddOrUpdate(item, (itemName) => { success = false; return(0); }, (itemName, oldValue) => { if (oldValue == 0) { success = false; return(0); } else { success = true; return(oldValue - 1); } }); if (success) { Interlocked.Increment(ref _totalQuantitySold); _toDoQueue.AddTrade(new Trade(person, 1)); } return(success); }
public bool TrySellItem2(Part5_SalesPerson person, string item) { int newStockLevel = _stock.AddOrUpdate(item, -1, (key, oldValue) => oldValue - 1); if (newStockLevel < 0) { _stock.AddOrUpdate(item, 1, (key, oldValue) => oldValue + 1); return(false); } else { Interlocked.Increment(ref _totalQuantitySold); _toDoQueue.AddTrade(new Trade(person, 1)); return(true); } }
public Trade(Part5_SalesPerson person, int quantitySold) { this.Person = person; this.QuantitySold = quantitySold; }
public void BuyStock(Part5_SalesPerson person, string item, int quantity) { _stock.AddOrUpdate(item, quantity, (key, oldValue) => oldValue + quantity); Interlocked.Add(ref _totalQuantityBought, quantity); _toDoQueue.AddTrade(new Trade(person, -quantity)); }