Пример #1
0
        public (bool ShirtsInStock, string Status) ServeCustomer(
            StockController controller)
        {
            var    result = controller.SelectRandomShirt();
            TShirt shirt  = result.Shirt;

            if (result.Result == SelectResult.NoStockLeft)
            {
                return(false, "All shirts sold");
            }
            else if (result.Result == SelectResult.ChosenShirtSold)
            {
                return(true, "Can't show shirt to customer - already sold");
            }

            Thread.Sleep(Rnd.NextInt(30));

            // customer chooses to buy with only 20% probability
            if (Rnd.TrueWithProb(0.2))
            {
                bool sold = controller.Sell(shirt.Code);
                if (sold)
                {
                    return(true, $"Sold {shirt.Name}");
                }
                else
                {
                    return(true, $"Can't sell {shirt.Name}:Already sold");
                }
            }
            return(true, null);
        }
Пример #2
0
        public (SelectResult Result, TShirt Tshirt) SelectRandomShirt()
        {
            var keys = _stock.Keys.ToList();

            if (keys.Count == 0)
            {
                return(SelectResult.NoStockLeft, null);                    // all shirts sold
            }
            Thread.Sleep(Rnd.NextInt(10));
            string selectedCode = keys[Rnd.NextInt(keys.Count)];
            bool   found        = _stock.TryGetValue(selectedCode, out TShirt shirt);

            if (found)
            {
                return(SelectResult.Success, shirt);
            }
            else
            {
                return(SelectResult.ChosenTShirtSold, null);
            }
            //return _stock[selectedCode];
        }