Book and User Classes
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 56 of 64.
Challenge
EasyIn this challenge, you'll implement the Book class for a library management system using a multi-file structure with comprehensive test coverage.
Edit book.py to implement the Book class following the TODO comments. The implementation should:
- Initialize with title, author, and ISBN parameters
- Track availability status and borrower information
- Implement proper string representation
Cheat sheet
The challenge focuses on implementing a Book class for a library management system. The class should handle book initialization, track availability status and borrower information, and provide proper string representation.
Key components to implement:
- Initialize with title, author, and ISBN parameters
- Track availability status and borrower information
- Implement proper string representation methods
Try it yourself
from book import Book
from library import Library
# Comprehensive test case handler
test_case = input()
if test_case == "create_book":
library = Library("Community Library")
title = "Title"
author = "Author"
isbn = "ISBN"
book = Book(title, author, isbn)
print(book)
elif test_case == "add_to_library":
library = Library("Community Library")
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
book2 = Book("To Kill a Mockingbird", "Harper Lee", "789012")
library.add_book(book1)
library.add_book(book2)
print(f"Library has {len(library.books)} books")
print(f"Book 1: {library.books[0]}")
print(f"Book 2: {library.books[1]}")
elif test_case == "book_availability":
book = Book("1984", "George Orwell", "345678")
print(f"Initial availability: {book.available}")
# Change availability
book.available = False
print(f"After change: {book.available}")
print(book) # Should show "Not Available"
elif test_case == "empty_values":
book = Book("", "", "")
print(book)
library = Library("")
library.add_book(book)
print(library)
elif test_case == "special_characters":
book = Book("Title with $#@!", "Author with &*^%", "ISBN-123!@#")
print(book)
elif test_case == "multiple_books":
library = Library("Public Library")
books = [
Book("Book 1", "Author 1", "111111"),
Book("Book 2", "Author 2", "222222"),
Book("Book 3", "Author 3", "333333"),
Book("Book 4", "Author 4", "444444"),
Book("Book 5", "Author 5", "555555")
]
for book in books:
library.add_book(book)
print(library)
for i, book in enumerate(library.books):
print(f"Book {i+1}: {book}")
elif test_case == "borrower_assignment":
book = Book("The Hobbit", "J.R.R. Tolkien", "987654")
print(f"Initial borrower: {book.borrower}")
book.borrower = "John Doe"
book.available = False
print(f"New borrower: {book.borrower}")
print(book)
elif test_case == "attribute_access":
book = Book("Pride and Prejudice", "Jane Austen", "246810")
print(f"Title: {book.title}")
print(f"Author: {book.author}")
print(f"ISBN: {book.isbn}")
print(f"Available: {book.available}")
print(f"Borrower: {book.borrower}")
# Modify attributes
book.title = "Updated Title"
book.author = "Updated Author"
book.isbn = "999999"
book.available = False
book.borrower = "Jane Smith"
print("\
After modifications:")
print(f"Title: {book.title}")
print(f"Author: {book.author}")
print(f"ISBN: {book.isbn}")
print(f"Available: {book.available}")
print(f"Borrower: {book.borrower}")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 Classes