Skip to content

Instantly share code, notes, and snippets.

@ivalexandru
Created May 29, 2018 15:23
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/780ab85786c2565ac19c67435b7b03cf to your computer and use it in GitHub Desktop.
Save ivalexandru/780ab85786c2565ac19c67435b7b03cf to your computer and use it in GitHub Desktop.
Kotlin_fortuneCookie.kt
//Create a function called getBirthday() that gets the birthday from the user.
//Pass the result of getBirthday() to getFortune() using an Integer argument, and use it to return the correct fortune.
//Remove getting the birthday from getFortune()
//Instead of calculating the fortune based on the birthday, use a when statement to assign some fortunes as follows (or use your own conditions):
//
//Hint: There are several ways in which to make this when statement. How much can you Kotlinize it?
//
//Starter Code:
fun main(args: Array<String>) {
fun getBirthday(): Int {
print("\nEnter your birthday: ")
return readLine()?.toIntOrNull() ?: 1
}
fun getFortune(birthday: Int): String {
val fortunes = listOf("You will have a great day!",
"Things will go well for you today.",
"Enjoy a wonderful day of success.",
"Be humble and all will turn out well.",
"Today is a good day for exercising restraint.",
"Take it easy and enjoy life!",
"Treasure your friends, because they are your greatest fortune.")
val index = when (birthday) {
in 1..7 -> 4
28, 31 -> 2
else -> birthday.rem(fortunes.size)
}
return fortunes[index]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment