Sha256: 9d7d64e1bddd184f0c72e2cbe6f287e0421b1a2bf08565d6751647849f944954

Contents?: true

Size: 1.05 KB

Versions: 396

Compression:

Stored size: 1.05 KB

Contents

import java.math.BigDecimal

class SpaceAge(val seconds: Long) {

    companion object {
        val EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0
        val PRECISION = 2

        private enum class Planet(val relativeOrbitalPeriod: Double) {
            EARTH(1.0), MERCURY(0.2408467), VENUS(0.61519726), MARS(1.8808158),
            JUPITER(11.862615), SATURN(29.447498), URANUS(84.016846), NEPTUNE(164.79132)
        }
    }

    fun onEarth() = calculateAge(Planet.EARTH)
    fun onMercury() = calculateAge(Planet.MERCURY)
    fun onVenus() = calculateAge(Planet.VENUS)
    fun onMars() = calculateAge(Planet.MARS)
    fun onJupiter() = calculateAge(Planet.JUPITER)
    fun onSaturn() = calculateAge(Planet.SATURN)
    fun onUranus() = calculateAge(Planet.URANUS)
    fun onNeptune() = calculateAge(Planet.NEPTUNE)

    private fun calculateAge(planet: Planet): Double {
        val age: Double = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.relativeOrbitalPeriod)

        return BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).toDouble()
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.179 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.178 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.177 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.176 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.175 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.174 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.173 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.172 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.171 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.170 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.169 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.167 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.166 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.165 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.164 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.163 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.162 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.161 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt
trackler-2.2.1.160 tracks/kotlin/exercises/space-age/.meta/src/reference/kotlin/SpaceAge.kt