Library Management System
Library Management System
books.
class Book:
def __init__(self, book_id, title, author, year, status="Available"):
self.book_id = book_id
self.title = title
self.author = author
self.year = year
self.status = status # Available or Issued
def issue_book(self):
if self.status == "Available":
self.status = "Issued"
print(f"Book '{self.title}' has been issued.")
else:
print(f"Book '{self.title}' is already issued.")
def return_book(self):
if self.status == "Issued":
self.status = "Available"
print(f"Book '{self.title}' has been returned.")
else:
print(f"Book '{self.title}' is not issued.")
def get_book_info(self):
return f"ID: {self.book_id}, Title: {self.title}, Author:
{self.author}, Year: {self.year}, Status: {self.status}"
class Library:
def __init__(self):
self.books = []
def show_books(self):
if not self.books:
print("No books available in the library.")
else:
print("\n--- Available Books ---")
for book in self.books:
if book.status == "Available":
print(book.get_book_info())
# Menu-driven interface
def library_management():
library = Library()
while True:
print("\n--- Library Management System ---")
print("1. Show available books")
print("2. Issue a book")
print("3. Return a book")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
library.show_books()
elif choice == '2':
book_id = int(input("Enter the book ID to issue: "))
library.issue_book(book_id)
elif choice == '3':
book_id = int(input("Enter the book ID to return: "))
library.return_book(book_id)
elif choice == '4':
print("Thank you for using the Library Management System.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
library_management()
Explanation:
Classes:
1. Book Class:
o Represents a book with attributes like book_id, title, author, year, and
status (whether it is "Available" or "Issued").
o It has methods issue_book() to issue the book and return_book() to return
it.
o get_book_info() returns the details of the book.
2. Library Class:
o Represents a library which contains a collection of books.
o It has methods to add books (add_book()), show available books
(show_books()), issue books (issue_book()), and return books
(return_book()).
Functionality:
Example Output: