Practice Time: getFortuneCookie()




/** File:           Main.kt
* Description: Create a program with a function that returns a fortune
* cookie message that you can print.
* Programmer: Nolverto Urias Obesoo
* Creation Date: 06/13/2023
*/

fun main(args: Array<String>) {
// Use a for loop to run the program 10 times
for (iteration in 0..9){
println("Your fortune is: ${getFortuneCookie(getBirthday())}\n")
}
}

fun getFortuneCookie(birthday: Int): String{
val fortuneList = 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.")

// You can use toIntOrNull() to convert a number as a String to an Integer numeric.
// If the user enters "", toIntOrNull returns null.
// The next line gets the indexFortune calculating the reminder beteen the birthday and size of fortuneList
var indexFortune: Int? = birthday.mod(fortuneList.size)

when(birthday){
in 28..31 -> indexFortune = 0
else -> indexFortune = birthday.mod(fortuneList.size)
}

return fortuneList[indexFortune!!]
}

fun getBirthday(): Int{
print("Enter your birthday: ")

// Use readLine() to read a line of input (completed with Enter) as a String.
val birthday = readLine()!!

return birthday.toInt()

} 

Comments

Popular posts from this blog