private void LeaveEvent(Customer customer) { // generates a random interval for how long the person takes, with a minimum of 1 minute // and 30 seconds TimeSpan waiting = new TimeSpan(0, 0, (int)(120 + Negex(AvgCheckoutTime.TotalSeconds - 120))); // Creates departure event based on the time interval plus the time we are currently // at in the Priority Queue Event e = new Event(EVENTTYPE.LEAVE, ((EventPriorityQueue.Peek().Time + waiting))); //Associates departure event with registrant e.CurrCustomer = customer; customer.Leave = e; // Adds departure event to the queue EventPriorityQueue.Enqueue(e); // Update wait times for statistic counters TotalTime += waiting; if (waiting > LongestTime) { LongestTime = waiting; } if (waiting < ShortestTime) { ShortestTime = waiting; } }
public void Simulate() { Queue <Customer> shortLine = null; Queue <Customer> Line = null; if (EventPriorityQueue.IsEmpty()) // Indicates simulation is finished running { CheckedOut = true; return; } if (EventPriorityQueue.Peek().Type == EVENTTYPE.ARRIVAL) // If the event is an arrival { // Find shortest line and add registrant shortLine = GetShortLine(); shortLine.Enqueue(EventPriorityQueue.Peek().CurrCustomer); // Increment if new largest queue found if (shortLine.Count > LargestQueue) { LargestQueue++; } // Generate departure event if this customer is at the front of the line if (shortLine.Count == 1) { LeaveEvent(shortLine.Peek()); } // Event completely handled; remove the event and add to counter EventPriorityQueue.Dequeue(); TotalArrivals++; TotalEvents++; } else // If the event is a departure { // Get the line that registrant is about to leave for (int i = 0; i < InLine.Count; i++) { if (InLine[i].Count > 0) { if (InLine[i].Peek().CustomerID == EventPriorityQueue.Peek().CurrCustomer.CustomerID) { Line = InLine[i]; break; } } } // Remove registrant from wait line Line.Dequeue(); TotalCheckouts++; TotalEvents++; // If the wait line is not empty, generate a departure event for the next registrant if (Line.Count > 0) { LeaveEvent(Line.Peek()); } // Departure event handled; remove from Priority Queue EventPriorityQueue.Dequeue(); } } // end RunSimulation