Loans, limits and fines
After this: Write borrow and return with a limit, a due day and a fine rule in its own class.
On this page · 5 sections · 3 checks
The catalogue from lesson 2 can find a book, but nobody can take one home yet. This lesson adds the three rules from the paragraph about borrowing: at most 3 books at a time, 14 days for each loan, and 5 rupees for each day late.
The plan
Run the diagram to see the whole library drawn class by class. This lesson builds Member, Loan and FinePolicy; the line of waiting members comes in lesson 4.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
One loan, worked by hand
Ana borrows Dune on day 1 and brings it back on day 20. Days are whole numbers counted from the day the library opened.
- Borrowing on day 1: Ana has no loans, which is under the limit of 3, so she may borrow. The copy
DU-1is available, so it is hers. - The library makes a
Loanthat tiesDU-1to Ana. Its due day is 1 + 14 = 15. - Returning on day 20: the copy is 20 − 15 = 5 days late.
- The fine is 5 days × 5 rupees = 25. On day 15 or earlier, the days late would be 0 or less, and the fine 0.
The rule in one line: fine = the larger of 0 and (day back − due day), times 5.
Where each rule lives
Each rule sits in the class that owns its data:
| Rule | Class | Why there |
|---|---|---|
| at most 3 loans | Member keeps MAX_LOANS and its list of loans | the limit is about one member |
| due 14 days later | Loan works out due when it is made | the due day belongs to one loan |
| 5 rupees a day late | FinePolicy | the rule may change; see below |
The fine rule gets a class of its own. The library is given a FinePolicy object when it is made, and calls its fine method on every return. To change the rule, you hand the library a different object; the library's code stays the same. A rule kept in its own object and handed in like this is the pattern from "Three patterns you will meet first".
The code
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, the block shows the Java version to read, and you can paste it into a Java tool on your computer when you want to run it for real.
The new parts, class by class:
Memberkeeps a name, a list of loans and the fines owed so far.Loankeeps the copy, the member and the due day, which it works out from the day it was made.FinePolicyhas one method,fine, holding the one-line rule.borrowchecks the limit first. If the member already has 3 loans, it prints why and returns at once, before it looks for a copy. Otherwise it takes the first available copy, marks it on loan, makes theLoan, and stores it in two places: the member's list and the library's map of loans by copy id.- return_copyreturnCopy finds the loan by the copy id, removes it from both places, asks the fine rule for the fine, adds it to the member's total, and puts the copy back on the shelf.
The output follows the worked example: Ana borrows three books, is stopped on the fourth, and pays 0 for a copy back on its due day and 25 for Dune. The last line prints her total fines and how many books she still has: 25 1.
Check yourself
Ana borrows Dune on day 1, so it is due on day 15. She brings it back on day 15 instead of day 20. What fine does she pay?
The classes are this lesson's library. Ben borrows three books, then asks for Clean Code again on day 3. Both copies of Clean Code are already with him. What does the last line print?
lib = Library(FinePolicy())lib.add_book(Book("CC", "Clean Code", "Robert Martin"), 2)lib.add_book(Book("DU", "Dune", "Frank Herbert"), 1)lib.add_book(Book("RF", "Refactoring", "Martin Fowler"), 1)ben = Member("Ben")lib.borrow(ben, "CC", 1)lib.borrow(ben, "CC", 2)lib.borrow(ben, "RF", 2)lib.borrow(ben, "CC", 3)Library lib = new Library(new FinePolicy());lib.addBook(new Book("CC", "Clean Code", "Robert Martin"), 2);lib.addBook(new Book("DU", "Dune", "Frank Herbert"), 1);lib.addBook(new Book("RF", "Refactoring", "Martin Fowler"), 1);Member ben = new Member("Ben");lib.borrow(ben, "CC", 1);lib.borrow(ben, "CC", 2);lib.borrow(ben, "RF", 2);lib.borrow(ben, "CC", 3);Complete the fine rule. A loan that comes back after its due day costs PER_DAY rupees for each day late; on time or early costs 0. The parameters are the loan and day_backdayBack, the day the copy came back.
class FinePolicy: PER_DAY = 5
def fine(self, loan, day_back): ____ # ← the fine for this loanclass FinePolicy { static final int PER_DAY = 5;
int fine(Loan loan, int dayBack) { ____ // ← the fine for this loan }}- Work a fine by hand: due day = day out + 14, days late = day back minus due day, fine = the larger of 0 and the days late, times 5.
- Check the loan limit first in borrow, and return at once when it is reached.
- Keep each rule in the class that owns its data: the limit with the member, the due day with the loan.
- Keep a rule that may change in its own class handed to the library (Strategy), so a new rule is a new class.