Putting it together
- Kotlin course
- Intermediate
- Lesson 63/116
- 6 min
Combine the Library class with a menu loop.
Putting it together
In the final lesson you combine all pieces with a when expression. Write a command handler like when (command) { "checkout" -> lib.checkout("M1", "978-1"); "return" -> lib.returnBook("978-1"); "list" -> lib.books.forEach { println(it) } }.
Each branch represents a different action: checkout lends a book, return takes it back, list prints all books. The command variable decides which branch runs; only the matching branch executes. This prepares the library app for a command loop.
Command handler
The whole app comes together: the Library class carries book, member, checkout and search management; main reads a command and calls the relevant method with when. Example: val command = "checkout"; when (command) { "checkout" -> lib.checkout("M1", "978-1") }.
You could wrap this in a loop to build a real console app that reads commands from the user. For now you test the flow by handling a single command. In this lesson the checkout command runs and prints a checkout message.
Practice Handle a command with when
In main create a Library, add a book and a member. Define val command = "checkout" and write when (command) with three branches: "checkout" -> lib.checkout("M1", "978-1"), "return" -> lib.returnBook("978-1"), "list" -> lib.books.forEach { println(it) }. Keep the Library class from previous lessons.
Starter code
fun main() {
val lib = Library()
lib.addBook(Book("War and Peace", "Tolstoy", "978-1"))
lib.addMember(Member("Alex", "M1"))
// Handle the command with when and print the result
}
// The Book, Member and Library classes stay as defined in previous lessons.
// Library: books, members, borrowed, addBook, addMember, checkout, returnBook, searchByAuthor, availableBooksSample solution
data class Book(val title: String, val author: String, val isbn: String)
data class Member(val name: String, val id: String)
class Library {
val books = mutableListOf<Book>()
val members = mutableListOf<Member>()
val borrowed = mutableMapOf<String, String>()
fun addBook(book: Book) { books.add(book) }
fun addMember(member: Member) { members.add(member) }
fun checkout(memberId: String, isbn: String) {
borrowed[isbn] = memberId
println("Checkout: $memberId -> $isbn")
}
fun returnBook(isbn: String) {
borrowed.remove(isbn)
println("Return: $isbn")
}
fun searchByAuthor(author: String): List<Book> = books.filter { it.author == author }
fun availableBooks(): List<Book> = books.filter { it.isbn !in borrowed }
}
fun main() {
val lib = Library()
lib.addBook(Book("War and Peace", "Tolstoy", "978-1"))
lib.addMember(Member("Alex", "M1"))
val command = "checkout"
when (command) {
"checkout" -> lib.checkout("M1", "978-1")
"return" -> lib.returnBook("978-1")
"list" -> lib.books.forEach { println(it) }
}
}Expected output
Checkout: M1 -> 978-1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the when branches
| Term | Meaning |
|---|---|
"checkout" |
Check out |
"return" |
Return |
"list" |
List |
when (command) |
Pick command |
2 What does when (command) { "checkout" -> ... } do?
- Does nothing
- Runs all branches
- Runs the branch that matches the command Correct answer
- Runs only the first branch
Explanation: when executes only the matching branch.
3 Complete the when branch
when (command) {
"checkout" -> lib.____("M1", "978-1")
}
when (command) {
"checkout" -> lib.checkout("M1", "978-1")
}
Explanation: The checkout branch calls the checkout method.