Coding Challenge: Tracking Books Across All Libraries
On this page
The TaskCombine the branch-specific book lists with a class-wide counter.
This UML snapshot shows the target state: underlining marks the class-wide counter, while each labeled object stores its own collection.
The Task
Extend the Library from Instance and Class Methods:
- Store the count in a class attribute named
book_count. - Update it on every successful
add_bookcall. Duplicate titles represent separate copies. - Return it from a class method named
total_books(). - Start from zero on every fresh run. Removal and persistence are outside this exercise.
Add Hamlet and Macbeth to Central, and Faust to Neighborhood. Print each collection and the total.
Bonus: keep a registry of branches and implement library_summary(). Show each branch’s count and percentage, formatted to one decimal place. An empty system must not divide by zero.
Target output for your solution
Central Library Collection:
- Hamlet
- Macbeth
Neighborhood Library Collection:
- Faust
Total books in system: 3
Central: 2 books (66.7% of total)
Neighborhood: 1 book (33.3% of total)
Test an empty system, one branch and the two-branch example. Explain why the counter and the method need different names.
Managing a shared total also raises a design question: which operations should be allowed to change it? Encapsulation addresses that question.