paranid5

dates

Nov 4th, 2021 (edited)
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.46 KB | None | 0 0
  1. import io.ktor.client.*
  2. import io.ktor.client.engine.cio.*
  3. import io.ktor.client.features.*
  4. import io.ktor.client.request.*
  5. import io.ktor.client.statement.*
  6. import kotlinx.coroutines.runBlocking
  7.  
  8. private inline fun readIntUntilOk(message: String, additionalChecks: (Int) -> Boolean): Int {
  9.     while (true) {
  10.         print(message)
  11.         readLine()
  12.             ?.trim()
  13.             ?.toIntOrNull()
  14.             ?.takeIf(additionalChecks)
  15.             ?.let { return it }
  16.             ?: println("Не пон")
  17.     }
  18. }
  19.  
  20. fun main() = runBlocking {
  21.     val y = readIntUntilOk("Введи номер года: ") { it in 1000..9999 }
  22.     val m = readIntUntilOk("Введи номер месяца (1 - 12): ") { it in 1..12 }
  23.     val d = readIntUntilOk("Введи номер дня (число в календаре, > 0 и < 32): ") { it in 1..31 }
  24.  
  25.     val client = HttpClient(CIO) {
  26.         expectSuccess = false
  27.     }
  28.  
  29.     println("Жди...")
  30.  
  31.     val response = client.get<HttpResponse>(
  32.         "https://isdayoff.ru/$y${"$m".padStart(2, '0')}${"$d".padStart(2, '0')}"
  33.     )
  34.  
  35.     when (response.status.value) {
  36.         200 -> when (response.readText().toIntOrNull()) {
  37.             0 -> "Рабочий"
  38.             1 -> "Выходной"
  39.             100 -> "Некорректная дата"
  40.             else -> "Хз"
  41.         }
  42.         400 -> "Некорректная дата"
  43.         else -> "Ошибка сервера"
  44.     }.let(::print)
  45. }
Add Comment
Please, Sign In to add comment