Designing an Elevator System


Requirements:

Sequnece Diagram
cache
System Components:

Key Design Considerations:

Code Implementation:
import java.util.*;
import java.util.concurrent.*;

// Enum to represent the direction of the elevator
enum Direction {
    UP, DOWN, IDLE
}
// Class representing a user request
class UserRequest {
    int sourceFloor;
    int destinationFloor;

    public UserRequest(int sourceFloor, int destinationFloor) {
        this.sourceFloor = sourceFloor;
        this.destinationFloor = destinationFloor;
    }
}
// Class representing the elevator
class Elevator {
    private int capacity;
    private int currentFloor;
    private Direction direction;
    private List requestQueue;

    public Elevator(int capacity) {
        this.capacity = capacity;
        this.currentFloor = 0; // Assume starts at floor 0
        this.direction = Direction.IDLE;
        this.requestQueue = new LinkedList<>();
    }

    public synchronized void addRequest(UserRequest request) {
        // Add a request to the elevator's queue
        if (requestQueue.size() < capacity) {
            requestQueue.add(request);
            System.out.println("Request added to elevator on floor " + currentFloor);
        } else {
            System.out.println("Elevator at capacity, request cannot be added.");
        }
    }

    public void processRequests() {
        // Process the elevator's request queue
        while (!requestQueue.isEmpty()) {
            UserRequest request = requestQueue.remove(0);
            moveToFloor(request.sourceFloor);
            System.out.println("Elevator moving to floor " + request.sourceFloor);
            moveToFloor(request.destinationFloor);
            System.out.println("Elevator moving to floor " + request.destinationFloor);
        }
    }

    private void moveToFloor(int floor) {
        // Simulate elevator movement
        while (currentFloor != floor) {
            if (floor > currentFloor) {
                currentFloor++;
                direction = Direction.UP;
            } else {
                currentFloor--;
                direction = Direction.DOWN;
            }
            System.out.println("Elevator moving " + direction + " to floor " + currentFloor);
        }
        direction = Direction.IDLE;
    }
}
// Class representing the ElevatorSystem
class ElevatorSystem {
    private List elevators;
    private ExecutorService executorService;

    public ElevatorSystem(int numElevators, int elevatorCapacity) {
        elevators = new ArrayList<>();
        for (int i = 0; i < numElevators; i++) {
            elevators.add(new Elevator(elevatorCapacity));
        }
        executorService = Executors.newFixedThreadPool(numElevators);
    }

    public void requestElevator(UserRequest request) {
        Elevator closestElevator = findClosestElevator(request.sourceFloor);
        executorService.submit(() -> {
            closestElevator.addRequest(request);
            closestElevator.processRequests();
        });
    }

    private Elevator findClosestElevator(int sourceFloor) {
        Elevator closestElevator = elevators.get(0);
        int minDistance = Math.abs(closestElevator.currentFloor - sourceFloor);

        for (Elevator elevator : elevators) {
            int distance = Math.abs(elevator.currentFloor - sourceFloor);
            if (distance < minDistance) {
                minDistance = distance;
                closestElevator = elevator;
            }
        }
        return closestElevator;
    }
}
// Main application to simulate elevator system usage
public class ElevatorApp {
    public static void main(String[] args) {
        ElevatorSystem elevatorSystem = new ElevatorSystem(3, 5); // 3 elevators, each with a capacity of 5 requests

        // Users requesting elevators
        elevatorSystem.requestElevator(new UserRequest(0, 5));
        elevatorSystem.requestElevator(new UserRequest(2, 7));
        elevatorSystem.requestElevator(new UserRequest(5, 1));
    }
}

Explanation of the Code:

Design Considerations:

Improvements & Extensions: