Elevator Emulator is a java program that depicts the functionality of an elevator in a programmatic way.
The elevator starts from ground 1 and welcomes the user with a greeting. It then asks for the destination floor no. and greets accordingly.
Example:
Enter Destination Floor
3
The door is closing…
Going up…2
Going up…3
Hello, opening the door, we are on 3
Enter destination floor
1
Door is closing…
Going down…3
Going down…2
Going down…1
Hello opening the door, we are on 1
Algorithm for simple elevator emulator:
1. First create a source floor variable named n1 and set it to 1.
2. Secondly, make a do-while loop that asks the user to enter the destination floor input named n2.
3. There may be two cases:-
Case 1) Destination floor is greater than source floor
In this case, the elevator has to go upwards. Thus, we will create a for loop which starts from the source floor and ends till destination one. The for loop increments by 1 each time.
Case 2) Destination floor is lesser than source floor
In this case, the elevator has to go downwards. Thus, we will create a for loop which starts from the source floor and ends till destination one. The for loop decrements by 1 each time.
4. We will simply greet the user by specifying that he/she has reached the destination floor.
Program for simple elevator emulator:-
package com.test; import java.util.Scanner; public class Elevator { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n1=1; do { System.out.println("Enter destination floor"); System.out.println("Door is closing..."); int n2=sc.nextInt(); int i; if(n2>n1) { for( i=n1;i<=n2;i++) System.out.println("Going up..."+i) ; i--; } else { for( i=n1;i>=n2;i--) System.out.println("Going down..."+i); i++; } System.out.println("Hello, opening the door, we are on "+i); n1=n2; }while(true); } }
Input:-
Enter Destination Floor
3
Door is closing…
Going up…2
Going up…3
Hello, opening the door, we are on 3
Enter destination floor
1
Door is closing…
Going down…3
Going down…2
Going down…1
Hello, opening the door, we are on 1
Enter destination floor
2
Door is closing…
Going up…1
Going up…2
Hello, opening the door, we are on 2
Enter destination floor
4
Door is closing…
Going up…2
Going up…3
Going up…4
Hello, opening the door, we are on 4