//Through this method we will analyze the scores of the method above and select the best one public BestElevatorInfo checkIfElevatorISBetter(int scoreToCheck, Elevator newElevator, BestElevatorInfo bestElevatorInfo, int _requestedFloor) { if (scoreToCheck < bestElevatorInfo.bestScore) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referanceGap = Math.Abs(newElevator.currentFloor - _requestedFloor); } else if (bestElevatorInfo.bestScore == scoreToCheck) { int gap = Math.Abs(newElevator.currentFloor - _requestedFloor); if (bestElevatorInfo.referanceGap > gap) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referanceGap = gap; } } return(bestElevatorInfo); }
//Comparing elevator to previous best public BestElevatorInfo checkBestElevator(int scoreToCheck, Elevator newElevator, BestElevatorInfo bestElevatorInfo, int floor) { //If elevators situation is more favourable, set to best elevator if (scoreToCheck < bestElevatorInfo.bestScore) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referenceGap = Math.Abs(newElevator.currentFloor - floor); //If elevators are in a similar situation, set the closest one to the best elevator } else if (bestElevatorInfo.bestScore == scoreToCheck) { int gap = Math.Abs(newElevator.currentFloor - floor); if (bestElevatorInfo.referenceGap > gap) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referenceGap = gap; } } return(bestElevatorInfo); }
//Through this method we will score the best elevator, taking into account proximity, direction and its status public Elevator findElevator(int _requestedFloor, String _direction) { BestElevatorInfo bestElevatorInfo = new BestElevatorInfo(null, 7, 10000000); if (_requestedFloor == 1) { foreach (Elevator elevator in this.elevatorsList) { //The elevator is at the lobby and already has some requests. It is about to leave but has not yet departed if (_requestedFloor == elevator.currentFloor && elevator.status == "stopped") { bestElevatorInfo = checkIfElevatorISBetter(1, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is at the lobby and has no requests else if (_requestedFloor == elevator.currentFloor && elevator.status == "idle") { bestElevatorInfo = checkIfElevatorISBetter(2, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is lower than me and is coming up. It means that I'm requesting an elevator to go to a basement, and the elevator is on it's way to me. else if (_requestedFloor > elevator.currentFloor && elevator.direction == "up") { bestElevatorInfo = checkIfElevatorISBetter(3, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is above me and is coming down. It means that I'm requesting an elevator to go to a floor, and the elevator is on it's way to me else if (_requestedFloor < elevator.currentFloor && elevator.direction == "down") { bestElevatorInfo = checkIfElevatorISBetter(3, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is not at the first floor, but doesn't have any request else if (elevator.status == "idle") { bestElevatorInfo = checkIfElevatorISBetter(4, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is not available, but still could take the call if nothing better is found else { bestElevatorInfo = checkIfElevatorISBetter(5, elevator, bestElevatorInfo, _requestedFloor); } } } else { foreach (Elevator elevator in this.elevatorsList) { //The elevator is at the same level as me, and is about to depart to the first floor if (_requestedFloor == elevator.currentFloor && elevator.status == "stopped" && _direction == elevator.direction) { bestElevatorInfo = checkIfElevatorISBetter(1, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is lower than me and is going up. I'm on a basement, and the elevator can pick me up on it's way else if (_requestedFloor > elevator.currentFloor && elevator.direction == "up" && _direction == "up") { bestElevatorInfo = checkIfElevatorISBetter(2, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is higher than me and is going down. I'm on a floor, and the elevator can pick me up on it's way else if (_requestedFloor < elevator.currentFloor && elevator.direction == "down" && _direction == "down") { bestElevatorInfo = checkIfElevatorISBetter(2, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is idle and has no requests else if (elevator.status == "idle") { bestElevatorInfo = checkIfElevatorISBetter(4, elevator, bestElevatorInfo, _requestedFloor); } //The elevator is not available, but still could take the call if nothing better is found else { bestElevatorInfo = checkIfElevatorISBetter(5, elevator, bestElevatorInfo, _requestedFloor); } } } return(bestElevatorInfo.bestElevator); }
//Find best elevator to send public Elevator findBestElevator(int floor, string direction) { int requestedFloor = floor; string requestedDirection = direction; var bestElevatorInfo = new BestElevatorInfo(null, 6, 1000000); if (requestedFloor == 1) { foreach (Elevator elevator in this.elevatorsList) { //Elevator is at lobby with some requests, and about to leave but has not yet if (1 == elevator.currentFloor && elevator.status == "stopped") { this.checkBestElevator(1, elevator, bestElevatorInfo, requestedFloor); //Elevator is at lobby with no requests } else if (1 == elevator.currentFloor && elevator.status == "idle") { this.checkBestElevator(2, elevator, bestElevatorInfo, requestedFloor); //Elevator is lower than user and moving up. Shows user is requesting to go to basement, and elevator is moving to them. } else if (1 > elevator.currentFloor && elevator.direction == "up") { this.checkBestElevator(3, elevator, bestElevatorInfo, requestedFloor); //Elevator is higher than user and moving down. Shows user is requesting to go to a floor, and elevator is moving to them. } else if (1 < elevator.currentFloor && elevator.direction == "down") { this.checkBestElevator(3, elevator, bestElevatorInfo, requestedFloor); //Elevator is not at lobby floor, but has no requests } else if (elevator.status == "idle") { this.checkBestElevator(4, elevator, bestElevatorInfo, requestedFloor); //Elevator is last resort } else { this.checkBestElevator(5, elevator, bestElevatorInfo, requestedFloor); } } } else { foreach (Elevator elevator in this.elevatorsList) { //Elevator is at floor going to lobby if (requestedFloor == elevator.currentFloor && elevator.status == "stopped" && requestedDirection == elevator.direction) { this.checkBestElevator(1, elevator, bestElevatorInfo, requestedFloor); //Elevator is lower than user and moving through them to destination } else if (requestedFloor > elevator.currentFloor && elevator.direction == "up" && requestedDirection == "up") { this.checkBestElevator(2, elevator, bestElevatorInfo, requestedFloor); //Elevator is higher than user and moving through them to destination } else if (requestedFloor < elevator.currentFloor && elevator.direction == "down" && requestedDirection == "down") { this.checkBestElevator(2, elevator, bestElevatorInfo, requestedFloor); //Elevator is idle } else if (elevator.status == "idle") { this.checkBestElevator(3, elevator, bestElevatorInfo, requestedFloor); //Elevator is last resort } else { this.checkBestElevator(4, elevator, bestElevatorInfo, requestedFloor); } } } return(bestElevatorInfo.bestElevator); }