-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.DesignAnElevatorSystem.py
More file actions
68 lines (55 loc) · 1.95 KB
/
11.DesignAnElevatorSystem.py
File metadata and controls
68 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Design and implement a simple Elevator System simulation in Python that handles
# multiple floor requests, moves in real-time using the SCAN algorithm, and
# prints the elevator’s current floor and direction —
# all within a single-file console demo (no database or GUI).
import time
import threading
class Elevator:
def __init__(self, floors = 10):
self.current_floor = 0
self.direction = "UP"
self.requests = []
self.total_floors = floors
self.running = True
def add_request(self, floor: int):
if 0 <= floor < self.total_floors:
if floor not in self.requests:
self.requests.append(floor)
self.requests.sort()
print(f"Request added for floor {floor}")
else:
print("Invalid floor request.")
def move(self):
while self.running:
if not self.requests:
time.sleep(1)
continue
target_floor = self.requests[0]
if self.current_floor < target_floor:
self.direction = "UP"
self.current_floor += 1
elif self.current_floor > target_floor:
self.direction = "DOWN"
self.current_floor -= 1
else:
print(f"Reached floor {self.current_floor}. Doors opening...")
self.requests.pop(0)
time.sleep(1)
print("Doors closing...")
continue
print(f"Moving {self.direction} | Current Floor: {self.current_floor}")
time.sleep(0.7)
def run(self):
t = threading.Thread(target = self.move)
t.daemon = True
t.start()
if __name__ == "__main__":
e = Elevator(10)
e.run()
e.add_request(3)
e.add_request(6)
e.add_request(2)
# Let simulation run for 20 seconds
time.sleep(20)
e.running = False
print("Simulation ended.")