Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.scalatest.{MustMatchers, WordSpec}
- class BabbySpec extends WordSpec with MustMatchers {
- "baby" when {
- "sat for the normalest awake hour" should {
- "be paid for only awake time" in {
- calculateEarnings(9, 10, 11) mustBe 10
- }
- }
- "when crossing all time" should {
- "be calculates correctly" in {
- calculateEarnings(9, 1, 11) mustBe 34
- }
- }
- "arriving after bedtime" should {
- "give only 6s" in {
- calculateEarnings(10, 12, 9) must be (12)
- }
- }
- "arriving at midnight" should {
- "only get paid in 8s" in {
- calculateEarnings(12, 3, 11) must be (24)
- }
- }
- }
- "time" can {
- "be compared" should {
- "evening times behave as expected" in {
- assert(Time(6) > Time(5))
- assert(Time(5) < Time(6))
- }
- "after midnight times behave as expected" in {
- assert(Time(4) > Time(3))
- assert(Time(3) < Time(4))
- }
- "today and tomorrow get weird" in {
- assert(Time(4) > Time(5))
- assert(Time(5) < Time(4))
- assert(Time(1) > Time(12))
- assert(Time(12) < Time(1))
- }
- "midnight is between 11 and 1" in {
- assert(Time(11) < Midnight)
- assert(Midnight < Time(1))
- }
- }
- "be subtracted" should {
- "safe evening times" in {
- Time(6) - Time(5) must be(1)
- }
- "safe after-midnight times" in {
- Time(4) - Time(3) must be(1)
- }
- "wrap at midnight" in {
- assert(Time(1) - Midnight == 1)
- }
- "full night" in {
- Time(4) - Time(5) must be(11)
- }
- }
- }
- def calculateEarnings(arrival: Int, leave: Int, bed: Int): Int = {
- calculateEarnings(Time(arrival), Time(leave), Time(bed))
- }
- def calculateEarnings(arrivalTime: Time, leaveTime: Time, bedTime: Time): Int = {
- val awakeTime: Int = if (arrivalTime < OddHour.min(Midnight, bedTime)) OddHour.min(bedTime, leaveTime) - arrivalTime else 0
- val lateTime = Math.max(leaveTime - Midnight, 0)
- val asleepTime = Math.max(OddHour.min(leaveTime, Midnight) - OddHour.max(bedTime, arrivalTime), 0)
- awakeTime * 10 + asleepTime * 6 + lateTime * 8
- }
- }
- object OddHour {
- def min(a: OddHour, b: OddHour): OddHour = {
- if (a < b) a else b
- }
- def max(a: OddHour, b: OddHour): OddHour = {
- if (a > b) a else b
- }
- }
- sealed trait OddHour extends Ordered[OddHour] {
- def hour: Int
- def compare(that: OddHour): Int = {
- if (this.hour < 5 && that.hour >= 5) {
- 1
- } else if (this.hour >= 5 && that.hour < 5) {
- -1
- } else {
- this.hour.compare(that.hour)
- }
- }
- def -(that: OddHour): Int = {
- biggerize(this.hour) - biggerize(that.hour)
- }
- private def biggerize(i: Int): Int = {
- if (i < 5) i + 12 else i
- }
- }
- case class Time(hour: Int) extends OddHour
- case object Midnight extends OddHour {
- val hour = 12
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement