class Door:
def __init__(self, floor):
self.floor = floor
def open(self):
print(f"{self.floor} door opened")
self.status = "door opened"
def close(self):
print(f"{self.floor} door closed")
self.status = "door close"
class Button:
def __init__(self, floor, name):
self.floor = floor
self.name = name
self.on = None
self.systemObservers = []
def request(self):
if not self.on:
self.systemNotify()
self.on = True
print(f"{self.floor} Button activated")
def turnon(self):
self.on = True
print(f"{self.floor} Button turned on")
def turnoff(self):
self.on = False
print(f"{self.floor} Button turned off")
def systemRegister(self, observer):
self.systemObservers.append(observer)
def systemNotify(self):
for observer in self.systemObservers:
if self.name == "elevator":
observer.elevatorButtonNotify(self.floor)
elif self.name == "up":
observer.floorButtonNotify(self.floor, True)
elif self.name == "down":
observer.floorButtonNotify(self.floor, False)
class Floor:
def __init__(self, floor):
self.floor = floor
self.door = Door(floor)
self.upButton = Button(floor, "up")
self.downButton = Button(floor, "down")
self.systemObserver = []
def opendoor(self, isUp):
self.door.open()
self.updateButton(isUp)
def closedoor(self):
self.door.close()
def systemRegister(self, observer):
self.systemObserver.append(observer)
self.upButton.systemRegister(observer)
self.downButton.systemRegister(observer)
def updateButton(self, isUp):
if isUp:
self.upButton.turnoff()
else:
self.downButton.turnoff()
class Elevator:
def __init__(self, story):
self.story = story
self.buttons = [Button(i, "elevator") for i in range(story)]
self.door = Door(-1)
self.currentfloor = 0
self.systemObserver = None
def opendoor(self, floor):
self.door.open()
self.systemObserver.floors[floor].opendoor(self.systemObserver.directionUp)
def closedoor(self, floor):
self.door.close()
self.systemObserver.floors[floor].closedoor()
def moveup(self):
if self.currentfloor < self.story:
self.currentfloor += 1
self.systemObserver.elevatorNotify(self.currentfloor)
def movedown(self):
if self.currentfloor > 1:
self.currentfloor -= 1
self.systemObserver.elevatorNotify(self.currentfloor)
def SystemRegister(self, observer):
self.systemObserver = observer
for button in self.buttons:
button.systemRegister(observer)
import heapq, time
class ElevatorSystem:
def __init__(self, story):
self.story = story
self.floors = [Floor(i) for i in range(story)]
for floor in self.floors:
floor.systemRegister(self)
self.elevator = Elevator(story)
self.elevator.SystemRegister(self)
self.maxHeap = []
self.minHeap = []
self.directionUp = True
self.currentHeap = self.minHeap
self.moveElevator = self.elevator.moveup
self.currentFloor = 0
self.operation = True
def operate(self):
while self.operation:
while self.currentHeap:
floor = self.currentHeap.pop()
while self.currentFloor != floor:
self.moveElevator()
s.elevator.opendoor(floor)
s.elevator.closedoor(floor)
self.directionChange()
def directionChange(self):
time.sleep(1)
self.directionUp = not self.directionUp
if self.directionUp:
self.currentHeap = self.minHeap
self.moveElevator = self.elevator.moveup
else:
self.currentHeap = self.maxHeap
self.moveElevator = self.elevator.movedown
def stop(self):
self.minHeap.clear()
self.maxHeap.clear()
self.operation = False
def elevatorButtonNotify(self, floor):
if self.currentFloor < floor:
heapq.heappush(self.minHeap, floor)
else:
heapq.heappush(self.maxHeap, floor)
def floorButtonNotify(self, floor, isUp):
if self.currentFloor < floor:
heapq.heappush(self.minHeap, floor)
else:
heapq.heappush(self.maxHeap, floor)
def elevatorNotify(self, floor):
self.currentFloor = floor
def __str__(self):
s = f"{self.currentFloor}, is Elevator Going up?: {self.directionUp}, current Heap: {self.currentHeap} "
return s
import threading
def simpleTest():
s = ElevatorSystem(5)
threading.Thread(target=s.operate).start()
time.sleep(1)
s.floors[2].upButton.request()
time.sleep(1)
print(s)
s.elevator.buttons[4].request()
print(s)
s.elevator.buttons[0].request()
print(s)
s.floors[4].upButton.request()
s.floors[3].upButton.request()
print(s)
s.stop()