Library Management System Project in Kotlin

In this blog post, we will how to create a simple Library Management System (LMS). We are going to use an in-memory object to store the data (not a database).

System Overview

The LMS will allow users to: 

  1. Add and remove books. 
  2. Borrow and return books. 
  3. Display all available books. 
  4. Check the book availability.

Implementation

1. Class Definitions: 

Our system consists of two main classes: the Book and Library.

Book Class

data class Book(val bookId: Int, var title: String, var isBorrowed: Boolean = false)

The Book class represents an individual book with an ID, title, and status indicating whether it's borrowed.

Library class

The Library class contains a list of books. It provides methods for adding, removing, borrowing, returning, listing, and checking book availability. 

class Library {
    private val books = mutableListOf<Book>()

    fun addBook(book: Book) {
        books.add(book)
    }

    fun removeBook(bookId: Int) {
        books.removeIf { it.bookId == bookId }
    }

    fun borrowBook(bookId: Int): Boolean {
        val book = books.find { it.bookId == bookId && !it.isBorrowed }
        return if (book != null) {
            book.isBorrowed = true
            true
        } else false
    }

    fun returnBook(bookId: Int) {
        val book = books.find { it.bookId == bookId && it.isBorrowed }
        book?.isBorrowed = false
    }

    fun listAvailableBooks() {
        val availableBooks = books.filter { !it.isBorrowed }
        for (book in availableBooks) {
            println("${book.bookId} - ${book.title}")
        }
    }

    fun isBookAvailable(bookId: Int): Boolean {
        val book = books.find { it.bookId == bookId }
        return book?.isBorrowed == false
    }
}

2. Main Function:

The main() function offers an interactive menu for users to operate the library system.
fun main() {
    val library = Library()

    loop@ while (true) {
        println("\nOptions:\n1. Add Book\n2. Remove Book\n3. Borrow Book\n4. Return Book\n5. List Available Books\n6. Check Book Availability\n7. Exit")
        when (readLine()?.toIntOrNull()) {
            1 -> {
                println("Enter Book ID: ")
                val id = readLine()!!.toInt()
                println("Enter Book Title: ")
                val title = readLine()!!
                library.addBook(Book(id, title))
                println("Book added.")
            }
            // ... handle other options in a similar manner ...
            7 -> {
                println("Exiting...")
                break@loop
            }
            else -> println("Invalid choice!")
        }
    }
}

Complete Working Code with Output

To test the program: 
  • Copy the complete code. 
  • Use a Kotlin compiler or IDE to compile the code and run the program. 
  • Follow the menu options to add books, register members, issue, and return books.
data class Book(val bookId: Int, var title: String, var isBorrowed: Boolean = false)

class Library {
    private val books = mutableListOf<Book>()

    fun addBook(book: Book) {
        books.add(book)
    }

    fun removeBook(bookId: Int) {
        books.removeIf { it.bookId == bookId }
    }

    fun borrowBook(bookId: Int): Boolean {
        val book = books.find { it.bookId == bookId && !it.isBorrowed }
        return if (book != null) {
            book.isBorrowed = true
            true
        } else false
    }

    fun returnBook(bookId: Int) {
        val book = books.find { it.bookId == bookId && it.isBorrowed }
        book?.isBorrowed = false
    }

    fun listAvailableBooks() {
        val availableBooks = books.filter { !it.isBorrowed }
        for (book in availableBooks) {
            println("${book.bookId} - ${book.title}")
        }
    }

    fun isBookAvailable(bookId: Int): Boolean {
        val book = books.find { it.bookId == bookId }
        return book?.isBorrowed == false
    }
}

fun main() {
    val library = Library()

    loop@ while (true) {
        println("\nOptions:\n1. Add Book\n2. Remove Book\n3. Borrow Book\n4. Return Book\n5. List Available Books\n6. Check Book Availability\n7. Exit")
        when (readLine()?.toIntOrNull()) {
            1 -> {
                println("Enter Book ID: ")
                val id = readLine()!!.toInt()
                println("Enter Book Title: ")
                val title = readLine()!!
                library.addBook(Book(id, title))
                println("Book added.")
            }
            2 -> {
                println("Enter Book ID to remove: ")
                val id = readLine()!!.toInt()
                library.removeBook(id)
                println("Book removed.")
            }
            3 -> {
                println("Enter Book ID to borrow: ")
                val id = readLine()!!.toInt()
                if (library.borrowBook(id)) {
                    println("Book borrowed.")
                } else {
                    println("Book unavailable or already borrowed.")
                }
            }
            4 -> {
                println("Enter Book ID to return: ")
                val id = readLine()!!.toInt()
                library.returnBook(id)
                println("Book returned.")
            }
            5 -> {
                println("Available books:")
                library.listAvailableBooks()
            }
            6 -> {
                println("Enter Book ID to check availability: ")
                val id = readLine()!!.toInt()
                if (library.isBookAvailable(id)) {
                    println("Book is available.")
                } else {
                    println("Book is currently borrowed.")
                }
            }
            7 -> {
                println("Exiting...")
                break@loop
            }
            else -> println("Invalid choice!")
        }
    }
}
Output:
Options:
1. Add Book
2. Remove Book
3. Borrow Book
4. Return Book
5. List Available Books
6. Check Book Availability
7. Exit

Enter your choice: 1
Enter Book ID: 1001
Enter Book Title: Kotlin for Beginners
Book added.

Options:
...
Enter your choice: 1
Enter Book ID: 1002
Enter Book Title: Advanced Kotlin Programming
Book added.

Options:
...
Enter your choice: 5
Available books:
1001 - Kotlin for Beginners
1002 - Advanced Kotlin Programming

Options:
...
Enter your choice: 3
Enter Book ID to borrow: 1001
Book borrowed.

Options:
...
Enter your choice: 6
Enter Book ID to check availability: 1001
Book is currently borrowed.

Options:
...
Enter your choice: 4
Enter Book ID to return: 1001
Book returned.

Options:
...
Enter your choice: 7
Exiting...

Conclusion

Building a Library Management System in Kotlin showcases the simplicity and power of the language. With Kotlin's versatile features, one can expand this basic system by integrating databases, creating user interfaces, and adding advanced functionalities like user accounts, fines for late returns, etc.

Whether you are developing a small-scale application or a large enterprise system, Kotlin offers a blend of modern features and ease of use, making it an excellent choice for developers.

Comments