Library Management System

pythonCopy codeclass 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()

Step 2: Running the Library Management System

  1. Open your terminal (or command prompt).
  2. Navigate to the directory where you saved library_management.py.
  3. Run the script using the command:bashCopy codepython library_management.py

How It Works

  • Book Class: Represents a book with attributes for title, author, ISBN, and whether it is borrowed.
  • Library Class: Manages a list of books, including adding books, viewing available books, borrowing, and returning books.
  • User Interaction: The command-line interface allows users to choose actions like adding a book, viewing the library, borrowing a book, or returning a book.

Example Usage

  1. Add Book: Choose option 1 and enter the book title, author, and ISBN to add a new book.
  2. View Books: Choose option 2 to see all available books in the library.
  3. Borrow Book: Choose option 3 and enter the ISBN of the book you want to borrow.
  4. Return Book: Choose option 4 and enter the ISBN of the book you want to return.
  5. Exit: Choose option 5 to exit the system.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *