public static void ProgPart5_BlockingCollectionram_Main() { StaffLogsForBonuses staffLogs = new StaffLogsForBonuses(); ToDoQueue toDoQueue = new ToDoQueue(staffLogs); Part5_SalesPerson[] people = { new Part5_SalesPerson("Sahil"), new Part5_SalesPerson("Peter"), new Part5_SalesPerson("Juliette"), new Part5_SalesPerson("Xavier") }; Part5_StockController controller = new Part5_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.MonitorAndLogTrades()); Task bonusLogger2 = Task.Run(() => toDoQueue.MonitorAndLogTrades()); Task.WaitAll(t1, t2, t3, t4); toDoQueue.CompleteAdding(); Task.WaitAll(bonusLogger, bonusLogger2); controller.DisplayStatus(); staffLogs.DisplayReport(people); }
public void Work(Part5_StockController stockController, TimeSpan workDay) { Random rand = new Random(Name.GetHashCode()); DateTime start = DateTime.Now; while (DateTime.Now - start < workDay) { Thread.Sleep(rand.Next(100)); bool buy = (rand.Next(6) == 0); string itemName = ProgPart5_BlockingCollectionram.AllShirtNames[rand.Next(ProgPart5_BlockingCollectionram.AllShirtNames.Count)]; if (buy) { int quantity = rand.Next(9) + 1; stockController.BuyStock(this, itemName, quantity); DisplayPurchase(itemName, quantity); } else { bool success = stockController.TrySellItem(this, itemName); DisplaySaleAttempt(success, itemName); } } Console.WriteLine("SalesPerson {0} signing off", this.Name); }