Design an Elevator System

Hard
State MachinesStateStrategyObserver

Problem Statement

Design an elevator system for a building that:

  • Manages multiple elevators
  • Dispatches elevators to floors based on requests
  • Handles both internal (inside elevator) and external (floor) button presses
  • Minimizes wait time using a scheduling algorithm

Design Requirements

Key Entities:

  • ElevatorSystem — Central controller (Singleton)
  • Elevator — Has state, direction, current floor
  • ElevatorCar — Physical cab with buttons and display
  • Floor — Has up/down call buttons
  • Request — Internal (go to floor X) or external (call from floor Y)
  • Dispatcher — Algorithm that assigns elevators to requests

Elevator States: Idle, Moving Up, Moving Down, Door Open

Key Behaviors:

  • callElevator(floor, direction) — External request from a floor
  • selectFloor(elevatorId, floor) — Internal request inside elevator
  • Dispatcher should use SCAN algorithm (elevator serves floors in one direction before reversing)

!Constraints

- Building has 1–50 floors - 1–8 elevators per building - Each elevator has a max capacity - Door stays open for 3 seconds - Elevator moves at 1 floor/second - SCAN scheduling: serve all requests in current direction before reversing
Output

Run your code to see output here.