How Elevators Are Programmed — A Simple Java Simulation Explained
We use elevators (lifts) every day — in offices, malls, hospitals, and apartments. We press buttons, wait, and the elevator somehow knows exactly where to go.
Have you ever wondered:
How does an elevator decide which floor to visit first?
How does it know when to go up or down?
In this blog, we will understand elevator logic using a simple Java simulation. No complex hardware, no advanced system design — just clean programming logic.
Basic Elevator Rules (Simplified)
To keep things beginner-friendly, we will assume:
- Only one elevator
- One request list at a time
- Elevator moves floor by floor
- No emergency or priority floors
Even with these rules, you will clearly see how real-life systems are programmed.
What Information Does an Elevator Need?
- Current floor
- Requested destination floors
- Direction of movement (up or down)
Based on this data, the elevator continuously decides its next move.
Example Scenario
Current Floor: 2
Requested Floors: 5, 1
Logical behavior:
- Go up first and serve higher floors
- Then come down to serve lower floors
Expected Elevator Movement
Moving Up...
Floor 3
Floor 4
Floor 5 (Stopping)
Moving Down...
Floor 4
Floor 3
Floor 2
Floor 1 (Stopping)
Java Program — Elevator Simulation
import java.util.*;
public class ElevatorSimulation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter current floor: ");
int currentFloor = sc.nextInt();
System.out.print("Enter number of requests: ");
int n = sc.nextInt();
List requests = new ArrayList<>();
System.out.println("Enter requested floors:");
for (int i = 0; i < n; i++) {
requests.add(sc.nextInt());
}
// Separate upward and downward requests
List up = new ArrayList<>();
List down = new ArrayList<>();
for (int floor : requests) {
if (floor > currentFloor) {
up.add(floor);
} else if (floor < currentFloor) {
down.add(floor);
}
}
Collections.sort(up);
Collections.sort(down, Collections.reverseOrder());
// Move Up
if (!up.isEmpty()) {
System.out.println("\nMoving Up...");
for (int floor : up) {
for (int i = currentFloor + 1; i <= floor; i++) {
System.out.println("Floor " + i);
}
System.out.println("Floor " + floor + " (Stopping)");
currentFloor = floor;
}
}
// Move Down
if (!down.isEmpty()) {
System.out.println("\nMoving Down...");
for (int floor : down) {
for (int i = currentFloor - 1; i >= floor; i--) {
System.out.println("Floor " + i);
}
System.out.println("Floor " + floor + " (Stopping)");
currentFloor = floor;
}
}
}
}
Sample Input
Enter current floor: 2
Enter number of requests: 2
Enter requested floors:
5
1
Sample Output
Moving Up...
Floor 3
Floor 4
Floor 5 (Stopping)
Moving Down...
Floor 4
Floor 3
Floor 2
Floor 1 (Stopping)
Why This Logic Works
- Minimizes unnecessary direction changes
- Mimics real elevator behavior
- Easy to scale and improve
Practice Challenges
- Add validation so invalid floors are rejected.
- Modify the program to handle multiple elevators.
- Add priority floors like emergency or maintenance floors.
This is a simple demonstration of how programming logic powers everyday systems. Real elevator software is more complex, but the core idea remains the same.