using System.Collections.Generic;
public int Id { get; private set; }
public int CurrentFloor { get; private set; }
public Elevator(int id, int currentFloor = 1)
CurrentFloor = currentFloor;
public void MoveToFloor(int floor)
public int DistanceToFloor(int floor)
return Math.Abs(CurrentFloor - floor);
public override string ToString()
return $"Elevator {Id} is now on floor {CurrentFloor}";
private List<Elevator> elevators;
public Building(int numElevators = 4, int numFloors = 10)
elevators = new List<Elevator>();
for (int i = 0; i < numElevators; i++)
elevators.Add(new Elevator(i + 1));
public Elevator CallElevator(int floor)
Elevator closestElevator = elevators.OrderBy(e => e.DistanceToFloor(floor)).First();
closestElevator.MoveToFloor(floor);
public void DisplayStatus()
foreach (var elevator in elevators)
Console.WriteLine(elevator);
static void Main(string[] args)
Building building = new Building();
Console.WriteLine("\nBuilding status:");
building.DisplayStatus();
Console.Write("\nEnter the floor number to call the elevator (1-10) or 'q' to quit: ");
string userInput = Console.ReadLine().Trim().ToLower();
Console.WriteLine("Exiting...");
int floor = int.Parse(userInput);
if (floor < 1 || floor > 10)
Console.WriteLine("Invalid floor. Please enter a number between 1 and 10.");
Elevator selectedElevator = building.CallElevator(floor);
Console.WriteLine($"\nElevator {selectedElevator.Id} has been selected and moved to floor {floor}.");
Console.WriteLine("Invalid input. Please enter a valid floor number or 'q' to quit.");
Console.WriteLine($"An error occurred: {ex.Message}");