The design should support multiple elevators, concurrent requests, and different scheduling strategies.
Requirements
A correct elevator system should meet the following requirements.1. Support multiple elevators.
2. Allow users to request an elevator from any floor.
3. Allow passengers to select destination floors.
4. Move elevators in both directions.
5. Open and close doors at requested floors.
6. Assign the most suitable elevator for each request.
7. Support different scheduling algorithms.
Design
The system consists of an ElevatorController that manages multiple elevators. Each Elevator maintains its current floor, movement direction, door state, and pending requests.The controller selects an elevator using a scheduling strategy and forwards the request.
ElevatorController
β
ββββββββββββββββΌβββββββββββββββ
β β β
Elevator 1 Elevator 2 Elevator 3
β β β
Floor Queue Floor Queue Floor Queue
Java Implementation
The following implementation demonstrates a simplified elevator system that manages elevator movement, floor requests, and request assignment using object-oriented design.Direction
This enum represents the possible movement states of an elevator: moving up, moving down, or remaining idle.public enum Direction {
UP,
DOWN,
IDLE
}
Door State
This enum represents the current state of the elevator door, indicating whether it is open or closed.public enum DoorState {
OPEN,
CLOSED
}
Elevator
The Elevator class maintains the current floor, direction, door state, and pending floor requests. It processes requests one step at a time and opens the door when the destination floor is reached.import java.util.TreeSet;
public class Elevator {
private final int id;
private int currentFloor = 0;
private Direction direction = Direction.IDLE;
private DoorState doorState = DoorState.CLOSED;
private final TreeSet<Integer> requests =
new TreeSet<>();
public Elevator(int id) {
this.id = id;
}
public void addRequest(int floor) {
requests.add(floor);
}
public void move() {
if (requests.isEmpty()) {
direction = Direction.IDLE;
return;
}
int target = requests.first();
if (currentFloor < target) {
direction = Direction.UP;
currentFloor++;
} else if (currentFloor > target) {
direction = Direction.DOWN;
currentFloor--;
}
if (currentFloor == target) {
requests.remove(target);
doorState = DoorState.OPEN;
System.out.println("Elevator " + id + " reached floor " + currentFloor );
doorState = DoorState.CLOSED;
}
}
public int getCurrentFloor() {
return currentFloor;
}
public Direction getDirection() {
return direction;
}
public boolean isIdle() {
return direction == Direction.IDLE;
}
}
Elevator Controller
The ElevatorController manages multiple elevators and assigns each request to the nearest available elevator based on the current floor.import java.util.ArrayList;
import java.util.List;
public class ElevatorController {
private final List<Elevator> elevators = new ArrayList<>();
public ElevatorController(int count) {
for (int i = 1; i <= count; i++) {
elevators.add(new Elevator(i));
}
}
public void requestElevator(int floor) {
Elevator selected = elevators.get(0);
int minDistance = Math.abs(selected.getCurrentFloor() - floor);
for (Elevator elevator : elevators) {
int distance = Math.abs(elevator.getCurrentFloor() - floor);
if (distance < minDistance) {
selected = elevator;
minDistance = distance;
}
}
selected.addRequest(floor);
}
public void step() {
for (Elevator elevator : elevators) {
elevator.move();
}
}
}
Example
This example creates two elevators, submits requests for different floors, and repeatedly advances the simulation until all requests are served.public class Main {
public static void main(String[] args) {
ElevatorController controller = new ElevatorController(2);
controller.requestElevator(5);
controller.requestElevator(2);
for (int i = 0; i < 8; i++) {
controller.step();
}
}
}
Output:
Elevator 2 reached floor 2
Elevator 1 reached floor 5
Complexity
Finding the nearest elevator requires scanning all elevators. Adding a floor request to the TreeSet maintains sorted order.Time Complexity
Assign Elevator β O(n)
Add Floor Request β O(log m)
Move Elevator β O(1)
Space Complexity
O(m)
n is the number of elevators.
m is the number of pending requests.