class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
self.is_borrowed = False
def __str__(self):
return f"{self.title} by {self.author} (ISBN: {self.isbn}) - {'Borrowed' if self.is_borrowed else 'Available'}"
class Library:
def __init__(self):
self.books = []
def add_book(self, title, author, isbn):
book = Book(title, author, isbn)
self.books.append(book)
print(f"Added book: {book}")
def view_books(self):
if not self.books:
print("No books available in the library.")
return
print("\nAvailable Books:")
for book in self.books:
print(book)
def borrow_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
if book.is_borrowed:
print("Sorry, this book is already borrowed.")
else:
book.is_borrowed = True
print(f"You have borrowed: {book}")
return
print("Book not found.")
def return_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
if not book.is_borrowed:
print("This book wasn't borrowed.")
else:
book.is_borrowed = False
print(f"You have returned: {book}")
return
print("Book not found.")
def main():
library = Library()
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. View Books")
print("3. Borrow Book")
print("4. Return Book")
print("5. Exit")
choice = input("Choose an option (1-5): ")
if choice == '1':
title = input("Enter the book title: ")
author = input("Enter the author's name: ")
isbn = input("Enter the ISBN number: ")
library.add_book(title, author, isbn)
elif choice == '2':
library.view_books()
elif choice == '3':
isbn = input("Enter the ISBN of the book to borrow: ")
library.borrow_book(isbn)
elif choice == '4':
isbn = input("Enter the ISBN of the book to return: ")
library.return_book(isbn)
elif choice == '5':
print("Exiting the Library Management System.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Leave a Reply