Skip to content

Kotlin Hazi #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/src/main/kotlin/WeatherApp.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
package hu.vanio.kotlin.feladat.ms

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.net.URL
import java.time.LocalDate
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import java.io.IOException

@SpringBootApplication
class WeatherApp

fun main() {
runApplication<WeatherApp>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha ennyire minimál megoldás lett (nem baj!), akkor felesleges bele a SpringBootApplication

val url = "https://api.open-meteo.com/v1/forecast?latitude=47.4984&longitude=19.0404&hourly=temperature_2m&timezone=auto"

try {
val json = URL(url).readText()
val forecast = jacksonObjectMapper().readValue<Forecast>(json)

val dailyAverages = averageTempreature(forecast)
printForecast(dailyAverages)

} catch (e: IOException) {
println("Adatok letöltése sikertelen: ${e.message}")
} catch (e: JsonProcessingException) {
println("JSON feldolgozása sikertelen: ${e.message}")
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
data class Forecast(val hourly: HourlyData)
//@JsonIgnoreProperties(ignoreUnknown = true)
data class HourlyData(val time: List<String>, val temperature_2m: List<Double>)

fun averageTempreature(forecast: Forecast): Map<LocalDate, Double> {
val dailyTemperatures = mutableMapOf<LocalDate, MutableList<Double>>()

forecast.hourly.time.forEachIndexed { index, timestamp ->
val date = LocalDate.parse(timestamp.substring(0, 10))
val temperature = forecast.hourly.temperature_2m[index]

if (dailyTemperatures.containsKey(date)) {
dailyTemperatures[date]!!.add(temperature)
} else {
dailyTemperatures[date] = mutableListOf(temperature)
}
}
return dailyTemperatures.mapValues { it.value.average() }
}

fun printForecast(dailyAverages: Map<LocalDate, Double>) {
dailyAverages.forEach { (date, temperature) ->
println("$date: $temperature °C")
}
}
50 changes: 48 additions & 2 deletions app/src/test/kotlin/WeatherAppTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
package hu.vanio.kotlin.feladat.ms

import java.time.LocalDate
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class WeatherAppTest {

@Test fun `sikeres lekerdezes`() {
TODO()
@Test
fun `sikeres lekerdezes`() {
val forecast = Forecast(
HourlyData(
listOf(
"2024-03-18T00:00", "2024-03-18T01:00", "2024-03-18T02:00",
"2024-03-19T00:00", "2024-03-19T01:00", "2024-03-19T02:00",
"2024-03-20T00:00", "2024-03-20T01:00", "2024-03-20T02:00"
),
listOf(
6.0, 5.7, 5.4, 5.2, 4.3, 4.6, 6.5, 7.7, 8.9
)
)
)
val result = averageTempreature(forecast)

val expected = mapOf(
LocalDate.parse("2024-03-18") to 5.7,
LocalDate.parse("2024-03-19") to 4.7,
LocalDate.parse("2024-03-20") to 7.7
)
assertEquals(expected, result)
}

@Test
fun `sikertelen lekerdezes`() {
val forecast = Forecast(
HourlyData(
listOf("2024-03-18T00:00", "2024-03-18T01:00", "2024-03-18T02:00",
"2024-03-19T00:00", "2024-03-19T01:00", "2024-03-19T02:00",
"2024-03-20T00:00", "2024-03-20T01:00", "2024-03-20T02:00"
),
listOf(
6.0, 5.7, 5.4, 5.2, 4.3, 4.6, 6.5, 7.7, 11.1
)
)
)
val result = averageTempreature(forecast)

val expected = mapOf(
LocalDate.parse("2024-03-18") to 5.7,
LocalDate.parse("2024-03-19") to 4.7,
LocalDate.parse("2024-03-20") to 7.4
)
//3.nap 8.43
assertNotEquals(expected, result);
}
}