Ejemplo n.º 1
0
        public async Task Run()
        {
            StockController ctr     = new StockController(TShirtProvider.AllShirts);
            TimeSpan        workDay = new TimeSpan(0, 0, 0, 0, 500);

            Task task1 = Task.Run(() => new SalesPerson("Kim").Work(workDay, ctr));
            Task task2 = Task.Run(() => new SalesPerson("Sahil").Work(workDay, ctr));
            Task task3 = Task.Run(() => new SalesPerson("Chuck").Work(workDay, ctr));

            Task.WaitAll(task1, task2, task3);

            ctr.DisplayStock();
        }
Ejemplo n.º 2
0
        public void Work(TimeSpan workDay, StockController ctr)
        {
            DateTime start = DateTime.Now;

            while (DateTime.Now - start < workDay)
            {
                var result = ServeCustomer(ctr);

                if (result.Status != null)
                {
                    Console.WriteLine($"{Name}: {result.Status}");
                }
                if (!result.ShirtInStock)
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public (bool ShirtInStock, string Status) ServeCustomer(StockController ctr)
        {
            TShirt shirt = ctr.SelectRandomShirt();

            if (shirt == null)
            {
                return(false, "All shirts sold");
            }

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

            // customer chooses to buy with only 20% probability
            if (Rnd.TrueWithProb(0.2))
            {
                ctr.Sell(shirt.Code);
                return(true, $"Sold {shirt.Name}");
            }

            return(true, null);
        }