Design a Elevator

我们来设计一个电梯。

随便写了一下,电梯大概就是有下面简单几个功能:

  1. 用户make request。

  2. 电梯移动到下一个楼层。

  3. 显示电梯当前的楼层。

class Enum():
    class Direction():
        up = 'up'
        down = 'down'

class elevator():

    def __init__(self, height):
        self.height = height
        self.curFloor = 1
        self.destinations = [False]*height
        self.direction = Enum.Direction.up
        self.requsts = [[] for _ in range(len(height))]


    def addRequest(self, rquestFloor, desFloor):
        if desFloor == rquestFloor:
            return
        self.requsts[rquestFloor].append(desFloor)


    def getCurrentFloor(self):
        return self.floorNum


    def goNextFloor(self):
        if not any(self.destinations) or not any(self.requsts):
            return

        self.moveToNextFloor()

        for des in self.requst[curFloor]:
            self.destinations[des] = True
        self.requst[curFloor] = []
        self.destinations[self.curFloor-1] = False


    def moveToNextFloor(self):
        if any(self.destinations):
            curDestinations = self.destinations
        else:
            curDestinations = self.requst

        if self.direction == Enum.Direction.up: # go up
            if self.curFloor == self.height:
                self.curFloor -= 1
                self.direction = Enum.Direction.down
            else:
                if any(curDestinations[curFloor:]):
                    self.curFloor += 1
                else:
                    self.curFloor -= 1
                    self.direction = Enum.Direction.down
        else:
            if self.curFloor == 0:
                self.curFloor += 1
                self.direction = Enum.Direction.up
            else:
                if any(curDestinations[:curFloor-1]):
                    self.curFloor -= 1
                else:
                    self.curFloor += 1
                    self.direction = Enum.Direction.up

results matching ""

    No results matching ""