Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.94 KB | None | 0 0
  1. package converter
  2.  
  3. import java.util.*
  4.  
  5. enum class Measure(val category: String, val shortNameOfMetric: String, val fullNameOfMetric: String, val pluralNameOfMetric: String, val coefficientNum: Double){
  6.     METER("length", "m", "meter", "meters", 1.0),
  7.     KILOMETER("length", "km","kilometer","kilometers", 1000.0),
  8.     CENTIMETER("length", "cm","centimeter","centimeters", 0.01),
  9.     MILLIMETER("length", "mm","millimeter","millimeters", 0.001),
  10.     MILE("length", "mi","mile","miles", 1609.35),
  11.     YARD("length", "yd","yard","yards", 0.9144),
  12.     FOOT("length", "ft","foot","feet", 0.3048),
  13.     INCH("length", "in","inch","inches", 0.0254),
  14.     GRAM("weights", "g", "gram", "grams", 1.0),
  15.     KILOGRAM("weights", "kg", "kilogram", "kilograms", 1000.0),
  16.     MILLIGRAM("weights", "mg", "milligram", "milligrams", 0.001),
  17.     POUND("weights", "lb", "pound", "pounds", 453.592),
  18.     OUNCE("weights", "oz", "ounce", "ounces", 28.3495),
  19.     CELSIUS("degrees", "celsius", "degree Celsius", "degrees Celsius", 1.0),
  20.     FAHRENHEIT("degrees", "fahrenheit", "degree Fahrenheit", "degrees Fahrenheit", 1.0),
  21.     KELVIN("degrees", "k", "Kelvin", "Kelvins", 1.0),
  22.     NULL("???","???", "???", "???", 1.0)
  23. }
  24.  
  25.  
  26. fun main(args: Array<String>) {
  27.     val sc = Scanner(System.`in`)
  28.     loop@ do{
  29.  
  30.         print("Enter what you want to convert (or exit): ")
  31.         var inputAny = sc.next()
  32.         var input: Double
  33.         if(checkExit(inputAny)) {
  34.             break
  35.         } else if (isDigitsOnly(inputAny)) { // вот тут надо выяснить в строке число или слово
  36.             input = inputAny.toDouble()
  37.         } else {
  38.             print("Enter correct number")
  39.             break@loop;
  40.         }
  41.  
  42.         var inputMetric = sc.next()
  43.         if (inputMetric == "degree" || inputMetric == "degrees") {
  44.             inputMetric = sc.next()
  45.         }
  46.         var to = sc.next()
  47.         var toInputMetric = sc.next()
  48.  
  49.         /*присвоение метрик*/
  50.         var nameOfMetric = isMetric(inputMetric)
  51.         var toNameOfMetric = isMetric(toInputMetric)
  52.  
  53.         var nameOutOfMetric: String
  54.         var nameOutOfConvertMetric: String
  55.         var res: Double
  56.  
  57.         if(checkCategory(nameOfMetric, toNameOfMetric)) {
  58.             nameOutOfMetric = isPlural(input, nameOfMetric)
  59.             if(nameOfMetric.category == "degrees") {
  60.                 res = convertDegrees(input, nameOfMetric, toNameOfMetric)
  61.             } else {
  62.                
  63.                 if(checkNegative(input)){
  64.                     print(when(nameOfMetric.category) {
  65.                         "length" -> "Length shouldn't be negative"
  66.                         "weights" -> "Weight shouldn't be negative"
  67.                         else -> "????"
  68.                     })
  69.                     break;
  70.                 } else {
  71.                 res = convertWeightLength(input, nameOfMetric, toNameOfMetric)
  72.                 }
  73.             }
  74.             nameOutOfConvertMetric = isPlural(res, toNameOfMetric)
  75.             print("$input $nameOutOfMetric is $res $nameOutOfConvertMetric\n")
  76.         } else {
  77.             nameOutOfMetric = metricName(nameOfMetric, inputMetric, input)
  78.             nameOutOfConvertMetric = metricName(toNameOfMetric, toInputMetric, input)
  79.             print("Conversion from $nameOutOfMetric to $nameOutOfConvertMetric is impossible\n")
  80.         }
  81.        
  82.     } while (true)
  83. }
  84.  
  85.  
  86.  
  87. /*определение наименование входящей метрики*/
  88. fun metricName(metric: Measure, input: String, num: Double): String {
  89.     if(num == 1.0) {
  90.         return when(input.toLowerCase()) {
  91.         metric.fullNameOfMetric.toLowerCase() -> metric.fullNameOfMetric
  92.         metric.pluralNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
  93.         else -> metric.shortNameOfMetric
  94.         }
  95.     } else {
  96.         return when(input.toLowerCase()) {
  97.         metric.fullNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
  98.         metric.pluralNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
  99.         else -> metric.shortNameOfMetric
  100.         }
  101.     }
  102. }
  103.  
  104. /*конвертация метрик длины и веса*/
  105. fun convertWeightLength(input: Double, metric: Measure, toMetric: Measure): Double {
  106.     return (input * metric.coefficientNum) / toMetric.coefficientNum
  107. }
  108.  
  109. /*конвертация градусы*/
  110. fun convertDegrees(input: Double, nameOfMetric: Measure, toNameOfMetric: Measure): Double {
  111.     return when{
  112.         nameOfMetric == Measure.CELSIUS && toNameOfMetric == Measure.FAHRENHEIT -> input * 9 / 5 + 32
  113.         nameOfMetric == Measure.FAHRENHEIT && toNameOfMetric == Measure.CELSIUS -> (input - 32.0) * 5 / 9
  114.         nameOfMetric == Measure.CELSIUS && toNameOfMetric == Measure.KELVIN -> input + 273.15
  115.         nameOfMetric == Measure.KELVIN && toNameOfMetric == Measure.CELSIUS -> input - 273.15
  116.         nameOfMetric == Measure.FAHRENHEIT && toNameOfMetric == Measure.KELVIN -> (input + 459.67) * 5 / 9
  117.         nameOfMetric == Measure.KELVIN && toNameOfMetric == Measure.FAHRENHEIT -> input * 9 / 5 - 459.67
  118.         else -> input
  119.     }
  120. }
  121.  
  122. /*сопоставление категорий метрик*/
  123. fun checkCategory(metric: Measure, toMetric: Measure): Boolean{
  124.     return metric.category == toMetric.category && metric.category != "???" && toMetric.category != "???"
  125. }
  126.  
  127. /*проверка exit*/
  128. fun checkExit(input: String): Boolean{
  129.     return input == "exit"
  130. }
  131.  
  132. /**/
  133. fun checkNegative(inputNum: Double): Boolean {
  134.     return inputNum < 0.0 || inputNum < 0.0
  135. }
  136.  
  137. /*определение наименование метрики*/
  138. fun isMetric(inputMetric: String): Measure {
  139.     return when(inputMetric.toLowerCase()){
  140.         "m", "meter", "meters" -> Measure.METER
  141.         "km","kilometer","kilometers" -> Measure.KILOMETER
  142.         "cm","centimeter","centimeters" -> Measure.CENTIMETER
  143.         "mm","millimeter","millimeters" -> Measure.MILLIMETER
  144.         "mi","mile","miles" -> Measure.MILE
  145.         "yd","yard","yards" -> Measure.YARD
  146.         "ft","foot","feet" -> Measure.FOOT
  147.         "in","inch","inches" -> Measure.INCH
  148.         "g", "gram", "grams" -> Measure.GRAM
  149.         "kg", "kilogram", "kilograms" -> Measure.KILOGRAM
  150.         "mg", "milligram", "milligrams" -> Measure.MILLIGRAM
  151.         "lb", "pound", "pounds" -> Measure.POUND
  152.         "oz", "ounce", "ounces" -> Measure.OUNCE
  153.         "dc", "c", "celsius" -> Measure.CELSIUS
  154.         "df", "f", "fahrenheit" -> Measure.FAHRENHEIT
  155.         "k", "kelvin", "kelvins" -> Measure.KELVIN
  156.         else -> Measure.NULL
  157.     }
  158. }
  159.  
  160. /*определение множественности метрики*/
  161. fun isPlural(num: Double, metric: Measure): String {
  162.     return if (num == 1.0) metric.fullNameOfMetric else metric.pluralNameOfMetric
  163. }
  164. /*
  165. fun hasNumber(input: String): Boolean {
  166.     val regex: Regex = Regex("(?:-{1})?(\\d+)(?:\\.{1}\\d+)?")
  167.     val isNumber: (String) -> Boolean = regex::matches
  168.     return isNumber(input)
  169. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement