Skip to content

Instantly share code, notes, and snippets.

@ivalexandru
Created May 29, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivalexandru/bc377c0713e3c4ebb05fe09222a0809c to your computer and use it in GitHub Desktop.
Save ivalexandru/bc377c0713e3c4ebb05fe09222a0809c to your computer and use it in GitHub Desktop.
Kotlin_functions_feedTheFish.kt
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