Practice Time: shouldChangeWater()

 

import java.util.Random

// File: Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/15/2023
// Email: nolvertou@gmail.com
// Description:

fun main() {
feedTheFish()
}

fun feedTheFish() {
println("Hello, Kotlin")
val day = randomDay()
val food = fishFood(day)
println("Today is $day and the fish eat $food")

shouldChangeWater(day, 20, 50)
shouldChangeWater(day)
shouldChangeWater(day, dirty = 50)

if(shouldChangeWater(day)){
println("Change the water today")
}
}

fun fishFood(day: String): String{
var food = "fasting" // Default Food for the fish

return when(day){
"Monday" -> "flakes"
"Wednesday" -> "redworms"
"Thursday" -> "mosquitoes"
"Sunday" -> "plankton"
else -> "fasting"
}
}

fun randomDay(): String {
val week = listOf("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
return week[Random().nextInt(7)]
}

fun shouldChangeWater(
day: String,
temperature: Int = 22,
dirty: Int = getDirtySensorReading()
): Boolean{

return when{
isTooHot(temperature) -> true
isDirty(dirty) -> true
isSunday(day) -> true
else -> false
}
}

fun isTooHot(temperature: Int) = temperature > 30
fun isDirty(dirty: Int) = dirty > 30
fun isSunday(day: String) = day == "Sunday"

fun getDirtySensorReading() = 20


Comments

Popular posts from this blog