Skip to content

Commit f56c776

Browse files
authored
Merge pull request #13 from BjornMelin/feat/0.1.0/stack-queue-elevator
feat(elevator-system): Implement elevator simulation with MovingBox using Stack Queue
2 parents a9b66de + 6a86e73 commit f56c776

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package data_structures.linear.stack_queue_elevator;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Random;
7+
import java.util.Stack;
8+
9+
/**
10+
* Simple Elevator Driver to test Elevator and Building
11+
*
12+
* @author bjornmelin
13+
*/
14+
public class Driver {
15+
16+
/**
17+
* @param args the command line arguments
18+
*/
19+
public static void main(String[] args) {
20+
21+
// Creates a building 5 levels high using an array of queues starting at floor 0
22+
Queue<MovingBox>[] building = new Queue[5]; // Declares and creates an array of Queues
23+
for (int i = 0; i <= 4; i++) {
24+
building[i] = new LinkedList(); // Creates a linked list based queue inside each element of the array of
25+
// queues
26+
}
27+
System.out.println("A building has been created with " + building.length + " floors numbered floor 0-4\n");
28+
29+
// Creates 100 moving boxes #1-100 w/ random destination floors (2-5) (1-4 to
30+
// fit into the building array) in on the first floor
31+
Random gen = new Random(); // Allows us to create random destination floors
32+
MovingBox[] boxes = new MovingBox[100];
33+
for (int i = 0; i <= 99; i++) {
34+
boxes[i] = new MovingBox();
35+
boxes[i].setReferenceNumber(i); // Sets reference number for e/ moving box 1-100 sequentially
36+
boxes[i].setFloor(gen.nextInt(4 - 1 + 1) + 1); // Sets the floor number b/w 1-4
37+
building[0].add(boxes[i]); // Adds each moving box to the first floor of the building
38+
}
39+
System.out.println(
40+
"The current number of Moving Boxes on floor 1 of the building is: \t" + building[0].size() + "\n");
41+
System.out.println(
42+
"------THE FLOOR NUMBER EACH BOX ON THE FIRST FLOOR WANTS TO GO TO IS: ------" + "\n" + building[0]);
43+
System.out.println("\n\n\n");
44+
45+
// Uses a stack to move movingboxes between floors
46+
Stack<MovingBox> elevator = new Stack();
47+
int maxSize = 20; // Max amount of moving boxes on the elevator
48+
int delivered = 0;
49+
50+
while (delivered < boxes.length) {
51+
int isPeople = 0; // int used to denote the floor in which there are people
52+
53+
if (elevator.isEmpty()) // if elevator is empty, should go tofloor with moving boxes
54+
{
55+
for (int i = 0; i <= 4; i++) // Looks through 5 floors of building looking for one thats not empty
56+
{
57+
if (building[i].peek() == null) // If peek returns null, then there are no people on the floor. Move
58+
// to next floor
59+
{
60+
// Nothing happens if no people on the floor
61+
} else // if peek does not return null, then the floor has people on it. Save that
62+
// floor
63+
{
64+
isPeople = i; // If a floor has people on it, this shows that floor i has people
65+
}
66+
}
67+
} else // If not empty, peek() and use top box destination
68+
{
69+
// Think of this line below as checking the floor the first person in an
70+
// elevator is going to
71+
elevator.peek().getFloor(); // Looks at the floor of the box on top of the stack and gets the floor its
72+
// going to
73+
isPeople = elevator.peek().getFloor(); // Tells elevator there should be people on the floor the first
74+
// box is going to
75+
System.out.println("\n\n-------- NEXT FLOOR TO DELIVER TO: " + isPeople + " ---------");
76+
System.out.println("--- THE NUMBER OF BOXES ON ELEVATOR IS: " + elevator.size() + " ---");
77+
}
78+
79+
/**
80+
* When the Elevator stops at a floor:
81+
*/
82+
while (!elevator.isEmpty()) // While the elevator is not empty, dump all the boxes in the elevator out onto
83+
// that floor's queue
84+
{
85+
building[isPeople].add(elevator.pop()); // Goes to the floor the first box wants to go to and removes
86+
// everyone from elevator onto that floor
87+
}
88+
89+
// If box is on the correct floor, do NOT put it back on the elevator
90+
while (elevator.size() < maxSize && !building[isPeople].isEmpty()) // Fill elevator, loop pushes until the
91+
// elevator is FULL or queue for current
92+
// floor is empty
93+
{
94+
if (building[isPeople].peek().getFloor() == isPeople) {
95+
System.out.println("\tMoving Box: " + building[isPeople].peek().getReferenceNumber()
96+
+ " has been DELIVERED to traget floor.");
97+
building[isPeople].poll();
98+
delivered++;
99+
} else {
100+
System.out.println("\tPutting Moving Box: " + building[isPeople].peek().getReferenceNumber()
101+
+ " back onto the elevator. There target floor is: "
102+
+ building[isPeople].peek().getFloor());
103+
elevator.add(building[isPeople].poll()); // Removes people from the floor and adds them onto the
104+
// elevator
105+
}
106+
}
107+
}
108+
System.out.println("\n\n-----ALL MOVING BOXES HAVE BEEN DELIVERED-----");
109+
}
110+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package data_structures.linear.stack_queue_elevator;
2+
3+
/**
4+
* This is a Moving Box Object
5+
*
6+
* @author bjornmelin
7+
*/
8+
public class MovingBox {
9+
10+
private int referenceNumber; // Reference number to the moving box
11+
private int floor; // Floor to be delivered to
12+
13+
/**
14+
* Default constructor for Moving Box, initializes variables
15+
*/
16+
public MovingBox() {
17+
referenceNumber = 0; // reference number for each moving box
18+
floor = 0;
19+
}
20+
21+
/**
22+
* toString method for Moving Box Object
23+
*
24+
* @return String denoting the floor number and reference number assigned to
25+
* each Moving Box
26+
*/
27+
public String toString() {
28+
String output = "\tThe floor that Moving Box number " + getReferenceNumber() + " wants to go to is floor: "
29+
+ getFloor() + "\n";
30+
return output;
31+
}
32+
33+
/**
34+
* Get method for Reference Number
35+
*
36+
* @return int denoting the reference number for the moving box
37+
*/
38+
public int getReferenceNumber() {
39+
return referenceNumber;
40+
}
41+
42+
/**
43+
* Set method for Reference Number
44+
*
45+
* @param inReferenceNumber int denoting the reference number to set to a moving
46+
* box
47+
*/
48+
public void setReferenceNumber(int inReferenceNumber) {
49+
referenceNumber = inReferenceNumber;
50+
}
51+
52+
/**
53+
* Get method for Floor
54+
*
55+
* @return int denoting the floor the moving box will be delivered to
56+
*/
57+
public int getFloor() {
58+
return floor;
59+
}
60+
61+
/**
62+
* Set method for Floor
63+
*
64+
* @param inFloor int denoting the floor to deliver the moving box to
65+
*/
66+
public void setFloor(int inFloor) {
67+
floor = inFloor;
68+
}
69+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Stack and Queue Elevator Implementation
2+
3+
A Java implementation demonstrating the use of Stack and Queue data structures to simulate an elevator system for moving boxes between floors.
4+
5+
## Overview
6+
7+
The project simulates a building with 5 floors (numbered 0-4) and an elevator system that moves boxes between floors. Each box has a target destination floor, and the elevator's task is to efficiently deliver all boxes to their respective floors.
8+
9+
## Implementation Details
10+
11+
### Data Structures Used
12+
13+
- **Queue**: Represents each floor of the building, holding boxes waiting to be moved
14+
- **Stack**: Represents the elevator, following LIFO (Last In, First Out) principle
15+
- **Array**: Used to organize the queues for each floor
16+
17+
### Classes
18+
19+
1. **MovingBox.java**
20+
21+
- Represents a box to be moved
22+
- Properties:
23+
- referenceNumber: Unique identifier for each box
24+
- floor: Target floor for delivery
25+
26+
2. **Driver.java**
27+
- Contains the main simulation logic
28+
- Creates a building with 5 floors
29+
- Generates 100 boxes with random destination floors
30+
- Implements elevator logic using Stack and Queue operations
31+
32+
## Algorithm
33+
34+
The elevator system follows these rules:
35+
36+
1. Maximum capacity of 20 boxes
37+
2. If elevator is empty, it looks for a floor with waiting boxes
38+
3. When stopping at a floor:
39+
- Unloads all boxes
40+
- Checks each box:
41+
- If it's at its target floor, marks as delivered
42+
- If not, loads it back into the elevator
43+
4. Continues until all boxes are delivered to their target floors
44+
45+
## Usage
46+
47+
Compile and run the Driver class to start the simulation:
48+
49+
```bash
50+
javac data_structures/linear/stack_queue_elevator/Driver.java data_structures/linear/stack_queue_elevator/MovingBox.java
51+
java data_structures.linear.stack_queue_elevator.Driver
52+
```
53+
54+
The program will output:
55+
56+
- Initial building setup
57+
- Number of boxes on first floor
58+
- Target floors for each box
59+
- Delivery progress updates
60+
- Final completion message
61+
62+
## Time Complexity
63+
64+
- Box Creation: O(n) where n is the number of boxes
65+
- Delivery Process: O(n \* f) where:
66+
- n is the number of boxes
67+
- f is the number of floors

0 commit comments

Comments
 (0)