Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable
- import scala.util.Try
- object Solution extends App {
- /**
- * Implement the following interface for an elevator
- */
- trait Elevator {
- /**
- * Returns the number of floors that the elevator services
- */
- def numberOfFloors: Int
- /**
- * Request the elevator to visit the given floor
- */
- def requestFloor(floor: Int): Unit
- /**
- * Move the elevator to the next floor or remain if no movement is necessary.
- * Returns the floor the elevator is located at after the movement, i.e. either 0 or 1 floors different from before.
- * The elevator continues to travel in its current direction (up or down) until there are no more requests in that direction.
- * Then it continues in the opposite direction, or stops if there are no more requested floors.
- */
- def stepAdvanceFloor(): Int
- /**
- * Returns the floor the elevator is currently located at
- */
- def currentFloor: Int
- }
- val elevator: Elevator = new ElevatorImpl1(8)
- class ElevatorImpl1(reachableFloors: Int) extends Elevator {
- private var floor: Int = 0
- private val requests: mutable.PriorityQueue[Int] = mutable.PriorityQueue[Int]()
- private var request: Option[Int] = None
- /**
- * Returns the number of floors that the elevator services
- */
- override def numberOfFloors: Int = reachableFloors
- /**
- * Request the elevator to visit the given floor
- */
- override def requestFloor(floor: Int): Unit =
- if (floor >= 0 && floor <= reachableFloors)
- requests.enqueue(floor)
- /**
- * Returns the floor the elevator is currently located at
- */
- override def currentFloor: Int = floor
- /**
- * Move the elevator to the next floor or remain if no movement is necessary.
- * Returns the floor the elevator is located at after the movement, i.e. either 0 or 1 floors different from before.
- * The elevator continues to travel in its current direction (up or down) until there are no more requests in that direction.
- * Then it continues in the opposite direction, or stops if there are no more requested floors.
- */
- override def stepAdvanceFloor(): Int = {
- if (request.isEmpty)
- request = Try(requests.dequeue()).toOption
- request.foreach { r =>
- if (r > floor)
- floor += 1
- else if (r < floor)
- floor -= 1
- if (r == floor)
- request = None
- }
- currentFloor
- }
- }
- // Ensure this example returns the correct results
- assert(elevator.numberOfFloors == 8, "numberOfFloors != 8")
- assert(elevator.currentFloor == 0, "currentFloor != 0, stopped")
- elevator.requestFloor(2)
- assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving up")
- elevator.requestFloor(6)
- assert(elevator.stepAdvanceFloor() == 2, "currentFloor != 2, moving up")
- assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving up")
- assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving up")
- elevator.requestFloor(3)
- assert(elevator.stepAdvanceFloor() == 5, "currentFloor != 5, moving up")
- assert(elevator.stepAdvanceFloor() == 6, "currentFloor != 6, moving up")
- assert(elevator.stepAdvanceFloor() == 5, "currentFloor != 5, moving down")
- assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving down")
- assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving down")
- assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, stopped")
- elevator.requestFloor(4)
- elevator.requestFloor(2)
- assert(elevator.stepAdvanceFloor() == 4, "currentFloor != 4, moving up again")
- assert(elevator.stepAdvanceFloor() == 3, "currentFloor != 3, moving down again")
- assert(elevator.stepAdvanceFloor() == 2, "currentFloor != 3, moving down again")
- // Current floor == 2
- elevator.requestFloor(1)
- elevator.requestFloor(5)
- elevator.requestFloor(0)
- assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving down again?")
- assert(elevator.stepAdvanceFloor() == 0, "currentFloor != 0, moving down again?")
- assert(elevator.stepAdvanceFloor() == 1, "currentFloor != 1, moving up again?")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement