public void AssignElevator(int RequestedFloor) { // Finding the proper column Column currentColumn = this.columnList[0]; foreach (var item in this.columnList) { if (RequestedFloor >= item.floorList[1] && RequestedFloor <= item.floorList.Last()) { currentColumn = item; break; } } Console.WriteLine("the column ID for floor {0} is {1}", RequestedFloor, currentColumn.ID); int DistanceToGo = 1000; Elevator BestElevator = currentColumn.elevatorList[0]; string UserDirection; if (RequestedFloor > 1) { UserDirection = "UP"; } else { UserDirection = "DOWN"; } // Finding the best elevator based on comparing DistanceToGo foreach (Elevator elevator in currentColumn.elevatorList) { int currentDestination; if (elevator.Direction == "IDLE") { currentDestination = 0; } else { currentDestination = elevator.StopList.Last(); } Console.Write("Elevator ID = {0}, PositonOfElevator = {1}, DirectionOfElevator = {2}, current Destination = {3}", elevator.ID, elevator.Position, elevator.Direction, currentDestination); int distance = CalculateDistanceToGo(elevator, 1, UserDirection); Console.WriteLine(". distanceToGo: {0}", distance); if (distance <= DistanceToGo) { DistanceToGo = distance; BestElevator = elevator; } } Console.WriteLine("*** Take the column {0}, elevator {1} *** ", currentColumn.ID, BestElevator.ID); if (BestElevator.Position == 1) { UpdateList(BestElevator, BestElevator.StopList, RequestedFloor); } else { UpdateList(BestElevator, BestElevator.BufferList, RequestedFloor); if (RequestedFloor > 1) { BestElevator.BufferDirection = "DOWN"; } else { BestElevator.BufferDirection = "UP"; } } move(BestElevator); }
public void move(Elevator elevator) { while (elevator.StopList.Count > 0) { if (elevator.StopList[0] > elevator.Position) { elevator.Direction = "UP"; while (elevator.Position < elevator.StopList[0]) { elevator.Position += 1; if (elevator.Position != 0) { Console.WriteLine("Elevator {0} is at floor {1} ", elevator.ID, elevator.Position); Thread.Sleep(2000); } if (elevator.Position == elevator.floorList.Last()) { elevator.Direction = "IDLE"; } } elevator.Door = "OPENED"; for (int s = 5; s > 0; s--) { Console.Beep(); } Console.WriteLine("DOOR IS OPENED"); elevator.StopList.RemoveAt(0); } else { elevator.Direction = "DOWN"; while (elevator.Position > elevator.StopList.Last()) { elevator.Position -= 1; if (elevator.Position != 0) { Console.WriteLine("Elevator {0} is at floor {1} ", elevator.ID, elevator.Position); Thread.Sleep(2000); } if (elevator.Position == elevator.floorList.First()) { elevator.Direction = "IDLE"; } } elevator.Door = "OPENED"; Console.WriteLine("DOOR OPENING..."); for (int t = 5; t > 0; t--) { Thread.Sleep(1000); Console.WriteLine(t); } for (int s = 5; s > 0; s--) { Console.Beep(); } Console.WriteLine("DOOR IS OPENED"); elevator.StopList.RemoveAt(elevator.StopList.Count - 1); } elevator.Door = "CLOSED"; Console.WriteLine("DOOR IS CLOSING IN: "); for (int t = 5; t > 0; t--) { Thread.Sleep(1000); Console.WriteLine(t); } for (int s = 5; s > 0; s--) { Console.Beep(); } Console.WriteLine("DOOR CLOSED"); Thread.Sleep(2000); elevator.Direction = "IDLE"; } if (elevator.BufferList.Count > 0) { elevator.StopList = elevator.BufferList; elevator.Direction = elevator.BufferDirection; move(elevator); } else { elevator.Direction = "IDLE"; } }
public void RequestElevator(int FloorNumber) { // Finding the proper column Column currentColumn = this.columnList[0]; foreach (var item in this.columnList) { if (FloorNumber >= item.floorList[1] && FloorNumber <= item.floorList.Last()) { currentColumn = item; break; } } // determining the USER Direction string Direction; if (FloorNumber < 1) { Direction = "UP"; } else { Direction = "DOWN"; } Console.WriteLine("Serving column: columnID {0} from floor {1} to floor {2}", currentColumn.ID, currentColumn.floorList[1], currentColumn.floorList.Last()); // finding the closest elevator Based on on comparing DistanceToGo AND the following Priority: // 1- the moving elevator which is arriving to the user // 2- the IDLE elevator // 3- other elevators int DistanceToGo = 1000; int distance; List <Elevator> FirstPriority = new List <Elevator>(); List <Elevator> SecondPriority = new List <Elevator>(); List <Elevator> ThirdPriority = new List <Elevator>(); Elevator BestElevator = currentColumn.elevatorList[0]; foreach (Elevator elev in currentColumn.elevatorList) { int currentDestination; if (elev.Direction == "IDLE") { currentDestination = 0; } else { currentDestination = elev.StopList.Last(); } Console.Write("Elevator ID = {0}, PositonOfElevator = {1}, DirectionOfElevator = {2}, current Destination = {3}", elev.ID, elev.Position, elev.Direction, currentDestination); distance = CalculateDistanceToGo(elev, FloorNumber, Direction); Console.WriteLine(". distanceToGo: {0}", distance); if (elev.Direction == Direction) { if ((Direction == "UP" && elev.Position <= FloorNumber) | (Direction == "DOWN" && elev.Position >= FloorNumber)) { FirstPriority.Add(elev); } } else if (elev.Direction == "IDLE") { SecondPriority.Add(elev); } else { ThirdPriority.Add(elev); } } if (FirstPriority.Count > 0) { foreach (Elevator elev in FirstPriority) { distance = CalculateDistanceToGo(elev, FloorNumber, Direction); if (distance <= DistanceToGo) { DistanceToGo = distance; BestElevator = elev; } } } else if (SecondPriority.Count > 0) { foreach (Elevator elev in SecondPriority) { distance = CalculateDistanceToGo(elev, FloorNumber, Direction); if (distance <= DistanceToGo) { DistanceToGo = distance; BestElevator = elev; } } } else { foreach (Elevator elev in ThirdPriority) { distance = CalculateDistanceToGo(elev, FloorNumber, Direction); if (distance <= DistanceToGo) { DistanceToGo = distance; BestElevator = elev; } } } // Updating the STOPLIST of the selected elevator if (BestElevator.Direction == Direction | BestElevator.Direction == "IDLE") { if (BestElevator.Direction == "DOWN" && BestElevator.Position >= FloorNumber) { Console.Write(" Take column {0} ElevatorID: {1} which is currently at floor {2}. ", currentColumn.ID, BestElevator.ID, BestElevator.Position); Thread.Sleep(2000); UpdateList(BestElevator, BestElevator.StopList, FloorNumber); UpdateList(BestElevator, BestElevator.StopList, 1); Console.WriteLine(" StopList: [ " + string.Join(" | ", BestElevator.StopList) + " ]"); move(BestElevator); } else if (BestElevator.Direction == "UP" && BestElevator.Position <= FloorNumber) { Console.Write(" Take column {0} ElevatorID: {1} which is currently at floor {2}. ", currentColumn.ID, BestElevator.ID, BestElevator.Position); Thread.Sleep(2000); UpdateList(BestElevator, BestElevator.StopList, FloorNumber); UpdateList(BestElevator, BestElevator.StopList, 1); Console.WriteLine(" StopList: [ " + string.Join(" | ", BestElevator.StopList) + " ]"); move(BestElevator); } else if (BestElevator.Direction == "IDLE") { Console.Write(" Take ElevatorID: {0} which is currently at floor {1}. ", BestElevator.ID, BestElevator.Position); Thread.Sleep(2000); UpdateList(BestElevator, BestElevator.StopList, FloorNumber); UpdateList(BestElevator, BestElevator.StopList, 1); Console.WriteLine(" StopList: [ " + string.Join(" | ", BestElevator.StopList) + " ]"); move(BestElevator); } else { Console.Write(" Take ElevatorID: {0} which is currently at floor {1}. ", BestElevator.ID, BestElevator.Position); Thread.Sleep(2000); UpdateList(BestElevator, BestElevator.BufferList, FloorNumber); UpdateList(BestElevator, BestElevator.BufferList, 1); Console.Write(" StopList: [ " + string.Join(" | ", BestElevator.StopList) + " ]"); Console.WriteLine(" BufferLsit: [ " + string.Join(" | ", BestElevator.BufferList) + " ]"); if (FloorNumber > 1) { BestElevator.BufferDirection = "DOWN"; move(BestElevator); } else { BestElevator.BufferDirection = "UP"; move(BestElevator); } } } else { Console.Write(" Take ElevatorID: {0} which is currently at floor {1}. ", BestElevator.ID, BestElevator.Position); Thread.Sleep(2000); UpdateList(BestElevator, BestElevator.BufferList, FloorNumber); UpdateList(BestElevator, BestElevator.StopList, 1); Console.Write(" StopList: [ " + string.Join(" | ", BestElevator.StopList) + " ]"); Console.WriteLine(" BufferLsit: [ " + string.Join(" | ", BestElevator.BufferList) + " ]"); // For Basements if (FloorNumber > 1) { BestElevator.BufferDirection = "DOWN"; move(BestElevator); } // For other floors else { BestElevator.BufferDirection = "UP"; move(BestElevator); } } }