private string FindNearestLiftToGoDown(List <IElevator> elevators, ElevatorOutsideRequest currentRequest) { int noOfPendingRequest = 0; ElevatorOutsideRequest elevatorOutsideRequest = currentRequest as ElevatorOutsideRequest; try { foreach (Elevator el in elevators) { if (el.CurrentDirection == Direction.Down && elevatorOutsideRequest.MoveDirection == Direction.Down) { if (el.CurrentFloor.FloorNumber > elevatorOutsideRequest.CurrentFloor.FloorNumber) { var numberOfFloors = el.CurrentFloor.FloorNumber - elevatorOutsideRequest.CurrentFloor.FloorNumber; for (int i = 1; i < numberOfFloors; i++) { if ( (el.PendingRequestsInside.ConvertAll(s => s as ElevatorInsideRequest).Select(s => s.DestinationFloor.FloorNumber == (el.CurrentFloor.FloorNumber - i)).Count() == 1) || ((el.PendingRequestsOutside.ConvertAll(s => s as ElevatorOutsideRequest).Select(s => s.CurrentFloor.FloorNumber == (el.CurrentFloor.FloorNumber - i)).Count() == 1) && (el.PendingRequestsOutside.Select(s => s.MoveDirection == el.CurrentDirection).Count() == 1)) ) { noOfPendingRequest++; } } } var totalTime = noOfPendingRequest * stopageTimeforEachRequest + ((el.CurrentFloor.FloorNumber - elevatorOutsideRequest.CurrentFloor.FloorNumber) - noOfPendingRequest) * timeForPassingAFloor; elevatorTimeList.Add(new TimeForEachElevator { ElevatorName = el.ElevatorName, Time = totalTime }); } } } catch (Exception ex) { throw ex; } return(pickLiftWithLowestTime(elevatorTimeList)); }
//Written the Nearest lifet based on the directions public string FindBestLift(List <IElevator> elevators, ElevatorOutsideRequest currentRequest) { string liftName = ""; // List<TimeForEachElevator> elevatorTimeList = new List<TimeForEachElevator>(); switch (currentRequest.MoveDirection) { case Direction.Down: { liftName = FindNearestLiftToGoDown(elevators, currentRequest); break; } case Direction.Up: { liftName = FindNearestLiftToGoUp(elevators, currentRequest); break; } } return(liftName); }
private void button1_Click(object sender, EventArgs e) { InitBuildingElevatorsAndFloors(); // Initialize a request from Floor 3 and press Down ElevatorOutsideRequest CurrentRequest = new ElevatorOutsideRequest(new Floor { FloorNumber = 3 }, Direction.Down); // 3 Elevators init building.Elevators[0].CurrentDirection = Direction.Down; building.Elevators[1].CurrentDirection = Direction.Up; building.Elevators[2].CurrentDirection = Direction.Down; try { //get the best fit elevator based on the conditions var elevatorName = building.FindBestLift(CurrentRequest); MessageBox.Show("Elevator Selected = " + elevatorName.ToString()); } catch (Exception ex) { log.Error(ex.Message); } }