Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package converter
- import java.util.*
- enum class Measure(val category: String, val shortNameOfMetric: String, val fullNameOfMetric: String, val pluralNameOfMetric: String, val coefficientNum: Double){
- METER("length", "m", "meter", "meters", 1.0),
- KILOMETER("length", "km","kilometer","kilometers", 1000.0),
- CENTIMETER("length", "cm","centimeter","centimeters", 0.01),
- MILLIMETER("length", "mm","millimeter","millimeters", 0.001),
- MILE("length", "mi","mile","miles", 1609.35),
- YARD("length", "yd","yard","yards", 0.9144),
- FOOT("length", "ft","foot","feet", 0.3048),
- INCH("length", "in","inch","inches", 0.0254),
- GRAM("weights", "g", "gram", "grams", 1.0),
- KILOGRAM("weights", "kg", "kilogram", "kilograms", 1000.0),
- MILLIGRAM("weights", "mg", "milligram", "milligrams", 0.001),
- POUND("weights", "lb", "pound", "pounds", 453.592),
- OUNCE("weights", "oz", "ounce", "ounces", 28.3495),
- CELSIUS("degrees", "celsius", "degree Celsius", "degrees Celsius", 1.0),
- FAHRENHEIT("degrees", "fahrenheit", "degree Fahrenheit", "degrees Fahrenheit", 1.0),
- KELVIN("degrees", "k", "Kelvin", "Kelvins", 1.0),
- NULL("???","???", "???", "???", 1.0)
- }
- fun main(args: Array<String>) {
- val sc = Scanner(System.`in`)
- loop@ do{
- print("Enter what you want to convert (or exit): ")
- var inputAny = sc.next()
- var input: Double
- if(checkExit(inputAny)) {
- break
- } else if (isDigitsOnly(inputAny)) { // вот тут надо выяснить в строке число или слово
- input = inputAny.toDouble()
- } else {
- print("Enter correct number")
- break@loop;
- }
- var inputMetric = sc.next()
- if (inputMetric == "degree" || inputMetric == "degrees") {
- inputMetric = sc.next()
- }
- var to = sc.next()
- var toInputMetric = sc.next()
- /*присвоение метрик*/
- var nameOfMetric = isMetric(inputMetric)
- var toNameOfMetric = isMetric(toInputMetric)
- var nameOutOfMetric: String
- var nameOutOfConvertMetric: String
- var res: Double
- if(checkCategory(nameOfMetric, toNameOfMetric)) {
- nameOutOfMetric = isPlural(input, nameOfMetric)
- if(nameOfMetric.category == "degrees") {
- res = convertDegrees(input, nameOfMetric, toNameOfMetric)
- } else {
- if(checkNegative(input)){
- print(when(nameOfMetric.category) {
- "length" -> "Length shouldn't be negative"
- "weights" -> "Weight shouldn't be negative"
- else -> "????"
- })
- break;
- } else {
- res = convertWeightLength(input, nameOfMetric, toNameOfMetric)
- }
- }
- nameOutOfConvertMetric = isPlural(res, toNameOfMetric)
- print("$input $nameOutOfMetric is $res $nameOutOfConvertMetric\n")
- } else {
- nameOutOfMetric = metricName(nameOfMetric, inputMetric, input)
- nameOutOfConvertMetric = metricName(toNameOfMetric, toInputMetric, input)
- print("Conversion from $nameOutOfMetric to $nameOutOfConvertMetric is impossible\n")
- }
- } while (true)
- }
- /*определение наименование входящей метрики*/
- fun metricName(metric: Measure, input: String, num: Double): String {
- if(num == 1.0) {
- return when(input.toLowerCase()) {
- metric.fullNameOfMetric.toLowerCase() -> metric.fullNameOfMetric
- metric.pluralNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
- else -> metric.shortNameOfMetric
- }
- } else {
- return when(input.toLowerCase()) {
- metric.fullNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
- metric.pluralNameOfMetric.toLowerCase() -> metric.pluralNameOfMetric
- else -> metric.shortNameOfMetric
- }
- }
- }
- /*конвертация метрик длины и веса*/
- fun convertWeightLength(input: Double, metric: Measure, toMetric: Measure): Double {
- return (input * metric.coefficientNum) / toMetric.coefficientNum
- }
- /*конвертация градусы*/
- fun convertDegrees(input: Double, nameOfMetric: Measure, toNameOfMetric: Measure): Double {
- return when{
- nameOfMetric == Measure.CELSIUS && toNameOfMetric == Measure.FAHRENHEIT -> input * 9 / 5 + 32
- nameOfMetric == Measure.FAHRENHEIT && toNameOfMetric == Measure.CELSIUS -> (input - 32.0) * 5 / 9
- nameOfMetric == Measure.CELSIUS && toNameOfMetric == Measure.KELVIN -> input + 273.15
- nameOfMetric == Measure.KELVIN && toNameOfMetric == Measure.CELSIUS -> input - 273.15
- nameOfMetric == Measure.FAHRENHEIT && toNameOfMetric == Measure.KELVIN -> (input + 459.67) * 5 / 9
- nameOfMetric == Measure.KELVIN && toNameOfMetric == Measure.FAHRENHEIT -> input * 9 / 5 - 459.67
- else -> input
- }
- }
- /*сопоставление категорий метрик*/
- fun checkCategory(metric: Measure, toMetric: Measure): Boolean{
- return metric.category == toMetric.category && metric.category != "???" && toMetric.category != "???"
- }
- /*проверка exit*/
- fun checkExit(input: String): Boolean{
- return input == "exit"
- }
- /**/
- fun checkNegative(inputNum: Double): Boolean {
- return inputNum < 0.0 || inputNum < 0.0
- }
- /*определение наименование метрики*/
- fun isMetric(inputMetric: String): Measure {
- return when(inputMetric.toLowerCase()){
- "m", "meter", "meters" -> Measure.METER
- "km","kilometer","kilometers" -> Measure.KILOMETER
- "cm","centimeter","centimeters" -> Measure.CENTIMETER
- "mm","millimeter","millimeters" -> Measure.MILLIMETER
- "mi","mile","miles" -> Measure.MILE
- "yd","yard","yards" -> Measure.YARD
- "ft","foot","feet" -> Measure.FOOT
- "in","inch","inches" -> Measure.INCH
- "g", "gram", "grams" -> Measure.GRAM
- "kg", "kilogram", "kilograms" -> Measure.KILOGRAM
- "mg", "milligram", "milligrams" -> Measure.MILLIGRAM
- "lb", "pound", "pounds" -> Measure.POUND
- "oz", "ounce", "ounces" -> Measure.OUNCE
- "dc", "c", "celsius" -> Measure.CELSIUS
- "df", "f", "fahrenheit" -> Measure.FAHRENHEIT
- "k", "kelvin", "kelvins" -> Measure.KELVIN
- else -> Measure.NULL
- }
- }
- /*определение множественности метрики*/
- fun isPlural(num: Double, metric: Measure): String {
- return if (num == 1.0) metric.fullNameOfMetric else metric.pluralNameOfMetric
- }
- /*
- fun hasNumber(input: String): Boolean {
- val regex: Regex = Regex("(?:-{1})?(\\d+)(?:\\.{1}\\d+)?")
- val isNumber: (String) -> Boolean = regex::matches
- return isNumber(input)
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement