🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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:
- Add and remove books.
- Borrow and return books.
- Display all available books.
- 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:
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
- 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!")
}
}
}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...
Comments
Post a Comment
Leave Comment