// Find best elevator in the chosen column public Elevator ElevatorInTheChosenColumn(Column columnFinded, int requestedFloor, String direction, int userCurrentFloor) { var bestNearestElevatorGap = 10000000; Elevator bestElevator = null; var bestCase = 0; foreach (Elevator elevator in columnFinded.listElevInColumn) { // Condition 1 if (userCurrentFloor == elevator.position) { // BestCase 1 if (bestCase == 0 || bestCase > 1) { bestCase = 1; bestElevator = elevator; // if user is at floor 1 } else if (bestCase == 1 && requestedFloor > 1) { var gap = Math.Abs(elevator.position - 1); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } // if user is not at floor 1 else if (bestCase == 1 && requestedFloor != 1) { var gap = Math.Abs(elevator.position - requestedFloor); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } } // condition 2 else if (elevator.position > userCurrentFloor && requestedFloor == 1) { // BestCase 2 if (bestCase == 0 || bestCase > 2) { bestCase = 2; bestElevator = elevator; }// if user is at floor 1 else if (bestCase == 2 && requestedFloor > 2) { var gap = Math.Abs(elevator.position - 1); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } // if user is not at floor 1 } else if (bestCase == 2 && requestedFloor != 1) { var gap = Math.Abs(elevator.position - requestedFloor); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } } else if (elevator.position > 1 && direction == "down" && elevator.direction == "down") { // BestCase 3 if (bestCase == 0 || bestCase > 3) { bestCase = 3; bestElevator = elevator; }// if user is at floor 1 else if (bestCase == 3 && requestedFloor > 1) { var gap = Math.Abs(elevator.position - 1); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } // if user is not at floor 1 } else if (bestCase == 3 && requestedFloor != 1) { var gap = Math.Abs(elevator.position - requestedFloor); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } } // Condition 3 else if (elevator.position > 1 && direction == "up" && elevator.direction == "down") { // BestCase 3 if (bestCase == 0 || bestCase > 4) { bestCase = 4; bestElevator = elevator; }// if user is at floor 1 else if (bestCase == 4 && requestedFloor > 1) { var gap = Math.Abs(elevator.position - 1); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } // if user is not at floor 1 } else if (bestCase == 4 && requestedFloor != 1) { var gap = Math.Abs(elevator.position - requestedFloor); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } } // Condition 4 else if (elevator.direction == "idle") { // BestCase 4 if (bestCase == 0 || bestCase > 4) { bestCase = 4; bestElevator = elevator; } // if user is at floor 1 else if (bestCase == 4 && requestedFloor > 1) { var gap = Math.Abs(elevator.position - 1); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } // if user is not at floor 1 else if (bestCase == 4 && requestedFloor != 1) { var gap = Math.Abs(elevator.position - requestedFloor); if (bestNearestElevatorGap >= gap) { bestElevator = elevator; bestNearestElevatorGap = gap; } } } } bestElevator.moveToUserCurrentFloor(userCurrentFloor); bestElevator.moveToRequestedFloor(requestedFloor); return(bestElevator); }
static void Main(string[] args) { // elevators in columnOne (BASEMENT) Elevator elevatorA1 = new Elevator(1, "idle", -4, new List <int>()); Elevator elevatorA2 = new Elevator(2, "idle", 1, new List <int>()); Elevator elevatorA3 = new Elevator(3, "down", -5, new List <int>()); Elevator elevatorA4 = new Elevator(4, "up", 1, new List <int>()); Elevator elevatorA5 = new Elevator(5, "down", -6, new List <int>()); // elevators in columnTwo Elevator elevatorB6 = new Elevator(6, "down", 20, new List <int>()); Elevator elevatorB7 = new Elevator(7, "up", 3, new List <int>()); Elevator elevatorB8 = new Elevator(8, "down", 13, new List <int>()); Elevator elevatorB9 = new Elevator(9, "down", 15, new List <int>()); Elevator elevatorB10 = new Elevator(10, "down", 6, new List <int>()); // elevators in columnThree Elevator elevatorC11 = new Elevator(11, "up", 1, new List <int>()); Elevator elevatorC12 = new Elevator(12, "up", 23, new List <int>()); Elevator elevatorC13 = new Elevator(13, "down", 33, new List <int>()); Elevator elevatorC14 = new Elevator(14, "down", 40, new List <int>()); Elevator elevatorC15 = new Elevator(15, "down", 39, new List <int>()); // elevators in columnFour Elevator elevatorD16 = new Elevator(16, "down", 58, new List <int>()); Elevator elevatorD17 = new Elevator(17, "up", 50, new List <int>()); Elevator elevatorD18 = new Elevator(18, "up", 46, new List <int>()); Elevator elevatorD19 = new Elevator(19, "up", 1, new List <int>()); Elevator elevatorD20 = new Elevator(20, "down", 60, new List <int>()); // -------column------- // (int id, List<Elevator> listElevInColumn, int maxFloor, int minFloor) Column columnOne = new Column(1, new List <Elevator>(), 1, -6); Column columnTwo = new Column(2, new List <Elevator>(), 2, 20); Column columnThree = new Column(3, new List <Elevator>(), 40, 21); Column columnFour = new Column(4, new List <Elevator>(), 60, 41); // add the elevator in the list columnOne.listElevInColumn.Add(elevatorA1); columnOne.listElevInColumn.Add(elevatorA2); columnOne.listElevInColumn.Add(elevatorA3); columnOne.listElevInColumn.Add(elevatorA4); columnOne.listElevInColumn.Add(elevatorA5); columnTwo.listElevInColumn.Add(elevatorB6); columnTwo.listElevInColumn.Add(elevatorB7); columnTwo.listElevInColumn.Add(elevatorB8); columnTwo.listElevInColumn.Add(elevatorB9); columnTwo.listElevInColumn.Add(elevatorB10); columnThree.listElevInColumn.Add(elevatorC11); columnThree.listElevInColumn.Add(elevatorC12); columnThree.listElevInColumn.Add(elevatorC13); columnThree.listElevInColumn.Add(elevatorC14); columnThree.listElevInColumn.Add(elevatorC15); columnFour.listElevInColumn.Add(elevatorD16); columnFour.listElevInColumn.Add(elevatorD17); columnFour.listElevInColumn.Add(elevatorD18); columnFour.listElevInColumn.Add(elevatorD19); columnFour.listElevInColumn.Add(elevatorD20); // -------battery------- // (int id, int nbOfColumnInBattery, int nbOfBasement, List<Column> listColumnInBattery) Battery batteryOne = new Battery(1, 4, 6, new List <Column>()); // add the columns in the list. batteryOne.listColumnInBattery.Add(columnOne); batteryOne.listColumnInBattery.Add(columnTwo); batteryOne.listColumnInBattery.Add(columnThree); batteryOne.listColumnInBattery.Add(columnFour); // ------------------------------- SCENARIO 1 ------------------------------------------------- batteryOne.requestElevator(20, "down", 1); Console.WriteLine("---------------------------"); // ------------------------------- SCENARIO 2 ------------------------------------------------- batteryOne.requestElevator(36, "up", 1); Console.WriteLine("---------------------------"); // ------------------------------- SCENARIO 3 ------------------------------------------------- batteryOne.requestElevator(1, "down", 54); Console.WriteLine("----------------------------"); // ------------------------------- SCENARIO 4 ------------------------------------------------- batteryOne.requestElevator(1, "up", -3); Console.WriteLine("----------------------------"); }