Design an elevator system


  • 6
    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.


Log in to reply
 

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