Sha256: e8ddc709ed32cd50c2cc916622994871464966712edf9a087c7b58f561bf2744

Contents?: true

Size: 905 Bytes

Versions: 218

Compression:

Stored size: 905 Bytes

Contents

data class Clock(private var hours: Int, private var minutes: Int) {

    companion object {
        private val MINUTES_IN_AN_HOUR = 60
        private val HOURS_IN_A_DAY = 24
    }

    init {
        sanitiseTime()
    }

    override fun toString(): String {
        return "${hours.toTimeString()}:${minutes.toTimeString()}"
    }

    fun add(minutes: Int) {
        this.minutes += minutes
        sanitiseTime()
    }

    private fun sanitiseTime() {
        while (minutes < 0) {
            minutes += MINUTES_IN_AN_HOUR
            hours--
        }

        while (hours < 0) {
            hours += HOURS_IN_A_DAY
        }

        val minutesOverflow = minutes / MINUTES_IN_AN_HOUR
        minutes %= MINUTES_IN_AN_HOUR
        hours = (hours + minutesOverflow) % HOURS_IN_A_DAY
    }

}

private fun Int.toTimeString(): String {
    return toString().padStart(length = 2, padChar = '0')
}

Version data entries

218 entries across 218 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.118 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.117 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.116 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.115 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.114 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.113 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.111 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.110 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.109 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.108 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.107 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.106 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.105 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.104 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.103 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.102 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.101 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.100 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt
trackler-2.2.1.99 tracks/kotlin/exercises/clock/.meta/src/reference/kotlin/Clock.kt