static void Main(string[] args)
        {
            #region old
            //var stock = new Dictionary<string, int>()
            //{
            //    {"Rick", 4},
            //    {"Morty", 3}
            //};

            //Console.WriteLine($"No. of shirts in stock = {stock.Count}");

            //stock.Add("Let`sGetShifty", 6);
            //stock["BirdMan"] = 5; // adding by using an indexer

            //stock["Let`sGetShifty"] = 12; // up from 6, 6 were bought
            //Console.WriteLine($"\r\nstock[Let`sGetShifty] = {stock["Let`sGetShifty"]}");

            //stock.Remove("Rick");

            //Console.WriteLine("\r\nEnumerating:");
            //foreach (var kewValPair in stock)
            //{
            //    Console.WriteLine($"{kewValPair.Key}: {kewValPair.Value}");
            //}
            //Console.WriteLine($"No. of shirts in stock = {stock.Count}");
            //Console.ReadKey();
            #endregion

            StaffLogsForBonuses staffLogs = new StaffLogsForBonuses();
            ToDoQueue           toDoQueue = new ToDoQueue(staffLogs);

            SalesPerson[] people = { new SalesPerson("Robert"), new SalesPerson("Emma"), new SalesPerson("Arnold"), new SalesPerson("Cameron") };

            StockController controller = new StockController(toDoQueue);
            TimeSpan        workDay    = new TimeSpan(0, 0, 1);

            Task t1 = Task.Run(() => people[0].Work(controller, workDay));
            Task t2 = Task.Run(() => people[1].Work(controller, workDay));
            Task t3 = Task.Run(() => people[2].Work(controller, workDay));
            Task t4 = Task.Run(() => people[3].Work(controller, workDay));

            Task bonusLogger  = Task.Run(() => toDoQueue.MonitorAndLogTrates());
            Task bonusLogger2 = Task.Run(() => toDoQueue.MonitorAndLogTrates());

            Task.WaitAll(t1, t2, t3, t4);
            toDoQueue.CompleteAdding();
            Task.WaitAll(bonusLogger, bonusLogger2);

            controller.DisplayStatus();
            staffLogs.DisplayReport(people);
            Console.ReadKey();
        }
Example #2
0
        internal void Work(StockController controller, TimeSpan workDay)
        {
            Random   rand  = new Random(Name.GetHashCode());
            DateTime start = DateTime.Now;

            while (DateTime.Now - start < workDay)
            {
                Thread.Sleep(rand.Next(100));          // by commenting this we can get real stress test of our conc. dict, as all transactions are effected almost at the same time.
                bool   buy      = (rand.Next(6) == 0); // will choose a number between 0-5, and if 0 we buy, so we sell 5 times more than buy
                string itemName = Program.AllShirtNames[rand.Next(Program.AllShirtNames.Count)];
                if (buy)
                {
                    int quantity = rand.Next(9) + 1;
                    controller.BuyStock(this, itemName, quantity);
                    DisplayPurchase(itemName, quantity);
                }
                else
                {
                    bool success = controller.TrySellItem(this, itemName); // or try TrySellItem2
                    DisplaySaleAttempt(success, itemName);
                }
            }
            Console.WriteLine($"SalesPerson {this.Name} signing off");
        }