Building a Library Management System [10%]¶

Suggestion: Complete IU 3.6.9.1 before attempting this question.

You are tasked with building a library management system using Python classes and objects. The system should allow for the management of books, library members, and borrowing transactions. Here are the requirements:

  1. Create a class called Book with the following attributes and methods: [2%]
  • Attributes: title, author, isbn, copies_available, total_copies
  • Method: display_info() that displays information about the book.
  1. Create a class called Member with the following attributes and methods: [3%]
  • Attributes: name, member_id, books_borrowed (a list of books borrowed)
  • Method: borrow_book(book) that allows a member to borrow a book.
  • Method: return_book(book) that allows a member to return a book.
  1. Create a class called Library with the following attributes and methods: [5%]
  • Attributes: name, books (a list of books), members (a list of members)
  • Method: add_book(book) to add a book to the library.
  • Method: add_member(member) to add a member to the library.
  • Method: get_available_books() to get a list of books available for borrowing.
  • Method: get_borrowed_books() to get a list of borrowed books.
  • Method: get_member_books(member) to get a list of books borrowed by a member.

Your goal is to create the above class hierarchy and implement the methods to demonstrate interactions between books, members, and the library.

Test your classes by creating instances, adding books and members, and simulating borrowing and returning transactions.


This code defines three core classes for a simple Library Management System:

  • Book: Represents a book with details such as title, author, ISBN, available copies, and total copies. Includes a method to display book information.

  • Member: Represents a library member with a name, ID, and a list of borrowed books. Provides methods to borrow and return books, with appropriate messages.

  • Library: Represents a library with a name, and collections of books and members. Includes methods to add books and members, list available and borrowed books, and display books borrowed by a specific member.

These classes form the foundation for simulating library operations such as lending and inventory tracking.

In [8]:
class Book:
    def __init__(self, title, author, isbn, copies_available, total_copies):
        self.title = title
        self.author = author
        self.isbn = isbn
        self.copies_available = copies_available
        self.total_copies = total_copies

    def display_info(self):
        return f"{self.title} by {self.author} (ISBN: {self.isbn})"


class Member:
    def __init__(self, name, member_id, books_borrowed):
        self.name = name
        self.member_id = member_id
        self.books_borrowed = books_borrowed

    def borrow_book(self, book):
        self.books_borrowed.append(book)
        print(f"{self.name} has borrowed: {book.display_info()}")

    def return_book(self, book):
        if book in self.books_borrowed:
            self.books_borrowed.remove(book)
            print(f"{self.name} has returned: {book.display_info()}")
        else:
            print(f"{self.name} has not borrowed: {book.display_info()}")


class Library:
    def __init__(self, name, books, members):
        self.name = name
        self.books = books
        self.members = members

    def add_book(self, book):
        self.books.append(book)
        print(f"Added '{book.title}' to {self.name}.")

    def add_member(self, member):
        self.members.append(member)
        print(f"Member '{member.name}' has been added to {self.name}.")

    def get_available_books(self):
        print(f"\n📚 Available Books in {self.name}:")
        for book in self.books:
            print(f"- {book.title}: {book.copies_available} copy/copies available")

    def get_borrowed_books(self):
        print(f"\n📦 Borrowed Books in {self.name}:")
        for book in self.books:
            borrowed_copies = book.total_copies - book.copies_available
            print(f"- {book.title}: {borrowed_copies} copy/copies borrowed")

    def get_member_books(self, member):
        print(f"\n👤 Books Borrowed by {member.name}:")
        if not member.books_borrowed:
            print("No books currently borrowed.")
        else:
            for book in member.books_borrowed:
                print(f"- {book.display_info()}")

This code sets up sample data to test the Book, Member, and Library classes. It creates a list of book objects with predefined details, a list of members with unique IDs, and initializes a Library instance named "National Library" containing these books and members. The setup allows you to test core functionalities such as borrowing, returning, and tracking books and members.

In [9]:
# Sample Book Data
book1 = Book("1984", "George Orwell", "9780451524935", 3, 5)
book2 = Book("To Kill a Mockingbird", "Harper Lee", "9780061120084", 2, 4)
book3 = Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565", 1, 2)
book4 = Book("Pride and Prejudice", "Jane Austen", "9780141439518", 0, 3)
book5 = Book("The Catcher in the Rye", "J.D. Salinger", "9780316769488", 4, 4)

books = [book1, book2, book3, book4, book5]

# Sample Member Data
member1 = Member("Alice Tan", "M001", [])
member2 = Member("Benjamin Lee", "M002", [])
member3 = Member("Clara Lim", "M003", [])

members = [member1, member2, member3]

# Create the Library
library = Library("National Library", books, members)

This code tests the functionality of a basic Library Management System by simulating book borrowing and returning actions. It checks the availability of books, updates inventory accordingly, handles edge cases (like unavailable books), and displays the borrowing status of individual members and the overall library.

In [10]:
# Display available books in the library
library.get_available_books()

# Alice borrows "1984"
member1.borrow_book(book1)
book1.copies_available -= 1  # update inventory manually
library.get_member_books(member1)

# Benjamin tries to borrow "Pride and Prejudice" (no available copies)
if book4.copies_available > 0:
    member2.borrow_book(book4)
    book4.copies_available -= 1
else:
    print(f"{book4.title} is currently unavailable for borrowing.")

# Clara borrows "The Great Gatsby"
member3.borrow_book(book3)
book3.copies_available -= 1
library.get_member_books(member3)

# Display borrowed books in the library
library.get_borrowed_books()

# Display updated available books
library.get_available_books()

# Alice returns "1984"
member1.return_book(book1)
book1.copies_available += 1
library.get_member_books(member1)

# Display final state of borrowed and available books
library.get_borrowed_books()
library.get_available_books()
📚 Available Books in National Library:
- 1984: 3 copy/copies available
- To Kill a Mockingbird: 2 copy/copies available
- The Great Gatsby: 1 copy/copies available
- Pride and Prejudice: 0 copy/copies available
- The Catcher in the Rye: 4 copy/copies available
Alice Tan has borrowed: 1984 by George Orwell (ISBN: 9780451524935)

👤 Books Borrowed by Alice Tan:
- 1984 by George Orwell (ISBN: 9780451524935)
Pride and Prejudice is currently unavailable for borrowing.
Clara Lim has borrowed: The Great Gatsby by F. Scott Fitzgerald (ISBN: 9780743273565)

👤 Books Borrowed by Clara Lim:
- The Great Gatsby by F. Scott Fitzgerald (ISBN: 9780743273565)

📦 Borrowed Books in National Library:
- 1984: 3 copy/copies borrowed
- To Kill a Mockingbird: 2 copy/copies borrowed
- The Great Gatsby: 2 copy/copies borrowed
- Pride and Prejudice: 3 copy/copies borrowed
- The Catcher in the Rye: 0 copy/copies borrowed

📚 Available Books in National Library:
- 1984: 2 copy/copies available
- To Kill a Mockingbird: 2 copy/copies available
- The Great Gatsby: 0 copy/copies available
- Pride and Prejudice: 0 copy/copies available
- The Catcher in the Rye: 4 copy/copies available
Alice Tan has returned: 1984 by George Orwell (ISBN: 9780451524935)

👤 Books Borrowed by Alice Tan:
No books currently borrowed.

📦 Borrowed Books in National Library:
- 1984: 2 copy/copies borrowed
- To Kill a Mockingbird: 2 copy/copies borrowed
- The Great Gatsby: 2 copy/copies borrowed
- Pride and Prejudice: 3 copy/copies borrowed
- The Catcher in the Rye: 0 copy/copies borrowed

📚 Available Books in National Library:
- 1984: 3 copy/copies available
- To Kill a Mockingbird: 2 copy/copies available
- The Great Gatsby: 0 copy/copies available
- Pride and Prejudice: 0 copy/copies available
- The Catcher in the Rye: 4 copy/copies available
In [ ]:
 

<< Back

Davina's Jupyter Notebooks © Davina Leong, 2025