Library Loans and Due Days
Implement a class Library that lends books for a fixed number of days and keeps track of late returns. Days are whole numbers, and day never decreases from one call to the next. Write these methods:
Library(period)starts with an empty shelf. Every loan is dueperioddays after the day it starts.add_copies(title, n)putsnmore copies oftitleon the shelf. Returns how many copies oftitleare on the shelf now.lend(member, title, day)lends a copy oftitlefrom the shelf tomemberonday. It works only if a copy is on the shelf andmemberdoes not already havetitleon loan; then it returns the due day,day + period. Otherwise nothing changes and it returns-1.give_back(member, title, day)takes backmember's copy oftitleondayand puts it on the shelf. Returns how many days late it is:dayminus the due day, or0if it comes back on or before the due day. Returns-1ifmemberdoes not havetitleon loan.list_overdue(day)returns the names of the members who hold at least one book whose due day is beforeday. The names are sorted alphabetically and each appears once; the list is empty if nobody is overdue.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Library(7)
add_copies("dune", 1) returns 1
lend("ann", "dune", 0) returns 7
lend("bob", "dune", 1) returns -1
list_overdue(7) returns []
list_overdue(8) returns ["ann"]
give_back("ann", "dune", 10) returns 3
lend("bob", "dune", 10) returns 17
give_back("bob", "dune", 12) returns 0
give_back("bob", "dune", 12) returns -1
ann borrows the only copy on day 0, due on day 7, so bob cannot borrow it. On day 7 the book is due but not yet overdue; on day 8 it is. It comes back on day 10, three days late. bob then borrows it and returns it early, and a second return finds no loan.
Constraints
1 <= period <= 14and1 <= n <= 30 <= day <= 100, anddaynever decreases from one call to the nextmemberandtitleare1to8lowercase letters.- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.