Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sealed class Measure(
- val short: String,
- val normal: String,
- val plural: String,
- val multiplier: Double,
- val shift: Double = 0.0
- )
- class Dist(
- short: String,
- normal: String,
- plural: String,
- multiplier: Double
- ) : Measure(short, normal, plural, multiplier)
- class Weight(
- short: String,
- normal: String,
- plural: String,
- multiplier: Double
- ) : Measure(short, normal, plural, multiplier)
- class Tempr(
- short: String,
- normal: String,
- plural: String,
- multiplier: Double,
- shift: Double
- ) : Measure(short, normal, plural, multiplier, shift)
- val allMeasures = listOf(
- Dist("a", "a", "a", 1.0),
- Weight("b", "b", "b", 2.0),
- Tempr("degree", "c", "", 1.0, 0.0),
- Tempr("fahrenheit", "f", "", 1.8, 32.0),
- Tempr("kelvin", "k", "", 1.0, 273.5)
- )
- val nameToMeasure = allMeasures
- .flatMap { m ->
- listOf(m.short, m.normal, m.plural).map { name -> name to m }
- }.toMap()
- fun main(args: Array<String>) {
- // 23 f to k
- val m1 = nameToMeasure.getValue("f")
- val m2 = nameToMeasure.getValue("k")
- val input = 23
- if (m1::class != m2::class) {
- throw IllegalStateException("incomparable types: ${m1::class.simpleName} and ${m2::class.simpleName}")
- }
- val inDefault = (input - m1.shift) / m1.multiplier
- val output = inDefault * m2.multiplier + m2.shift
- println(output)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement