Borrowing System
Part of the Object Oriented Programming section of Coddy's Python journey. Lesson 57 of 64.
Challenge
MediumIn this challenge, you'll add borrowing functionality to the library management system.
Edit library.py and implement the borrow_book method, following the TODO comments.
The other files are provided for you and are locked:
book.py— theBookclass you built in the previous challengeuser.py— theUserclass, holdinguser_id,nameandbooks_borroweddriver.py— runs the test scenarios against your implementation
borrow_book(book_isbn, user_id) must return exactly one of: "Book not found", "User not found", "Book is not available", or "Book borrowed successfully".
Try it yourself
from book import Book
from user import User
from library import Library
# Comprehensive test case handler
test_case = input()
# Basic functionality tests
if test_case == "borrow_success":
library = Library("Community Library")
# Add books and users
book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
library.add_book(book)
user = User("U001", "Alice")
library.add_user(user)
# Borrow book
result = library.borrow_book("123456", "U001")
print(result)
print(f"Book available: {book.available}")
print(f"Books borrowed by user: {len(user.books_borrowed)}")
elif test_case == "book_not_found":
library = Library("Community Library")
user = User("U001", "Alice")
library.add_user(user)
result = library.borrow_book("nonexistent", "U001")
print(result)
elif test_case == "user_not_found":
library = Library("Community Library")
book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
library.add_book(book)
result = library.borrow_book("123456", "nonexistent")
print(result)
elif test_case == "book_not_available":
library = Library("Community Library")
# Add books and users
book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
book.available = False # Book is already borrowed
library.add_book(book)
user = User("U001", "Alice")
library.add_user(user)
# Try to borrow unavailable book
result = library.borrow_book("123456", "U001")
print(result)
# Edge cases and additional tests
elif test_case == "multiple_books":
library = Library("Community Library")
user = User("U001", "Alice")
library.add_user(user)
# Create multiple books
books = [
Book("Book 1", "Author 1", "111"),
Book("Book 2", "Author 2", "222"),
Book("Book 3", "Author 3", "333")
]
for book in books:
library.add_book(book)
# Borrow multiple books
results = []
for book in books:
results.append(library.borrow_book(book.isbn, "U001"))
for result in results:
print(result)
print(f"Total books borrowed: {len(user.books_borrowed)}")
elif test_case == "multiple_users":
library = Library("Community Library")
book = Book("Popular Book", "Famous Author", "999")
library.add_book(book)
# Create multiple users
users = [
User("U001", "Alice"),
User("U002", "Bob"),
User("U003", "Charlie")
]
for user in users:
library.add_user(user)
# Try to borrow the same book with different users
result1 = library.borrow_book("999", "U001")
result2 = library.borrow_book("999", "U002") # Should fail
print(result1)
print(result2)
print(f"Book's borrower: {book.borrower.name}")
elif test_case == "return_book":
library = Library("Community Library")
book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
library.add_book(book)
user = User("U001", "Alice")
library.add_user(user)
# Borrow the book
borrow_result = library.borrow_book("123456", "U001")
print(borrow_result)
# Verify book status
print(f"Book available: {book.available}")
print(f"Book borrower: {book.borrower.name}")
print(f"User's borrowed books: {len(user.books_borrowed)}")
print(f"First borrowed book title: {user.books_borrowed[0].title}")
elif test_case == "empty_library":
library = Library("Empty Library")
result = library.borrow_book("any_isbn", "any_user_id")
print(result)
elif test_case == "borrower_reference":
library = Library("Community Library")
book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
library.add_book(book)
user = User("U001", "Alice")
library.add_user(user)
# Borrow the book
library.borrow_book("123456", "U001")
# Verify references
print(f"Book's borrower name: {book.borrower.name}")
print(f"First book in user's borrowed list: {user.books_borrowed[0].title}")All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User ClassesBorrowing SystemSearch FunctionalityAdmin InterfaceTesting and IntegrationPractice on your own: Online Python compiler