Library management systems are crucial in organizing and maintaining the records of books, members, and transactions in a library. In this blog post, we'll create a simple library management system using Python's object-oriented features.
Key Features
Book Management: Add, view, and remove books.
Member Management: Register and view members.
Issue & Return Books: Allow members to borrow and return books.
Implementation
1. Class Definitions:
We'll start with the Book and Member classes.
class Book:
def __init__(self, book_id, title, author):
self.book_id = book_id
self.title = title
self.author = author
self.is_available = True
def __str__(self):
return f"{self.book_id}. {self.title} by {self.author}"
class Member:
def __init__(self, member_id, name):
self.member_id = member_id
self.name = name
self.borrowed_books = []
def __str__(self):
return f"{self.member_id}. {self.name}"
def borrow_book(self, book):
self.borrowed_books.append(book)
book.is_available = False
def return_book(self, book):
self.borrowed_books.remove(book)
book.is_available = True
2. Library Class:
This class will contain the main logic of our library system.
class Library:
def __init__(self):
self.books = []
self.members = []
def add_book(self, book):
self.books.append(book)
def register_member(self, member):
self.members.append(member)
def issue_book(self, member_id, book_id):
member = next((m for m in self.members if m.member_id == member_id), None)
book = next((b for b in self.books if b.book_id == book_id and b.is_available), None)
if member and book:
member.borrow_book(book)
print(f"Book {book.title} issued to {member.name}.")
else:
print("Book cannot be issued.")
def return_book(self, member_id, book_id):
member = next((m for m in self.members if m.member_id == member_id), None)
book = next((b for b in self.books if b.book_id == book_id and not b.is_available), None)
if member and book and book in member.borrowed_books:
member.return_book(book)
print(f"Book {book.title} returned by {member.name}.")
else:
print("Book cannot be returned.")
3. Main Function:
This function provides a menu-driven interface for the library.
def main():
library = Library()
while True:
print("\n1. Add Book\n2. Register Member\n3. Issue Book\n4. Return Book\n5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
book_id = int(input("Enter Book ID: "))
title = input("Enter Book Title: ")
author = input("Enter Book Author: ")
library.add_book(Book(book_id, title, author))
elif choice == 2:
member_id = int(input("Enter Member ID: "))
name = input("Enter Member Name: ")
library.register_member(Member(member_id, name))
elif choice == 3:
member_id = int(input("Enter Member ID: "))
book_id = int(input("Enter Book ID to issue: "))
library.issue_book(member_id, book_id)
elif choice == 4:
member_id = int(input("Enter Member ID: "))
book_id = int(input("Enter Book ID to return: "))
library.return_book(member_id, book_id)
elif choice == 5:
print("Exiting...")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
Complete Source with Output
class Book:
def __init__(self, book_id, title, author):
self.book_id = book_id
self.title = title
self.author = author
self.is_available = True
def __str__(self):
return f"{self.book_id}. {self.title} by {self.author}"
class Member:
def __init__(self, member_id, name):
self.member_id = member_id
self.name = name
self.borrowed_books = []
def __str__(self):
return f"{self.member_id}. {self.name}"
def borrow_book(self, book):
self.borrowed_books.append(book)
book.is_available = False
def return_book(self, book):
self.borrowed_books.remove(book)
book.is_available = True
class Library:
def __init__(self):
self.books = []
self.members = []
def add_book(self, book):
self.books.append(book)
def register_member(self, member):
self.members.append(member)
def issue_book(self, member_id, book_id):
member = next((m for m in self.members if m.member_id == member_id), None)
book = next((b for b in self.books if b.book_id == book_id and b.is_available), None)
if member and book:
member.borrow_book(book)
print(f"Book {book.title} issued to {member.name}.")
else:
print("Book cannot be issued.")
def return_book(self, member_id, book_id):
member = next((m for m in self.members if m.member_id == member_id), None)
book = next((b for b in self.books if b.book_id == book_id and not b.is_available), None)
if member and book and book in member.borrowed_books:
member.return_book(book)
print(f"Book {book.title} returned by {member.name}.")
else:
print("Book cannot be returned.")
def main():
library = Library()
while True:
print("\n1. Add Book\n2. Register Member\n3. Issue Book\n4. Return Book\n5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
book_id = int(input("Enter Book ID: "))
title = input("Enter Book Title: ")
author = input("Enter Book Author: ")
library.add_book(Book(book_id, title, author))
elif choice == 2:
member_id = int(input("Enter Member ID: "))
name = input("Enter Member Name: ")
library.register_member(Member(member_id, name))
elif choice == 3:
member_id = int(input("Enter Member ID: "))
book_id = int(input("Enter Book ID to issue: "))
library.issue_book(member_id, book_id)
elif choice == 4:
member_id = int(input("Enter Member ID: "))
book_id = int(input("Enter Book ID to return: "))
library.return_book(member_id, book_id)
elif choice == 5:
print("Exiting...")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
Output:
1. Add Book
2. Register Member
3. Issue Book
4. Return Book
5. Exit
Enter your choice: 1
Enter Book ID: 1
Enter Book Title: Learn Python
Enter Book Author: John
1. Add Book
2. Register Member
3. Issue Book
4. Return Book
5. Exit
Enter your choice: 2
Enter Member ID: 1
Enter Member Name: Tom
1. Add Book
2. Register Member
3. Issue Book
4. Return Book
5. Exit
Enter your choice: 3
Enter Member ID: 1
Enter Book ID to issue: 1
Book Learn Python issued to Tom.
1. Add Book
2. Register Member
3. Issue Book
4. Return Book
5. Exit
Enter your choice: 4
Enter Member ID: 1
Enter Book ID to return: 1
Book Learn Python returned by Tom.
1. Add Book
2. Register Member
3. Issue Book
4. Return Book
5. Exit
Enter your choice: 5
Exiting...
That's it! You now have a basic library management system built with Python. This system can be further enhanced by adding features like searching books/members, maintaining a record of issue dates, computing fines, and much more. Python, with its vast library ecosystem, offers endless possibilities to expand and refine this project.
Comments
Post a Comment