Library Management System
A simple console-based Library Management System built with Python.
Description
This project allows a librarian or users to manage books and members of a library.
It supports adding books, registering users, borrowing/returning books, searching books, and viewing user details.
Features
- View all books with ID, title, author, and available quantity
- Add new books
- Search books by title (case-insensitive)
- Borrow a book (only if available)
- Return a book
- View borrowed books of a specific user
- Register new users
- View all registered users
- Pre-loaded with 10 sample books and 3 sample users
Classes Used
| Class | Purpose |
|---|---|
Book |
Stores book details and manages quantity |
User |
Stores user details and borrowed books list |
Library |
Manages the collection of books and users |
How to Run
- Make sure you have Python 3 installed.
- Save the code in a file named
library_management.py - Open the terminal/command prompt
- Run the command:
python library_management.py
class Book:
def __init__(self, book_id, title, author, quantity):
self.book_id = book_id
self.title = title
self.author = author
self.quantity = quantity
def display_book_info(self):
print(f"ID: {self.book_id}, Title: {self.title}, Author: {self.author}, Available Quantity: {self.quantity}")
def check_availability(self):
return self.quantity > 0
def update_quantity(self, change):
self.quantity += change
class User:
def __init__(self, user_id, name, place):
self.user_id = user_id
self.name = name
self.place = place
self.borrowed_books = []
def borrow_book(self, book):
if book.check_availability():
self.borrowed_books.append(book)
book.update_quantity(-1)
print(f"{self.name} has borrowed '{book.title}'")
else:
print(f"Sorry, '{book.title}' is not available for borrowing.")
def return_book(self, book):
if book in self. borrowed_books:
self.borrowed_books.remove(book)
book.update_quantity(1)
print(f"{self.name} has returned '{book.title}'")
else:
print(f"{self.name} does not have '{book.title}' borrowed.")
def view_borrowed_books(self):
if self.borrowed_books:
print(f"{self.name}'s Borrowed Books:")
for book in self. borrowed_books:
print(f" - {book.title}")
else:
print(f"{self.name} has not borrowed any books.")
class Library:
def __init__(self):
self.books = []
self.users = []
def add_book(self, book):
self.booksappend(book)
def list_users(self):
if self.users:
print("\nRegistered Users:")
for user in self.users:
print(f"ID: {user.user_id}, Name: {user.name}, Place: {user.place}")
else:
print("No users registered yet.")
def register_user(self, user):
if any(u.user_id == user.user_id for u in self.users):
print("User with the same ID already exists! Try another ID.")
return
self.usersappend(user)
print(f"New user {user.name} added successfully.")
def search_book_by_title(self, title):
return [book for book in selfbooks if title.lower() in book.title.lower()]
def list_books(self):
if self.books:
print("Books in the library:")
for book in self. books:
book.display_book_info()
else:
print("No books available in the library.")
def add_new_book(self):
book_id = int(input("Enter Book ID: "))
if any(book.book_id == book_id for book in self.books):
print("Book with the same ID already exists, please try another ID.")
return
title = input("Enter the book title: ")
author = input("Enter author name: ")
quantity = int(input("Enter the quantity of book: "))
new_book = Book(book_id, title, author, quantity)
self.add_book(new_book)
print("Book added successfully!")
new_book.display_book_info()
def main():
library = Library()
# Sample books
book1 = Book(1, "Harry Potter and the Philosopher's Stone", "J.K. Rowling", 5)
book2 = Book(2, "The Hobbit", "J.R.R. Tolkien", 3)
book3 = Book(3, "1984", "George Orwell", 2)
book4 = Book(4,"AI Rising: India’s Artificial Intelligence Growth Story"," Jayanth N. Kolla, Leslie D’Monte",5)
book5 = Book(5, "The Great Gatsby", "F. Scott Fitzgerald", 4)
book6 = Book(6, "To Kill a Mockingbird", "Harper Lee", 3)
book7 = Book(7,"The Power of Critical Thinking","Richard Paul",5)
book8 = Book(8, "From Dump to Design", "John Doe", 2)
book9 = Book(9, "The Catcher in the Rye", "J.D. Salinger", 3)
book10= Book(10, "The Alchemist", "Paulo Coelho", 4)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.add_book(book4)
library.add_book(book5)
library.add_book(book6)
library.add_book(book7)
library.add_book(book8)
library.add_book(book9)
library.add_book(book10)
# Sample users (now properly registered)
user1 = User(1, "Karthick", "Chennai")
user2 = User(2, "Kaviya", "Perambalur")
user3 = User(3, "Praveen", "Trichy")
library.register_user(user1)
library.register_user(user2)
library.register_user(user3)
while True:
print("\nWelcome to the Library Management System")
print("1. View all books")
print("2. Add Books")
print("3. Search for a book by title")
print("4. Borrow a book")
print("5. Return a book")
print("6. View borrowed books")
print("7. Add new User")
print("8. Exit")
print("9. View all users")
choice = input("Enter your choice: ")
if choice == "1":
library.list_books()
elif choice == "2":
library.add_new_book()
elif choice == "3":
title = input("Enter the book title to search: ")
found_books = library.search_book_by_title(title)
if found_books:
for book in found_books:
book.display_book_info()
else:
print(f"No book found with the title '{title}'")
elif choice == "4":
user_id = int(input("Enter User ID: "))
book_id = int(input("Enter Book ID to Borrow: "))
user = next((u for u in library.users if u.user_id == user_id), None)
book = next((b for b in library.books if b.book_id == book_id), None)
if user and book:
user.borrow_book(book)
else:
print("Invalid User ID or Book ID")
elif choice == "5":
user_id = int(input("Enter your user ID: "))
book_id = int(input("Enter the book ID to return: "))
user = next((u for u in library.users if u.user_id == user_id), None)
book = next((b for b in library.books if b.book_id == book_id), None)
if user and book:
user.return_book(book)
else:
print("Invalid user or book ID.")
elif choice == "6":
user_id = int(input("Enter your user ID: "))
user = next((u for u in library.users if u.user_id == user_id), None)
if user:
user.view_borrowed_books()
else:
print("Invalid user ID.")
elif choice == "7":
user_id = int(input("Enter user id: "))
name = input("Enter user's name: ")
place = input("Enter user's place: ")
user = User(user_id, name, place)
library.register_user(user)
elif choice == "8":
print("Exiting the system. Goodbye!")
break
elif choice == "9":
library.list_users()
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
main()
Top comments (0)