Creating a Library Management System in Python is a great way to understand object-oriented programming concepts. This step-by-step tutorial will guide you through building a simple Library Management System using Python, focusing on adding, updating, deleting, listing, searching for books, and managing their checkout status.
Step 1: Define the Book Class
Start by defining a Book class to represent each book in the library. This class will include attributes for the book's ID, title, author, and checkout status.
3. Code Program
class Book:
def __init__(self, book_id, title, author, is_checked_out=False):
self.book_id = book_id
self.title = title
self.author = author
self.is_checked_out = is_checked_out
def __str__(self):
return f"{self.book_id}: {self.title} by {self.author}, {'Checked out' if self.is_checked_out else 'Available'}"
Step 2: Create the LibraryManager Class
The LibraryManager class manages the book collection. It includes methods for adding, updating, deleting, listing, searching for books, and managing their checkout status.
class LibraryManager:
def __init__(self):
self.books = {}
def add_book(self, book):
if book.book_id in self.books:
print("This book ID already exists.")
else:
self.books[book.book_id] = book
print("Book added successfully.")
def update_book(self, book_id, title=None, author=None):
if book_id not in self.books:
print("Book not found.")
else:
if title:
self.books[book_id].title = title
if author:
self.books[book_id].author = author
print("Book updated successfully.")
def delete_book(self, book_id):
if book_id in self.books:
del self.books[book_id]
print("Book deleted successfully.")
else:
print("Book not found.")
def list_books(self):
for book in self.books.values():
print(book)
def search_books(self, query):
found_books = [book for book in self.books.values() if query.lower() in book.title.lower() or query.lower() in book.author.lower()]
if found_books:
for book in found_books:
print(book)
else:
print("No books found matching your query.")
def check_out_book(self, book_id):
if book_id not in self.books:
print("Book not found.")
elif self.books[book_id].is_checked_out:
print("Book is already checked out.")
else:
self.books[book_id].is_checked_out = True
print("Book checked out successfully.")
def check_in_book(self, book_id):
if book_id not in self.books:
print("Book not found.")
elif not self.books[book_id].is_checked_out:
print("Book is not checked out.")
else:
self.books[book_id].is_checked_out = False
print("Book checked in successfully.")
Step 3: Implement User Interface
Create a simple console-based interface for interacting with the LibraryManager. This will allow the user to perform all the actions described above.
def main():
library_manager = LibraryManager()
while True:
print("\nLibrary Management System")
print("1. Add a new book")
print("2. Update a book")
print("3. Delete a book")
print("4. List all books")
print("5. Search for books")
print("6. Check out a book")
print("7. Check in a book")
print("8. Exit")
choice = input("Enter your choice: ")
if choice == '1':
book_id = input("Enter book ID: ")
title = input("Enter book title: ")
author = input("Enter book author: ")
library_manager.add_book(Book(book_id, title, author))
elif choice == '2':
book_id = input("Enter book ID: ")
title = input("Enter new title (leave blank to skip): ")
author = input("Enter new author (leave blank to skip): ")
library_manager.update_book(book_id, title, author)
elif choice == '3':
book_id = input("Enter book ID to delete: ")
library_manager.delete_book(book_id)
elif choice == '4':
library_manager.list_books()
elif choice == '5':
query = input("Enter search query (title or author): ")
library_manager.search_books(query)
elif choice == '6':
book_id = input("Enter book ID to check out: ")
library_manager.check_out_book(book_id)
elif choice == '7':
book_id = input("Enter book ID to check in: ")
library_manager.check_in_book(book_id)
elif choice == '8':
print("Exiting system.")
break
else:
print("Invalid choice. Please try again.")
Step 4: Adding the Entry Point Check
After defining all the classes and the main() function as outlined in the previous steps, you would conclude your script with:
if __name__ == "__main__":
main()
This line of code should be added at the end of your Python script. It calls the main() function, which contains the logic for running the Library Management System, including the user interaction loop for performing various actions (adding, updating, deleting books, etc.).
Step 5: Running the Program
To run the Library Management System:- Ensure you have Python installed on your computer.
- Copy the complete code (including the classes, main() function, and the entry point check) into a file named something like library_management_system.py.
- Open a terminal or command prompt.
- Navigate to the directory where you saved library_management_system.py.
python library_management_system.py
- Interact with the Library Management System through the console as prompted.
Complete Code
Combine all the snippets above into a single Python file. This code provides a basic Library Management System that supports adding, updating, deleting, listing, and searching for books, as well as checking them in and out.
class Book:
def __init__(self, book_id, title, author, is_checked_out=False):
self.book_id = book_id
self.title = title
self.author = author
self.is_checked_out = is_checked_out
def __str__(self):
return f"{self.book_id}: {self.title} by {self.author}, {'Checked out' if self.is_checked_out else 'Available'}"
class LibraryManager:
def __init__(self):
self.books = {}
def add_book(self, book):
if book.book_id in self.books:
print("This book ID already exists.")
else:
self.books[book.book_id] = book
print("Book added successfully.")
def update_book(self, book_id, title=None, author=None):
if book_id not in self.books:
print("Book not found.")
else:
if title:
self.books[book_id].title = title
if author:
self.books[book_id].author = author
print("Book updated successfully.")
def delete_book(self, book_id):
if book_id in self.books:
del self.books[book_id]
print("Book deleted successfully.")
else:
print("Book not found.")
def list_books(self):
for book in self.books.values():
print(book)
def search_books(self, query):
found_books = [book for book in self.books.values() if query.lower() in book.title.lower() or query.lower() in book.author.lower()]
if found_books:
for book in found_books:
print(book)
else:
print("No books found matching your query.")
def check_out_book(self, book_id):
if book_id not in self.books:
print("Book not found.")
elif self.books[book_id].is_checked_out:
print("Book is already checked out.")
else:
self.books[book_id].is_checked_out = True
print("Book checked out successfully.")
def check_in_book(self, book_id):
if book_id not in self.books:
print("Book not found.")
elif not self.books[book_id].is_checked_out:
print("Book is not checked out.")
else:
self.books[book_id].is_checked_out = False
print("Book checked in successfully.")
def main():
library_manager = LibraryManager()
while True:
print("\nLibrary Management System")
print("1. Add a new book")
print("2. Update a book")
print("3. Delete a book")
print("4. List all books")
print("5. Search for books")
print("6. Check out a book")
print("7. Check in a book")
print("8. Exit")
choice = input("Enter your choice: ")
if choice == '1':
book_id = input("Enter book ID: ")
title = input("Enter book title: ")
author = input("Enter book author: ")
library_manager.add_book(Book(book_id, title, author))
elif choice == '2':
book_id = input("Enter book ID: ")
title = input("Enter new title (leave blank to skip): ")
author = input("Enter new author (leave blank to skip): ")
library_manager.update_book(book_id, title, author)
elif choice == '3':
book_id = input("Enter book ID to delete: ")
library_manager.delete_book(book_id)
elif choice == '4':
library_manager.list_books()
elif choice == '5':
query = input("Enter search query (title or author): ")
library_manager.search_books(query)
elif choice == '6':
book_id = input("Enter book ID to check out: ")
library_manager.check_out_book(book_id)
elif choice == '7':
book_id = input("Enter book ID to check in: ")
library_manager.check_in_book(book_id)
elif choice == '8':
print("Exiting system.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Output
Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 1 Enter book ID: 111 Enter book title: Python for Beginners Enter book author: Ramesh Book added successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 1 Enter book ID: 112 Enter book title: Python for Developers Enter book author: Ramesh Book added successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 2 Enter book ID: 112 Enter new title (leave blank to skip): Python for Developers - Guide Enter new author (leave blank to skip): Book updated successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 4 111: Python for Beginners by Ramesh, Available 112: Python for Developers - Guide by Ramesh, Available Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 5 Enter search query (title or author): Python 111: Python for Beginners by Ramesh, Available 112: Python for Developers - Guide by Ramesh, Available Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 6 Enter book ID to check out: 111 Book checked out successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 7 Enter book ID to check in: 111 Book checked in successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 3 Enter book ID to delete: 112 Book deleted successfully. Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 4 111: Python for Beginners by Ramesh, Available Library Management System 1. Add a new book 2. Update a book 3. Delete a book 4. List all books 5. Search for books 6. Check out a book 7. Check in a book 8. Exit Enter your choice: 8 Exiting system. === Code Execution Successful ===
Comments
Post a Comment
Leave Comment