Kotlin
Putting it together
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.
Exercise 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.
Show example 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) }
}
}
Test yourself
-
Match the when branches
- "checkout"
- Check out
- "return"
- Return
- "list"
- List
- when (command)
- Pick command
-
What does when (command) { "checkout" -> ... } do?
- Does nothing
- Runs all branches
- Runs the branch that matches the command
- Runs only the first branch
Show answer
Runs the branch that matches the command — when executes only the matching branch.
-
Complete the when branch
when (command) {
"checkout" -> lib.____("M1", "978-1")
}
Show answer
when (command) {
"checkout" -> lib.checkout("M1", "978-1")
}
Next lesson: map and filter