How Elevators Are Programmed — A Simple Java Simulation

Understand how elevators work behind the scenes by simulating real-life lift logic using a simple Java program and step-by-step explanation.

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:

Even with these rules, you will clearly see how real-life systems are programmed.

What Information Does an Elevator Need?

Based on this data, the elevator continuously decides its next move.

Example Scenario

Current Floor: 2

Requested Floors: 5, 1

Logical behavior:

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

Practice Challenges

  1. Add validation so invalid floors are rejected.
  2. Modify the program to handle multiple elevators.
  3. 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.