// Sort requests, for added efficiency private void SortRequestsQueue() { // Remove any requests which are useless i.e. requests that are already on their desired floor foreach (var req in RequestsQueue.ToArray()) { if (req.Floor == CurrentFloor) { RequestsQueue.Remove(req); } } // Decide if elevator is going up, down or is staying idle SetMovement(); // Sort if (RequestsQueue.Count > 1) { if (Movement == "up") { // Sort the queue in ascending order RequestsQueue.Sort((x, y) => x.Floor.CompareTo(y.Floor)); // Push any request to the end of the queue that would require a direction change foreach (var req in RequestsQueue.ToArray()) { if (req.Direction != Movement || req.Floor < CurrentFloor) { RequestsQueue.Remove(req); RequestsQueue.Add(req); } } } else { // Reverse the sorted queue (will now be in descending order) RequestsQueue.Sort((x, y) => y.Floor.CompareTo(x.Floor)); // Push any request to the end of the queue that would require a direction change foreach (var req in RequestsQueue.ToArray()) { if (req.Direction != Movement || req.Floor > CurrentFloor) { RequestsQueue.Remove(req); RequestsQueue.Add(req); } } } } }
// Change properties of elevator in one line - USE ONLY FOR TESTING public void ChangeProperties(int newCurrentFloor, int newNextFloor) { CurrentFloor = newCurrentFloor; NextFloor = newNextFloor; if (CurrentFloor > NextFloor) { Movement = "down"; } else { Movement = "up"; } RequestsQueue.Add(new Request(NextFloor, Movement)); }
// Send new request to its request queue public void SendRequest(int stopFloor, string btnDirection) { var request = new Request(stopFloor, btnDirection); RequestsQueue.Add(request); }