Late Fees Across the Library

Python for AIHard+10 XP+10

Late Fees Across the Library

A library lends books, magazines and DVDs. Design the family so that nothing is written twice.

LibraryItem(title, year) (the parent):

  • describe() returns "<title> (<year>)", like "Dune (1965)".
  • loan_days() returns 21.
  • daily_fine() returns 0.25.
  • late_fee(days_kept) returns the fine for keeping the item days_kept days. Days past loan_days() are overdue. The fee is overdue days × daily_fine(), rounded to 2 decimal places. If nothing is overdue, it's 0.

Children (each lets the parent set up title and year):

ClassMade withdescribe() addsloan_days()daily_fine()
BookBook(title, year, author)" by <author>"same as parentsame as parent
MagazineMagazine(title, year, issue)", issue <issue>"70.1
DVDDVD(title, year, minutes)", <minutes> min"31.0

describe() in each child adds its part after the parent's text. late_fee should only be written once, in the parent.

Finally, write fees_due(items, days_kept): the total late fee if every item in the list is kept for days_kept days, rounded to 2 decimal places. An empty list gives 0.

Examples

Book("Dune", 1965, "Herbert").describe()
returns 'Dune (1965) by Herbert'
items = [Book("Dune", 1965, "Herbert"), Magazine("New Scientist", 2024, 412)]
days_kept = 10
returns 0.3

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run