Project 6 — Design Elevator System
Project 6 — Design Elevator System
Section titled “Project 6 — Design Elevator System”Problem: Design an elevator control system that manages multiple elevators across multiple floors, handling requests efficiently.
Requirements
Section titled “Requirements”| Requirement | Details |
|---|---|
| Multiple elevators | N elevators serving M floors |
| Floor requests | Users press up/down on floors |
| Cabin requests | Users press floor number inside elevator |
| Scheduling | Assign nearest available elevator |
| States | Moving up, moving down, idle, door open |
Class Design
Section titled “Class Design”classDiagram class ElevatorSystem { -elevators: Elevator[] +requestFloor(floor: int, direction: Direction): void +selectFloor(elevatorId: int, floor: int): void +step(): void +getStatus(): SystemStatus } class Elevator { +id: int +currentFloor: int +direction: Direction +state: ElevatorState -stops: SortedSet~int~ +addStop(floor: int): void +move(): void +openDoor(): void +closeDoor(): void } class Direction { <<enumeration>> UP DOWN NONE } class ElevatorState { <<enumeration>> MOVING IDLE DOOR_OPEN DOOR_CLOSED } class Request { +floor: int +direction: Direction +timestamp: Date } class SchedulingStrategy { <<interface>> +schedule(elevators: Elevator[], request: Request): Elevator } class NearestElevatorStrategy { +schedule(elevators: Elevator[], request: Request): Elevator }
ElevatorSystem *-- Elevator ElevatorSystem --> SchedulingStrategy SchedulingStrategy <|.. NearestElevatorStrategy ElevatorSystem *-- RequestDesign Patterns Used
Section titled “Design Patterns Used”| Pattern | Where | Why |
|---|---|---|
| Strategy | Scheduling algorithm | Different scheduling strategies |
| Singleton | ElevatorSystem | One control system |
JavaScript Example
Section titled “JavaScript Example”class Elevator { constructor(id, totalFloors) { this.id = id; this.currentFloor = 0; this.direction = 'NONE'; this.state = 'IDLE'; this.stops = new Set(); }
addStop(floor) { this.stops.add(floor); this.direction = floor > this.currentFloor ? 'UP' : 'DOWN'; }
move() { if (this.stops.size === 0) { this.direction = 'NONE'; this.state = 'IDLE'; return; } this.state = 'MOVING'; this.currentFloor += this.direction === 'UP' ? 1 : -1;
if (this.stops.has(this.currentFloor)) { this.stops.delete(this.currentFloor); this.state = 'DOOR_OPEN'; // After delay, door closes setTimeout(() => { this.state = 'DOOR_CLOSED'; }, 2000); } }
distanceFrom(floor) { return Math.abs(this.currentFloor - floor); }}
class ElevatorSystem { constructor(numElevators, totalFloors) { this.elevators = Array.from({ length: numElevators }, (_, i) => new Elevator(i, totalFloors)); }
requestFloor(floor, direction) { // Find nearest idle or moving-same-direction elevator let best = null; let minDist = Infinity;
for (const e of this.elevators) { if (e.state === 'IDLE' || e.direction === direction) { const dist = e.distanceFrom(floor); if (dist < minDist) { minDist = dist; best = e; } } }
if (best) best.addStop(floor); }}Interview Questions
Section titled “Interview Questions”- How would you handle peak hours (office opening/closing)?
- How would you implement emergency override (fire alarm)?
- How do you prevent elevators from going to the same floor?
- How would you add priority for handicapped or VIP users?