Skip to content

OOP: Elevator Design

Mani Bhushan edited this page Sep 6, 2016 · 5 revisions

Design an elevator control system

Interfaces:
-----------------
IElevator:
+ moveUp();
+ moveDown();
+ addDestination();

+ ElevatorStatus status();
+ ElevatorDirection direction();
---------------------

IElevatorControl:
+ pickUp(Integer pickUpFloor);
+ destination(Integer elevatorId, Integer destinationFloor);
+ step();
Enums:
ElevatorStatus:
+ ELEVATOR_BUSY,
+ ELEVATOR_FREE;
-----------------------

ElevatorDirection:
+ ELEVATOR_UP,
+ ELEVATOR_DOWN,
+ ELEVATOR_HOLD;
Classes:
Elevator implements IElevator:
- currentFloor: Integer
- targetFloor: PriorityQueue<Integer> //smallest to largest if UP, else reverse.

public Elevator(int currentFloor) {
		this.currentFloor = currentFloor;
		this.targetFloor = new PriorityQueue<Integer>(10);
}

public int nextDestination() {
		return this.targetFloor.peek();
}

public int currentFloor() {
		return this.currentFloor;
}

public void popDestination() {
		this.targetFloor.poll();
}

       @Override
	public void moveUp() {
		++this.currentFloor;
	}

	@Override
	public void moveDown() {
		--this.currentFloor;
	}

	@Override
	public void addDestination(Integer destination) {
		this.targetFloor.add(destination);
	}

        @Override
	public ElevatorDirection direction() {
		if (this.targetFloor.size() > 0) {
			if (this.currentFloor < this.targetFloor.peek()) {
				return ElevatorDirection.ELEVATOR_UP;
			} else if (this.currentFloor > this.targetFloor.peek()) {
				return ElevatorDirection.ELEVATOR_DOWN;
			}
		}
		return ElevatorDirection.ELEVATOR_HOLD;
	}

	@Override
	public ElevatorStatus status() {
		return this.targetFloor.size() > 0 ? ElevatorStatus.ELEVATOR_BUSY: ElevatorStatus.ELEVATOR_FREE;
	}

Clone this wiki locally