Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.86 KB | None | 0 0
  1. package AirportScheduler
  2.  
  3. import collection.mutable
  4. import java.util.Date
  5. import scala.concurrent.duration.Duration
  6.  
  7. trait AirportScheduler {
  8.   def reserveTime(d: Date): Boolean
  9.   def freeSlot(d: Date): Boolean
  10.   def isValid(d: Date): Boolean
  11. }
  12.  
  13. class AirportScheduler extends AirportScheduler {
  14.  
  15.   private[this] val bookedTimes = mutable.TreeSet.empty[Date]
  16.   // Hours worth of milliseconds
  17.   private[this] val Duration = (60 * 60 * 1000)
  18.  
  19.   def reserveTime(d: Date): Boolean = {
  20.     if (isValid(d)) bookedTimes.add(d)
  21.     else false
  22.   }
  23.  
  24.   def isValid(d: Date): Boolean = {
  25.     val dt = d.getTime()
  26.  
  27.     bookedTimes.filter { r =>
  28.       val rt = r.getTime()
  29.       // our reserved time doesn't fall into any of the ranges
  30.       !(dt > rt && dt < rt + Duration)
  31.     }.isEmpty
  32.   }
  33.  
  34.   def freeSlot(d: Date): Boolean = {
  35.     bookedTimes.remove(d)
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement