Advertisement
Guest User

Solution

a guest
May 2nd, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.21 KB | None | 0 0
  1. import scala.collection.mutable
  2. import scala.util.Try
  3.  
  4. object Solution extends App {
  5.  
  6.   /**
  7.     * Implement the following interface for an elevator
  8.     */
  9.   trait Elevator {
  10.  
  11.     /**
  12.       * Returns the number of floors that the elevator services
  13.       */
  14.     def numberOfFloors: Int
  15.  
  16.     /**
  17.       * Request the elevator to visit the given floor
  18.       */
  19.     def requestFloor(floor: Int): Unit
  20.  
  21.     /**
  22.       * Move the elevator to the next floor or remain if no movement is necessary.
  23.       * Returns the floor the elevator is located at after the movement, i.e. either 0 or 1 floors different from before.
  24.       * The elevator continues to travel in its current direction (up or down) until there are no more requests in that direction.
  25.       * Then it continues in the opposite direction, or stops if there are no more requested floors.
  26.       */
  27.     def stepAdvanceFloor(): Int
  28.  
  29.     /**
  30.       * Returns the floor the elevator is currently located at
  31.       */
  32.     def currentFloor: Int
  33.  
  34.   }
  35.  
  36.   val elevator: Elevator = new ElevatorImpl1(8)
  37.  
  38.   class ElevatorImpl1(reachableFloors: Int) extends Elevator {
  39.  
  40.     private var floor: Int = 0
  41.     private val requests: mutable.PriorityQueue[Int] = mutable.PriorityQueue[Int]()
  42.     private var request: Option[Int] = None
  43.  
  44.     /**
  45.       * Returns the number of floors that the elevator services
  46.       */
  47.     override def numberOfFloors: Int = reachableFloors
  48.  
  49.     /**
  50.       * Request the elevator to visit the given floor
  51.       */
  52.     override def requestFloor(floor: Int): Unit =
  53.       if (floor >= 0 && floor <= reachableFloors)
  54.         requests.enqueue(floor)
  55.  
  56.     /**
  57.       * Returns the floor the elevator is currently located at
  58.       */
  59.     override def currentFloor: Int = floor
  60.  
  61.     /**
  62.       * Move the elevator to the next floor or remain if no movement is necessary.
  63.       * Returns the floor the elevator is located at after the movement, i.e. either 0 or 1 floors different from before.
  64.       * The elevator continues to travel in its current direction (up or down) until there are no more requests in that direction.
  65.       * Then it continues in the opposite direction, or stops if there are no more requested floors.
  66.       */
  67.     override def stepAdvanceFloor(): Int = {
  68.       if (request.isEmpty)
  69.         request = Try(requests.dequeue()).toOption
  70.       request.foreach { r =>
  71.         if (r > floor)
  72.           floor += 1
  73.         else if (r < floor)
  74.           floor -= 1
  75.         if (r == floor)
  76.           request = None
  77.       }
  78.       currentFloor
  79.     }
  80.   }
  81.  
  82.  
  83.   // Ensure this example returns the correct results
  84.  
  85.   assert(elevator.numberOfFloors == 8, "numberOfFloors != 8")
  86.   assert(elevator.currentFloor == 0, "currentFloor != 0, stopped")
  87.   elevator.requestFloor(2)
  88.   assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving up")
  89.   elevator.requestFloor(6)
  90.   assert(elevator.stepAdvanceFloor() == 2, "currentFloor != 2, moving up")
  91.   assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving up")
  92.   assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving up")
  93.   elevator.requestFloor(3)
  94.   assert(elevator.stepAdvanceFloor() == 5, "currentFloor != 5, moving up")
  95.   assert(elevator.stepAdvanceFloor() == 6, "currentFloor != 6, moving up")
  96.   assert(elevator.stepAdvanceFloor() == 5, "currentFloor != 5, moving down")
  97.   assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving down")
  98.   assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving down")
  99.   assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, stopped")
  100.   elevator.requestFloor(4)
  101.   elevator.requestFloor(2)
  102.   assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving up again")
  103.   assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving down again")
  104.   assert(elevator.stepAdvanceFloor() == 2, "currentFloor != 3, moving down again")
  105.   // Current floor == 2
  106.  
  107.   elevator.requestFloor(1)
  108.   elevator.requestFloor(5)
  109.   elevator.requestFloor(0)
  110.   assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving down again?")
  111.   assert(elevator.stepAdvanceFloor() == 0, "currentFloor != 0, moving down again?")
  112.   assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving up again?")
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement