Kotlin
Putting it together
Combine the quiz functions with a menu loop.
Putting it together
In the fifth and final lesson we combine all pieces with a menu loop. The runMenu function takes a list of actions and processes each one in order.
Example: fun runMenu(actions: List<String>) { for (action in actions) { when (action) { "quiz" -> runQuiz(); "score" -> println(score(listOf("Ankara", "5"))); else -> println("Unknown") } } }. With when each action type maps to a function call. In main questions are added, then runMenu(listOf("quiz", "score")) is called. All functions (Question, addQuestion, runQuiz, score) come from the previous lessons.
Menu loop
runMenu takes a List<String> and processes each action in order. With when it calls the matching function for each action type.
Example: runMenu(listOf("quiz", "score")) first calls runQuiz() (prints all questions), then println(score(listOf("Ankara", "5"))) (prints the score). Unknown actions go to the else -> println("Unknown") branch. The menu loop uses a fixed action list instead of user input, so the output is deterministic.
Exercise Combine the quiz with runMenu
Build on the previous lessons' solution: data class Question(val text: String, val answer: String), val questions: MutableList<Question> = mutableListOf(), fun addQuestion(text: String, answer: String), fun runQuiz() and fun score(answers: List<String>): Int are defined. Add a fun runMenu(actions: List<String>) function whose body iterates actions with for (action in actions) and uses when (action) to call runQuiz() for "quiz", println(score(listOf("Ankara", "5"))) for "score", and println("Unknown") for else. In main add two questions and call runMenu(listOf("quiz", "score")).
Show example solution
data class Question(val text: String, val answer: String)
val questions: MutableList<Question> = mutableListOf()
fun addQuestion(text: String, answer: String) {
questions.add(Question(text, answer))
}
fun runQuiz() {
for (q in questions) {
println(q.text)
println("Answer: ${q.answer}")
}
}
fun score(answers: List<String>): Int {
var correct = 0
for (i in questions.indices) {
if (questions[i].answer == answers[i]) correct++
}
return correct
}
fun runMenu(actions: List<String>) {
for (action in actions) {
when (action) {
"quiz" -> runQuiz()
"score" -> println(score(listOf("Ankara", "5")))
else -> println("Unknown")
}
}
}
fun main() {
addQuestion("Capital city?", "Ankara")
addQuestion("2+2?", "4")
runMenu(listOf("quiz", "score"))
}
Test yourself
-
What does the <code>runMenu</code> function use to process actions?
- if-else if
- when (action)
- try-catch
- recursion
Show answer
when (action) — when binds each action to a function.
-
Match the runMenu parts
- fun runMenu(actions)
- Function signature
- for (action in actions)
- Action loop
- when (action)
- Branching
- else -> println
- Unknown branch
-
Complete the runMenu branching
for (action in actions) {
____ (action) {
"quiz" -> runQuiz()
"score" -> println(score(listOf("Ankara", "5")))
else -> println("Unknown")
}
}
Show answer
for (action in actions) {
when (action) {
"quiz" -> runQuiz()
"score" -> println(score(listOf("Ankara", "5")))
else -> println("Unknown")
}
}
Next lesson: Generic class