예제 #1
0
        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);
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 public Trade(Part5_SalesPerson person, int quantitySold)
 {
     this.Person       = person;
     this.QuantitySold = quantitySold;
 }
예제 #4
0
 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));
 }