From 48bd1051bd793f830a0973fdc310f77a006121ff Mon Sep 17 00:00:00 2001 From: Alex Koryakov <31795607+DoomzD@users.noreply.github.com> Date: Sun, 10 Feb 2019 00:37:16 +0300 Subject: [PATCH] Update main.kt --- src/unitConverter/main.kt | 214 +++++++++++++++++++++++++++++++++++++- 1 file changed, 211 insertions(+), 3 deletions(-) diff --git a/src/unitConverter/main.kt b/src/unitConverter/main.kt index dfea104..e55da39 100644 --- a/src/unitConverter/main.kt +++ b/src/unitConverter/main.kt @@ -1,5 +1,213 @@ -package unitConverter +import java.util.* + +class Converter { + private val distancesNormalizer: Map = mapOf( + "m" to "meter", + "meters" to "meter", + "kilometers" to "kilometer", + "km" to "kilometer", + "cm" to "centimeter", + "centimeters" to "centimeter", + "mm" to "millimeter", + "millimeters" to "millimeter", + "mi" to "mile", + "miles" to "mile", + "yd" to "yard", + "yards" to "yard", + "ft" to "foot", + "feet" to "foot", + "in" to "inch", + "inches" to "inch" + ) + private val distancesInMeters: Map = mapOf( + "meter" to 1.0, + "kilometer" to 1000.0, + "centimeter" to 0.01, + "millimeter" to 0.001, + "mile" to 1609.35, + "yard" to 0.9144, + "foot" to 0.3048, + "inch" to 0.0254 + ) + private val pluralDistances: Map = mapOf( + "meter" to "meters", + "kilometer" to "kilometers", + "centimeter" to "centimeters", + "millimeter" to "millimeters", + "mile" to "miles", + "yard" to "yards", + "foot" to "feet", + "inch" to "inches" + ) + private val availableDistances: Set = setOf( + "meter", + "kilometer", + "centimeter", + "millimeter", + "mile", + "yard", + "foot", + "inch" + ) + + private val weightsNormalizer: Map = mapOf( + "g" to "gram", + "grams" to "gram", + "kilograms" to "kilogram", + "kg" to "kilogram", + "mg" to "milligram", + "milligrams" to "milligram", + "lb" to "pound", + "pounds" to "pound", + "oz" to "ounce", + "ounces" to "ounce" + ) + private val weightsInGrams: Map = mapOf( + "gram" to 1.0, + "kilogram" to 1000.0, + "milligram" to 0.001, + "pound" to 453.592, + "ounce" to 28.3495 + ) + private val pluralWeights: Map = mapOf( + "gram" to "grams", + "kilogram" to "kilograms", + "milligram" to "miligrams", + "pound" to "pounds", + "ounce" to "ounces" + ) + private val availableWeights: Set = setOf( + "gram", + "kilogram", + "milligram", + "pound", + "ounce" + ) + + private val degreesNormalizer: Map = mapOf( + "c" to "celsius", + "celsiuses" to "celsius", + "dc" to "celsius", + "f" to "fahrenheit", + "df" to "fahrenheit", + "fahrenheits" to "fahrenheit", + "kelvins" to "kelvin", + "k" to "kelvin", + "dk" to "kelvin" + ) + private val pluralDegrees: Map = mapOf( + "celsius" to "celsiuses", + "fahrenheit" to "fahrenheits", + "kelvin" to "kelvins" + ) + private val availableDegrees: Set = setOf( + "celsius", + "fahrenheit", + "kelvin" + ) + + + fun convertValues(from: String, to: String, value: Double) { + var fromMeasure = normalizeMeasure(from) + var toMeasure = normalizeMeasure(to) + + if (fromMeasure in availableDistances) { + if (toMeasure !in availableDistances) { + print("Conversion is impossible, try another one.\n") + return + } + + val newValue = convertDistances(fromMeasure, toMeasure, value) + if (value != 1.0) + fromMeasure = pluralDistances[fromMeasure]!! + if (newValue != 1.0) + toMeasure = pluralDistances[toMeasure]!! + printConversion(value, fromMeasure, newValue, toMeasure) + } else if (fromMeasure in availableWeights) { + if (toMeasure !in availableWeights) { + print("Conversion is impossible, try another one.\n") + return + } + + val newValue = convertWeights(fromMeasure, toMeasure, value) + if (value != 1.0) + fromMeasure = pluralWeights[fromMeasure]!! + if (newValue != 1.0) + toMeasure = pluralWeights[toMeasure]!! + printConversion(value, fromMeasure, newValue, toMeasure) + } else if (fromMeasure in availableDegrees) { + if (toMeasure !in availableDegrees) { + print("Conversion is impossible, try another one.\n") + return + } + + val newValue = convertDegrees(fromMeasure, toMeasure, value) + if (value != 1.0) + fromMeasure = pluralDegrees[fromMeasure]!! + if (newValue != 1.0) + toMeasure = pluralDegrees[toMeasure]!! + printConversion(value, fromMeasure, newValue, toMeasure) + } else { + print("Conversion is impossible, try another one.\n") + return + } + } + + private fun convertDistances(from: String, to: String, value: Double): Double { + val valueInMeters = distancesInMeters[from]!! * value + val newValueInMeters = distancesInMeters[to]!! + return valueInMeters / newValueInMeters + } + + private fun convertWeights(from: String, to: String, value: Double): Double { + val valueInGrams = weightsInGrams[from]!! * value + val newValueInGrams = weightsInGrams[to]!! + return valueInGrams / newValueInGrams + } + + private fun convertDegrees(from: String, to: String, value: Double): Double { + if (from == "celsius" && to == "fahrenheit") + return value * 1.8 + 32 + else if (to == "celsius" && from == "fahrenheit") + return (value - 32) * (5.0 / 9.0) + else if (from == "celsius" && to == "kelvin") + return value + 273.15 + else if (to == "celsius" && from == "kelvin") + return value - 273.15 + else if (from == "kelvin" && to == "fahrenheit") + return value * 1.8 - 459.67 + return (value + 459.67) * (5.0 / 9.0) + } + + private fun normalizeMeasure(measure: String): String { + var newMeasure = measure.toLowerCase() + if (newMeasure in distancesNormalizer) + newMeasure = distancesNormalizer[newMeasure]!! + else if (newMeasure in weightsNormalizer) + newMeasure = weightsNormalizer[newMeasure]!! + else if (newMeasure in degreesNormalizer) + newMeasure = degreesNormalizer[newMeasure]!! + return newMeasure + } + + private fun printConversion(value: Double, from: String, newValue: Double, to: String) { + println("$value $from is $newValue $to") + } +} fun main(args: Array) { - print("Hello world!") -} \ No newline at end of file + while (true) { + print("Enter what you want to convert (or 0 to Exit): ") + val scanner = Scanner(System.`in`) + + val value = scanner.nextDouble() + if (value == 0.0) + break + + val fromMeasure = scanner.next() + scanner.skip(" [a-zA-Z]+ ") + val toMeasure = scanner.next() + + Converter().convertValues(fromMeasure, toMeasure, value) + } +}