Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.94 KB | None | 0 0
  1. import org.scalatest.{MustMatchers, WordSpec}
  2.  
  3. class BabbySpec extends WordSpec with MustMatchers {
  4.  
  5.   "baby" when {
  6.     "sat for the normalest awake hour" should {
  7.       "be paid for only awake time" in {
  8.         calculateEarnings(9, 10, 11) mustBe 10
  9.       }
  10.     }
  11.  
  12.     "when crossing all time" should {
  13.       "be calculates correctly" in {
  14.         calculateEarnings(9, 1, 11) mustBe 34
  15.       }
  16.     }
  17.  
  18.     "arriving after bedtime" should {
  19.       "give only 6s" in {
  20.         calculateEarnings(10, 12, 9) must be (12)
  21.       }
  22.     }
  23.  
  24.     "arriving at midnight" should {
  25.       "only get paid in 8s" in {
  26.         calculateEarnings(12, 3, 11) must be (24)
  27.       }
  28.     }
  29.   }
  30.  
  31.   "time" can {
  32.     "be compared" should {
  33.       "evening times behave as expected" in {
  34.         assert(Time(6) > Time(5))
  35.         assert(Time(5) < Time(6))
  36.       }
  37.       "after midnight times behave as expected" in {
  38.         assert(Time(4) > Time(3))
  39.         assert(Time(3) < Time(4))
  40.       }
  41.       "today and tomorrow get weird" in {
  42.         assert(Time(4) > Time(5))
  43.         assert(Time(5) < Time(4))
  44.         assert(Time(1) > Time(12))
  45.         assert(Time(12) < Time(1))
  46.       }
  47.       "midnight is between 11 and 1" in {
  48.         assert(Time(11) < Midnight)
  49.         assert(Midnight < Time(1))
  50.       }
  51.     }
  52.  
  53.     "be subtracted" should {
  54.       "safe evening times" in {
  55.         Time(6) - Time(5) must be(1)
  56.       }
  57.       "safe after-midnight times" in {
  58.         Time(4) - Time(3) must be(1)
  59.       }
  60.       "wrap at midnight" in {
  61.         assert(Time(1) - Midnight == 1)
  62.       }
  63.       "full night" in {
  64.         Time(4) - Time(5) must be(11)
  65.       }
  66.     }
  67.   }
  68.  
  69.   def calculateEarnings(arrival: Int, leave: Int, bed: Int): Int = {
  70.     calculateEarnings(Time(arrival), Time(leave), Time(bed))
  71.   }
  72.  
  73.   def calculateEarnings(arrivalTime: Time, leaveTime: Time, bedTime: Time): Int = {
  74.     val awakeTime: Int = if (arrivalTime < OddHour.min(Midnight, bedTime)) OddHour.min(bedTime, leaveTime) - arrivalTime else 0
  75.     val lateTime = Math.max(leaveTime - Midnight, 0)
  76.     val asleepTime = Math.max(OddHour.min(leaveTime, Midnight) - OddHour.max(bedTime, arrivalTime), 0)
  77.  
  78.     awakeTime * 10 + asleepTime * 6 + lateTime * 8
  79.   }
  80. }
  81.  
  82. object OddHour {
  83.   def min(a: OddHour, b: OddHour): OddHour = {
  84.     if (a < b) a else b
  85.   }
  86.   def max(a: OddHour, b: OddHour): OddHour = {
  87.     if (a > b) a else b
  88.   }
  89. }
  90.  
  91. sealed trait OddHour extends Ordered[OddHour] {
  92.   def hour: Int
  93.  
  94.   def compare(that: OddHour): Int = {
  95.     if (this.hour < 5 && that.hour >= 5) {
  96.       1
  97.     } else if (this.hour >= 5 && that.hour < 5) {
  98.       -1
  99.     } else {
  100.       this.hour.compare(that.hour)
  101.     }
  102.   }
  103.  
  104.   def -(that: OddHour): Int = {
  105.     biggerize(this.hour) - biggerize(that.hour)
  106.   }
  107.  
  108.   private def biggerize(i: Int): Int = {
  109.     if (i < 5) i + 12 else i
  110.   }
  111. }
  112.  
  113. case class Time(hour: Int) extends OddHour
  114.  
  115. case object Midnight extends OddHour {
  116.   val hour = 12
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement