Beispiel #1
0
        public string ServeCustomer(StockController controller, LogTradesQueue bonusQueue)
        {
            Thread.Sleep(Rnd.NextInt(20));
            TShirt shirt = TShirtProvider.SelectRandomShirt();
            string code  = shirt.Code;

            bool custSells = Rnd.TrueWithProb(1.0 / 6.0);

            if (custSells)
            {
                int quantity = Rnd.NextInt(9) + 1;
                controller.BuyShirts(code, quantity);
                bonusQueue.QueueTradeForLogging(
                    new Trade(this, shirt, TradeType.Purchase, quantity));
                return($"Bought {quantity} of {shirt}");
            }
            else
            {
                bool success = controller.TrySellShirt(code);
                if (success)
                {
                    bonusQueue.QueueTradeForLogging(
                        new Trade(this, shirt, TradeType.Sale, 1));
                    return($"Sold {shirt}");
                }
                else
                {
                    return($"Couldn't sell {shirt}: Out of stock");
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            StockController controller  = new StockController();
            TimeSpan        workDay     = new TimeSpan(0, 0, 0, 0, 500);
            StaffRecords    staffLogs   = new StaffRecords();
            LogTradesQueue  tradesQueue = new LogTradesQueue(staffLogs);

            SalesPerson[] staff =
            {
                new SalesPerson("Sahil"),
                new SalesPerson("Julie"),
                new SalesPerson("Kim"),
                new SalesPerson("Chuck")
            };
            List <Task> salesTasks = new List <Task>();

            foreach (SalesPerson person in staff)
            {
                salesTasks.Add(
                    Task.Run(() => person.Work(workDay, controller, tradesQueue)));
            }

            Task[] loggingTasks =
            {
                Task.Run(() => tradesQueue.MonitorAndLogTrades()),
                Task.Run(() => tradesQueue.MonitorAndLogTrades())
            };

            Task.WaitAll(salesTasks.ToArray());
            tradesQueue.SetNoMoreTrades();
            Task.WaitAll(loggingTasks);

            controller.DisplayStock();
            staffLogs.DisplayCommissions(staff);
        }
Beispiel #3
0
        public void Work(TimeSpan workDay, StockController controller, LogTradesQueue bonusQueue)
        {
            DateTime start = DateTime.Now;

            while (DateTime.Now - start < workDay)
            {
                string msg = ServeCustomer(controller, bonusQueue);
                if (msg != null)
                {
                    Console.WriteLine($"{Name}: {msg}");
                }
            }
        }