Day 6 · Day 6: Object-Oriented Programming
Library Management System
Build an object-oriented Library Management System using classes for Book and Member, supporting borrowing and returning books, with inheritance to model different member types.
Requirements
1. Create a Book class with title, author, isbn, and is_available attributes.
2. Create a Member base class with name, member_id, and borrowed_books.
3. Create a PremiumMember subclass that inherits from Member and can borrow up to 5 books, while a regular Member can borrow up to 2.
4. Create a Library class that manages a collection of books and members.
5. Implement methods: add_book, borrow_book, return_book, list_available_books.
6. Borrowing should fail if the book is unavailable or the member has reached their limit.
7. Demonstrate polymorphism: both member types use the same borrow_book() method but enforce different limits.
2. Create a Member base class with name, member_id, and borrowed_books.
3. Create a PremiumMember subclass that inherits from Member and can borrow up to 5 books, while a regular Member can borrow up to 2.
4. Create a Library class that manages a collection of books and members.
5. Implement methods: add_book, borrow_book, return_book, list_available_books.
6. Borrowing should fail if the book is unavailable or the member has reached their limit.
7. Demonstrate polymorphism: both member types use the same borrow_book() method but enforce different limits.
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
self.is_available = True
def __str__(self):
return f"{self.title} by {self.author} ({self.isbn})"
class Member:
def __init__(self, name, member_id):
self.name = name
self.member_id = member_id
self.borrowed_books = []
self.max_books = 2
def borrow_book(self, book):
if not book.is_available:
print(f"'{book.title}' is not available.")
return False
if len(self.borrowed_books) >= self.max_books:
print(f"{self.name} has reached the borrowing limit ({self.max_books}).")
return False
book.is_available = False
self.borrowed_books.append(book)
print(f"{self.name} borrowed '{book.title}'")
return True
def return_book(self, book):
if book in self.borrowed_books:
book.is_available = True
self.borrowed_books.remove(book)
print(f"{self.name} returned '{book.title}'")
return True
print(f"{self.name} did not borrow '{book.title}'")
return False
class PremiumMember(Member):
def __init__(self, name, member_id):
super().__init__(name, member_id)
self.max_books = 5
class Library:
def __init__(self):
self.books = []
self.members = []
def add_book(self, book):
self.books.append(book)
def add_member(self, member):
self.members.append(member)
def find_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
return book
return None
def borrow_book(self, member, isbn):
book = self.find_book(isbn)
if not book:
print("Book not found.")
return
member.borrow_book(book) # polymorphism: works for Member or PremiumMember
def return_book(self, member, isbn):
book = self.find_book(isbn)
if book:
member.return_book(book)
def list_available_books(self):
available = [b for b in self.books if b.is_available]
if not available:
print("No books available.")
for b in available:
print(b)
if __name__ == "__main__":
library = Library()
library.add_book(Book("Python Basics", "J. Doe", "111"))
library.add_book(Book("Advanced OOP", "A. Smith", "222"))
alice = Member("Alice", "M001")
bob = PremiumMember("Bob", "M002")
library.add_member(alice)
library.add_member(bob)
library.list_available_books()
library.borrow_book(alice, "111")
library.borrow_book(bob, "222")
library.list_available_books()
library.return_book(alice, "111")
library.list_available_books()
50 XP on completion
Back to Day 6
Your Code
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to mark this project complete and earn XP.