Created
May 29, 2018 15:22
-
-
Save ivalexandru/bc377c0713e3c4ebb05fe09222a0809c to your computer and use it in GitHub Desktop.
Kotlin_functions_feedTheFish.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
fun main(args: Array<String>) { | |
println("Hello, ${args[0]}!") | |
feedTheFish() | |
} | |
fun feedTheFish(){ | |
val day = randomDay() //we call the function here | |
val food = fishFood(day) | |
println("Today is $day and the fish eat $food") | |
} | |
fun randomDay():String { | |
val week = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") | |
return week[Random().nextInt(7)] //randomly chose index in our list | |
} | |
fun fishFood(day: String) :String { | |
var food = "fasting" | |
//since EVERYTHING in kotlin has a value, when also has a value. this value will be the last expression of the branch that was picked.asa ca poti sa return direct la when, fara sa mai pui o alta variabila | |
//adica nu mai tre sa pui "Monday" -> food = "flakes" | |
return when (day) { | |
"Monday" -> "flakes" | |
"Tuesday" -> "pellets" | |
"Wednesday" ->"redworms" | |
"Thursday" -> "granules" | |
"Friday" -> "mosquitoes" | |
"Saturday" -> "lettuce" | |
"Sunday" -> "plankton" | |
else -> "fasting" | |
} | |
return food | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment