Advertisement
rafamsilva

UnitConverter

Sep 28th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.33 KB | None | 0 0
  1. package converter
  2.  
  3. fun main() {
  4.     loopcheck@ while (true) {
  5.         var conversion: Double
  6.         print("Enter what you want to convert (or exit): ")
  7.         val userInput = readln()
  8.  
  9.         if (userInput == "exit") break@loopcheck
  10.  
  11.         val (originalNumber, originalUnit, convertedUnit) = userInputSplit(userInput)
  12.  
  13.         // Validating inputs/Enums + exception checks
  14.         if (originalNumber == null) {
  15.             println("Wrong number")
  16.             continue@loopcheck
  17.         }
  18.         if (originalUnit == null && convertedUnit != null) {
  19.             println("Conversion from ??? to $convertedUnit is impossible")
  20.             continue@loopcheck
  21.         }
  22.         if (originalUnit != null && convertedUnit == null) {
  23.             println("Conversion from $originalUnit to ??? is impossible")
  24.             continue@loopcheck
  25.         }
  26.         if (originalUnit == null && convertedUnit == null) {
  27.             println("Conversion from ??? to ??? is impossible")
  28.             continue@loopcheck
  29.         }
  30.         if (originalUnit != null && convertedUnit != null) {
  31.             if (originalUnit.type != convertedUnit.type) {
  32.                 println("Conversion from ${originalUnit.plural} to ${convertedUnit.plural} is impossible")
  33.                 continue@loopcheck
  34.             } else {
  35.                 // Conversion Calculus
  36.                 conversion = originalNumber * originalUnit.ratio / convertedUnit.ratio
  37.                 // Check if the units are singular or plural
  38.                 val preUnitCheck = if (originalNumber == 1.0) originalUnit.singular else originalUnit.plural
  39.                 val postUnitCheck = if (conversion == 1.0) convertedUnit.singular else convertedUnit.plural
  40.                 // Prints final text
  41.                 println("$originalNumber $preUnitCheck is $conversion $postUnitCheck")
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. // Function to organize userInput in 3 variables: Number Unit1 Unit2
  48. fun userInputSplit(userInput: String): Triple<Double?, Unidade?, Unidade?> {
  49.     val userInputArray = userInput.lowercase().split(" ")
  50.  
  51.     val originalNumber = try {
  52.         userInputArray[0].toDouble()
  53.     } catch (e: NumberFormatException) { null }
  54.  
  55.     val originalUnit = try {
  56.         getUnit(userInputArray[1])
  57.     } catch (e: IllegalArgumentException) { null }
  58.  
  59.     val convertedUnit = try {
  60.         getUnit(userInputArray[2])
  61.     } catch (e: IllegalArgumentException) { null }
  62.  
  63.     return Triple(originalNumber, originalUnit, convertedUnit)
  64. }
  65.  
  66. // Function to set constant from Unidade for conversion based from User Input.
  67. fun getUnit(unit: String): Unidade =
  68.     when (unit) {
  69.         "m", "meter", "meters" -> Unidade.METER
  70.         "km", "kilometer", "kilometers" -> Unidade.KILOMETER
  71.         "cm", "centimeter", "centimeters" -> Unidade.CENTIMETER
  72.         "mm", "millimeter", "millimeters" -> Unidade.MILLIMETER
  73.         "mi", "mile", "miles" -> Unidade.MILE
  74.         "yd", "yard", "yards" -> Unidade.YARD
  75.         "ft", "foot", "feet" -> Unidade.FOOT
  76.         "in", "inch", "inches" -> Unidade.INCH
  77.         "g", "gram", "grams" -> Unidade.GRAM
  78.         "kg", "kilogram", "kilograms" -> Unidade.KILOGRAM
  79.         "mg", "milligram", "milligrams" -> Unidade.MILLIGRAM
  80.         "lb", "pound", "pounds" -> Unidade.POUND
  81.         "oz", "ounce", "ounces" -> Unidade.OUNCE
  82.         else -> throw IllegalArgumentException ("Wrong Unit. Try Again.")
  83.     }
  84. enum class Unidade (val short: String,
  85.                     val singular: String,
  86.                     val plural: String,
  87.                     val ratio: Double,
  88.                     val type: String
  89.                     ) {
  90.     METER("m","meter", "meters", 1.0, "Length"),
  91.     KILOMETER("km","kilometer", "kilometers", 1000.0, "Length"),
  92.     CENTIMETER("cm","centimeter", "centimeters", 0.01, "Length"),
  93.     MILLIMETER("mm", "millimeter", "millimeters", 0.001, "Length"),
  94.     MILE("mi","mile", "miles", 1609.35, "Length"),
  95.     YARD("yd","yard", "yards", 0.9144, "Length"),
  96.     FOOT("ft","foot", "feet", 0.3048, "Length"),
  97.     INCH("in","inch", "inches", 0.0254, "Length"),
  98.     GRAM("g", "gram", "grams", 1.0, "Weight"),
  99.     KILOGRAM("kg", "kilogram", "kilograms", 1000.0, "Weight"),
  100.     MILLIGRAM("mg", "milligram", "milligrams", 0.001, "Weight"),
  101.     POUND("lb", "pound", "pounds", 453.592, "Weight"),
  102.     OUNCE("oz","ounce", "ounces", 28.3495, "Weight");
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement