void InitTable(Establishment establishment) { for (int i = 0; i < establishment.MaxChairs; i++) { var chair = new Chair(); chairsAroundTable.Add(chair); } }
public bool TryGetChair(out Chair chairToReturn) { foreach (var chair in chairs) { if (chair.Occupant == null) { chairToReturn = chair; return(true); } } chairToReturn = null; return(false); }
private void LeavePub(ConcurrentDictionary <int, Patron> allPatrons, ConcurrentQueue <Chair> availableChairs, ConcurrentBag <Glass> glassesOnTables) { if (chairUsed != null) { availableChairs.Enqueue(chairUsed); } uiUpdater.UpdateChairLabel(availableChairs.Count()); chairUsed = null; glassesOnTables.Add(carriedBeer); carriedBeer = null; LogStatus($"{name} leaves the pub", this); allPatrons.TryRemove(patronID, out _); uiUpdater.UpdatePatronLabel(allPatrons.Count()); LeftPub = true; }
public override void AgentCycle(Bar bar) { while (hasGoneHome is false) { TimeDrinkingBeer(TimeSpentDrinkingBeer); switch (CurrentState) { case RunState.WalkingToBar: { BarController.EventListBoxHandler(this, $"{Name} entered and is walking to the bar"); bar.PatronsWaitingForBeer.Enqueue(this); CurrentQueue = bar.PatronsWaitingForBeer; Thread.Sleep(1000); CurrentState = RunState.WaitingForBeer; break; } case RunState.WaitingForBeer: { BarController.EventListBoxHandler(this, $"{Name} is waiting for a glass of beer"); while (glass is null) { Thread.Sleep(TimeSpentWaiting); } BarController.EventListBoxHandler(this, $"{Name} got a glass of beer"); DequeuePatron(CurrentQueue, this); CurrentState = RunState.WaitingForChair; break; } case RunState.WaitingForChair: { bar.PatronsWaitingForChair.Enqueue(this); CurrentQueue = bar.PatronsWaitingForChair; BarController.EventListBoxHandler(this, $"{Name} is waiting for an available chair"); var isFirstInQueue = false; while (isFirstInQueue is false) { Thread.Sleep(TimeSpentWaiting); bar.PatronsWaitingForChair.TryPeek(out var firstPatronInQueue); if (this.Equals(firstPatronInQueue)) { isFirstInQueue = true; while (chair is null) { foreach (var chair in bar.availableChairs) { if (!(chair.IsAvailable)) { continue; } this.chair = chair; chair.IsAvailable = false; DequeuePatron(CurrentQueue, this); BarController.RefreshLabels(); break; } } } } CurrentState = RunState.WalkingToChair; break; } case RunState.WalkingToChair: { BarController.EventListBoxHandler(this, $"{Name} is walking to a chair"); Thread.Sleep(TimeSpentWalkingToChair); CurrentState = RunState.DrinkingBeer; break; } case RunState.DrinkingBeer: { BarController.EventListBoxHandler(this, $"{Name} is drinking the beer"); Thread.Sleep(TimeSpentDrinkingBeer); glass.HasBeer = false; bar.glassesOnTables.Add(glass); chair.IsAvailable = true; CurrentState = RunState.LeavingThePub; break; } case RunState.LeavingThePub: { BarController.EventListBoxHandler(this, $"{Name} finished the beer and is going home"); while (hasGoneHome is false) { hasGoneHome = bar.patronsQueue.TryRemove(Name, out _); } hasGoneHome = true; break; } } } }