Esempio n. 1
0
 public void CloseDoor()
 {
     Elevator.CloseDoor();
 }
Esempio n. 2
0
 public void OpenDoor()
 {
     Elevator.OpenDoor();
 }
Esempio n. 3
0
 public Initialiser(Elevator elevator, Floor floor, IUserStatus userStatus)
 {
     _elevator   = elevator;
     _floor      = floor;
     _userStatus = userStatus;
 }
        public static Elevator Start(List <Elevator> elevatorList, int floor, int requestDirection)
        {
            List <int> elevatorFloors = new List <int>();

            foreach (var elev in elevatorList)
            {
                elevatorFloors.Add(elev.Floor);
            }

            // Assess' which elevators are available to handle the request.

            List <Elevator> availableElevators = new List <Elevator>();

            foreach (var elev in elevatorList)
            {
                var pse        = elev.isPaused == false && elev.MaintenanceMode == false;
                var stationary = elev.stopQueue.Count == 0 && elev.direction == 0;

                if (pse && stationary)
                {
                    availableElevators.Add(elev);                    // If it is stationary and is not paused or in maintenance mode it is available.
                }
                if (!pse && !stationary)
                {
                    // If the elevator is going to pass the request floor it is available.
                    if (elev.direction == 1 && elev.stopQueue.Min() > floor && requestDirection == elev.direction)
                    {
                        availableElevators.Add(elev);
                    }
                    else if (elev.direction == -1 && elev.stopQueue.Max() < floor && requestDirection == elev.direction)
                    {
                        availableElevators.Add(elev);
                    }
                }
            }


            // Gets the distance between the available elevators and the request floor.
            foreach (var elev in availableElevators)
            {
                ElevatorDistances(elev, floor);
            }

            // Gets the closest elevator, and returns that elevator as a result.
            Elevator elevToSend = ClosestDistance();

            return(elevToSend);

            // Get the distance between each elevator and the requested floor.
            void ElevatorDistances(Elevator elevator, int f)
            {
                ElevatorDistancesDict.Add(elevator, Math.Abs(elevator.Floor - f));
            }

            // Select the closest elevator.
            Elevator ClosestDistance()
            {
                Elevator closestElev = ElevatorDistancesDict.Where(e => e.Value == ElevatorDistancesDict.Min(e2 => e2.Value)).First().Key;

                ElevatorDistancesDict.Clear();
                return(closestElev);
            }
        }
 public ElevatorProcessing(Floor floor, Elevator elevator)
 {
     _floor    = floor;
     _elevator = elevator;
 }
Esempio n. 6
0
 public UserRequest(Elevator elevator, Floor floor, IStatusRandomiser randomiser)
 {
     _elevator   = elevator;
     _floor      = floor;
     _randomiser = randomiser;
 }
 // Translates the input from the command line and performs desired operations.
 public static void Start(string[] Inputs, List <Elevator> elevators, int floor)
 {
     if (Inputs[0] == "exit" || Inputs[0] == "quit")
     {
         Environment.Exit(0);
     }
     if (Inputs[0] == "floor")
     {
         if (Inputs.Length > 1)
         {
             Int32.TryParse(Inputs[1], out int newfloor);
             Program.SetFloor(newfloor);
         }
         Console.WriteLine("You are now on Floor " + Program.Floor.ToString());
         return;
     }
     if (Inputs[0] == "status")
     {
         foreach (var elevator in elevators)
         {
             Status(elevator);
         }
         return;
     }
     if (Inputs[0] == "call")
     {
         if (Inputs[1] == "elevator")
         {
             if (Inputs.Length >= 3)
             {
                 if (Inputs[2] == "floor")
                 {
                     Int32.TryParse(Inputs[3], out int newfloor);
                     Program.SetFloor(newfloor);
                 }
                 else if (Inputs[2] == "up")
                 {
                     Elevator e = ElevatorDispatcher.Start(elevators, floor, 1); Program.AddToStopQueue(e, floor); DistributionEngine.Start(e);  return;
                 }
                 else if (Inputs[2] == "down")
                 {
                     Elevator e = ElevatorDispatcher.Start(elevators, floor, -1); Program.AddToStopQueue(e, floor); DistributionEngine.Start(e); return;
                 }
             }
             Elevator elevToSend = ElevatorDispatcher.Start(elevators, floor, 0);
             Program.AddToStopQueue(elevToSend, floor);
             DistributionEngine.Start(elevToSend);
             return;
         }
     }
     if (Inputs[0] == null || Inputs[0] == "")
     {
         Console.WriteLine("No Input Detected. Type 'help' for available commands.");
         return;
     }
     if (Inputs[0] == "elevator" && Inputs[2] == "floor")
     {
         Int32.TryParse(Inputs[3], out floor);
         Int32.TryParse(Inputs[1], out int elevNum);
         Elevator elevToSend = elevators.SingleOrDefault(Elevator => Elevator.Id == elevNum);
         Program.AddToStopQueue(elevToSend, floor);
         DistributionEngine.Start(elevToSend);
     }
     else if (Inputs[0] == "toggle" && Inputs[1] == "system" && Inputs[2] == "maintenance")
     {
         Program.ToggleSysMaintenance();
     }
     else if (Inputs[0] == "elevator" && Inputs[2] == "maintenancemode")
     {
         Int32.TryParse(Inputs[1], out int elevNum);
         Elevator elevToToggle = elevators.SingleOrDefault(Elevator => Elevator.Id == elevNum);
         elevToToggle.MaintenanceMode = !elevToToggle.MaintenanceMode;
         Console.WriteLine("Elevator " + elevToToggle.Id.ToString() + " Maintenance Mode set to " + elevToToggle.MaintenanceMode.ToString() + ".");
     }
     else
     {
         if (Inputs[0] == "help")
         {
             Console.WriteLine("Type 'Floor *' to specify what floor you are on. (* = num) \n" +
                               "Type 'Floor' to see what floor you are on. \n" +
                               "Type 'Call Elevator Up/Down' to request an elevator to your floor. \n" +
                               "Type 'Elevator * Maintenance Mode' to toggle maintenance mode. (* = num) \n" +
                               "Type 'Elevator * Floor *' to add a floor to a specific elevator stops. (* = num) \n" +
                               "Type 'Toggle System Maintenance' to toggle system maintenance mode. \n" +
                               "Type 'Status' to see where each elevator is.");
             return;
         }
         else
         {
             Console.WriteLine("Command not recognized. Type 'help' for available commands.");
             return;
         }
     }
 }
 public static void Start(Elevator elevator)
 {
     while (true)
     {
         if (elevator.stopQueue.Count > 0 && elevator.MaintenanceMode == false && Program.SysMaintenance == false)
         {
             // Sets the elevator direction by comparing its current floor against its stop queue. 0 = Stationary / 1 = Ascending / -1 = Decending.
             if (elevator.direction == 0)
             {
                 int floor = 0;
                 if (elevator.Floor < elevator.stopQueue.Min())
                 {
                     elevator.direction = 1;
                     floor = elevator.stopQueue.Min();
                 }
                 else if (elevator.Floor > elevator.stopQueue.Max())
                 {
                     elevator.direction = -1;
                     floor = elevator.stopQueue.Max();
                 }
             }
             // If the elevator is supposed to be ascending do so untill it reaches the lowest floor in its stop queue. Remove it from the stop queue, open the doors.
             if (elevator.direction == 1)
             {
                 Console.WriteLine("Moving Elevator " + elevator.Id.ToString() + " to floor " + elevator.stopQueue.Min().ToString());
                 do
                 {
                     Console.WriteLine("Elevator " + elevator.Id.ToString() + " passing by floor " + elevator.Floor.ToString() + ".");
                     elevator.Floor++;
                 } while (elevator.Floor != elevator.stopQueue.Min());
                 elevator.stopQueue.Remove(elevator.Floor);
                 Console.WriteLine("Elevator " + elevator.Id + " at floor " + elevator.Floor.ToString() + ".");
                 elevator.OpenDoors();
                 Task.Delay(5000);
                 elevator.CloseDoors();
             }
             // If the elevator is supposed to be decending do so untill it reaches the highest floor in its stop queue. Remove the floor from the stop queue, open the doors.
             else if (elevator.direction == -1)
             {
                 Console.WriteLine("Moving Elevator " + elevator.Id.ToString() + " to floor " + elevator.stopQueue.Max().ToString());
                 do
                 {
                     Console.WriteLine("Elevator " + elevator.Id.ToString() + " passing by floor " + elevator.Floor.ToString() + ".");
                     elevator.Floor--;
                 } while (elevator.Floor != elevator.stopQueue.Max());
                 elevator.stopQueue.Remove(elevator.Floor);
                 Console.WriteLine("Elevator " + elevator.Id + " at floor " + elevator.Floor.ToString());
                 elevator.OpenDoors();
                 Task.Delay(5000);
                 elevator.CloseDoors();
             }
             // Are there any outstanding stops left in the queue, if not anounce journey end, set elevator direction to stationary and exit the loop.
             if (elevator.stopQueue.Count == 0)
             {
                 elevator.direction = 0;
                 Console.WriteLine("Elevator " + elevator.Id + " has finished its journey.");
                 break;
             }
         }
     }
 }
 public StatusRandomiser(Floor floor, Elevator elevator)
 {
     _floor    = floor;
     _elevator = elevator;
 }