Design an elevator system


  • 7
    R

    How do you notify the elevator that it needs to move up/down? You could also have a centralized class to control this. Define all the entities and behavior and clearly state your assumptions.


  • 1
    W
    public interface IElevator()
    {
         public boolean GoUP();
         public boolean GoDown();
         public int GetLevel();
         public void SetLevel(int level);
         public boolean CanAddLevel(int level);
    
    }
    
    public enum Directions {up, down};
    
    public enum Levels {1,2,3,4};
    
    // this is a normal elevator. We can also implement maintenance elevators or other kinds in similar way 
    public class Elevator extends IElevator()
    {
         // Implement Go up
         // Implement Go Down
    
        Queue<Integer> elevQ = new LinkedList<Integer>();
        
    // Method to set level
         public void SetLevel(int level)
         {
                   elevQ .add(level);
         }
    
    // method to get level
         public int GetLevel(int level)
         {
                   elevQ .peek();
         }
    
    // Method to check if a level can be added
    // we can either do this check on controller or give it to the elevators
          public boolean CanAddLevel(int level)
          {
                  if(Direction.up && elevQ .peek() < level)
                         return true;
                  else if(Direction.down && elevQ .peek() > level)
                         return true;
            return false
          } 
    }
    
    public class ElevatorController()
    {
    // contains logic to decide which elevator can be called from a particular floor
    // Contains a list of elevator
    }

  • 0
    G

    Guys, this is my thought process. Please give your feedback. :)

     
    class ELEVATOR_CONTROLLER {
      List<ELEVATOR>
      LIST<FLOORS>
      processRequest();
    }
    
    interface BUTTON{
       placeRequest();
    }
    class FloorButton implements BUTTON {
      direction
      placeRequest();
    }
    class ElevatorButton implements BUTTON {
       direction
       placeRequest();
    }
    
    class ELEVATOR {
      STATES: [IDLE, MAINTENANCE, LOADING, UNLOADING, MOVING ]
      direction: UP, Down
      current_floor:
      min_floor:
      max_floor:
      prohibited_floors: [...]
      currentRequest: 
      NextRequest:
    }
    
    class REQUEST {
     direction:
     floor:
    }
    
    class ELEVATOR_REQUEST {
      LIST<REQUEST>
      addRequest();
      removeRequest();
    }
    

    Wanted to upload the ER diagram image that I worked out on a notepad but Leetcode is giving some error.


  • 0
    B

    I have developed sample code in Kotlin under following constraints -
    i) I am running single lift
    ii) I am not handling maximum weight lift can bear

    Using this class i am starting lift using main method-

    import kotlin.concurrent.thread
    
    fun main(args: Array<String>) {
    
        val liftModel = LiftModel(0, Floor.GROUND, Direction.NONE)
        val liftController = LiftController(liftModel)
        thread(true, false, null, "Thread1", -1, { startLiftFirstThread(liftController) })
        thread(true, false, null, "Thread2", -1, { startLiftSecondThread(liftController) })
        liftController.addFloor(Floor.FIRST)
    }
    
    fun startLiftFirstThread(liftController: LiftController) {
        Thread.sleep(500)
        liftController.addFloor(Floor.GROUND)
        liftController.addFloor(Floor.SECOND)
        liftController.addFloor(Floor.FOURTH)
    }
    
    fun startLiftSecondThread(liftController: LiftController) {
        Thread.sleep(2000)
        liftController.addFloor(Floor.FIRST)
        Thread.sleep(5000)
        liftController.addFloor(Floor.THIRD)
    }
    

    Lift Controller class -

    enum class Floor {GROUND, FIRST, SECOND, THIRD, FOURTH, FIFTH }
    enum class Direction {UP, DOWN, NONE }
    
    class LiftController(liftModel: LiftModel) {
        private val mUpLiftFloorsList: MutableList<Floor> = mutableListOf<Floor>()
        private val mDownLiftFloorsList: MutableList<Floor> = mutableListOf<Floor>()
        private var mLiftModel: LiftModel = liftModel
    
        fun startLift() {
            mLiftModel.currentDirection = getLiftDirection()
    
            if (mLiftModel.currentDirection == Direction.UP) {
                moveLiftUp()
            } else if (mLiftModel.currentDirection == Direction.DOWN) {
                moveLiftDown()
            }
        }
    
        fun addFloor(newFloor: Floor) {
            //println("Adding Floor:$newFloor Current Ordinal ${mLiftModel.currentFloor.ordinal}, new Ordinal ${newFloor.ordinal}")
    
            if (mLiftModel.currentFloor.ordinal > newFloor.ordinal) {
                mDownLiftFloorsList.add(newFloor)
                mDownLiftFloorsList.sort()
                mDownLiftFloorsList.reverse()
                //println("Adding to Down List size=${mDownLiftFloorsList.size} and Floor $newFloor")
            } else if (mLiftModel.currentFloor.ordinal < newFloor.ordinal) {
                mUpLiftFloorsList.add(newFloor)
                mUpLiftFloorsList.sort()
                //println("Adding to Up List size=${mUpLiftFloorsList.size} and Floor $newFloor")
            }
    
            if (mLiftModel.currentDirection == Direction.NONE) {
                startLift()
            }
        }
    
        private fun removeVisitingFloor() {
            when (mLiftModel.currentDirection) {
                Direction.UP -> mUpLiftFloorsList.remove(mLiftModel.currentFloor)
                Direction.DOWN -> mDownLiftFloorsList.remove(mLiftModel.currentFloor)
                else -> {
                    //print("Do nothing")
                }
            }
        }
    
        private fun moveLiftUp() {
            do {
                val currentFloor: Floor = mUpLiftFloorsList.get(0)
                mLiftModel.currentFloor = currentFloor
                removeVisitingFloor()
                println("Lift Going Up to Floor: $currentFloor")
                Thread.sleep(1000); // The time in which lift moves up
            } while (mUpLiftFloorsList.size > 0)
    
            if (mDownLiftFloorsList.size > 0) {
                mLiftModel.currentDirection = Direction.DOWN
                moveLiftDown()
            } else {
                mLiftModel.currentDirection = Direction.NONE
            }
        }
    
        private fun moveLiftDown() {
            do {
                val currentFloor = mDownLiftFloorsList.get(0)
                mLiftModel.currentFloor = currentFloor
                removeVisitingFloor()
                println("Lift Going Down to Floor: $currentFloor")
                Thread.sleep(1000) // The time in which lift moves up
            } while (mDownLiftFloorsList.size > 0)
    
            if (mUpLiftFloorsList.size > 0) {
                mLiftModel.currentDirection = Direction.UP
                moveLiftUp()
            } else {
                mLiftModel.currentDirection = Direction.NONE
            }
        }
    
        private fun getLiftDirection(): Direction {
            if (mUpLiftFloorsList.size > 0) return Direction.UP
            else if (mDownLiftFloorsList.size > 0) return Direction.DOWN
            else return Direction.NONE
        }
    }
    

    Lift Model class to store data -

    data class LiftModel(var currentWeight: Int, var currentFloor: Floor, var currentDirection: Direction) {
        val TOP = Floor.FIFTH
        val GROUND = Floor.GROUND
    }
    

    Here is the output of my program -

    Lift Going Up to Floor: FIRST
    Lift Going Up to Floor: SECOND
    Lift Going Up to Floor: FOURTH
    Lift Going Down to Floor: FIRST
    Lift Going Down to Floor: GROUND
    Lift Going Up to Floor: THIRD
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.