CodeBlanks Logo Icon CodeBlanks

Library class

Set up collection management with the Library class.

The Library class

The brain of the library is the Library class. It holds two mutable lists for books and members: val books = mutableListOf<Book>() and val members = mutableListOf<Member>().

To add elements you write fun addBook(book: Book) and fun addMember(member: Member). Both work with a single line like books.add(book). This way the library manages its book and member collections; checkout and search will build on these lists in later lessons.

addBook and addMember

The add method appends an element to a list. The Library class wraps it twice: fun addBook(book: Book) { books.add(book) } and fun addMember(member: Member) { members.add(member) }.

From the outside you call lib.addBook(Book(...)) while the list itself stays hidden. This is the encapsulation principle: even though the list variable is val, its contents are managed through the add methods. To print the list use books.forEach { println(it) }.

Practice Define the Library class

Define the Book and Member data classes as in the previous lesson. Then define class Library with val books = mutableListOf<Book>() and val members = mutableListOf<Member>(). Add fun addBook(book: Book) and fun addMember(member: Member). In main add two books and print them with books.forEach { println(it) }.

Starter code

Main.kt
data class Book(val title: String, val author: String, val isbn: String)
data class Member(val name: String, val id: String)

// Define the Library class: books and members lists, addBook and addMember methods

fun main() {
    val lib = Library()
    // Add two books with addBook and print books with forEach
}

Sample solution

Main.kt
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>()
    fun addBook(book: Book) { books.add(book) }
    fun addMember(member: Member) { members.add(member) }
}

fun main() {
    val lib = Library()
    lib.addBook(Book("War and Peace", "Tolstoy", "978-1"))
    lib.addBook(Book("Anna Karenina", "Tolstoy", "978-2"))
    lib.books.forEach { println(it) }
}

Expected output

Book(title=War and Peace, author=Tolstoy, isbn=978-1)
Book(title=Anna Karenina, author=Tolstoy, isbn=978-2)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the book list

Question
class Library {
    val books = mutable____<Book>()
}
Answer
class Library {
    val books = mutableListOf<Book>()
}

Explanation: mutableListOf creates a mutable book list.

2 Match the Library parts

Term Meaning
addBook Adds a book
addMember Adds a member
books Book list
members Member list

3 What does mutableListOf&lt;Book&gt;() produce?

  • Fixed-size array
  • Read-only list
  • Single-element set
  • Mutable list Correct answer

Explanation: mutableListOf returns a mutable list that supports add and remove.

All lessons in Intermediate