Final challenge: extend the library
After this: Add a student member with its own limit and fine, predict the new lines and choose where each change goes.
This challenge is the last part of an interview round: a follow-up. The library now has students, who may borrow 5 books and pay 2 rupees for each day late, while everyone else keeps 3 books and 5 rupees. You will choose where the two new rules go, write the student fine rule, predict a student's run, say which object works out a fine, and add one more kind of member. Each step is scored on its own. Code is shown in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Place the change
Place the change
New rule: students may borrow 5 books, and pay 2 rupees for each day late. Other members keep 3 books and 5 rupees. Where do the two new rules go?
Write the student fine
Complete the student fine rule: 2 rupees for each day after the due day, and 0 when the copy is on time or early. It replaces the fine method of FinePolicy.
class StudentFinePolicy(FinePolicy): def fine(self, loan, day_back): ____ # ← 2 rupees for each day lateclass StudentFinePolicy extends FinePolicy { @Override int fine(Loan loan, int dayBack) { ____ // ← 2 rupees for each day late }}Predict the student run
Book, BookCopy, Loan and the library are as in lesson 3, except that the library asks the member for its limit and its fine rule. Sam, a student, borrows four books on day 1 and returns Dune on day 20. What do the last two lines print?
class FinePolicy: PER_DAY = 5
def fine(self, loan, day_back): return max(0, day_back - loan.due) * FinePolicy.PER_DAY
class StudentFinePolicy(FinePolicy): def fine(self, loan, day_back): return max(0, day_back - loan.due) * 2
class Member: def __init__(self, name): self.name = name self.loans = [] self.fines = 0
def limit(self): return 3
def fine_policy(self): return FinePolicy()
class StudentMember(Member): def limit(self): return 5
def fine_policy(self): return StudentFinePolicy()
lib = Library()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)sam = StudentMember("Sam")lib.borrow(sam, "CC", 1)lib.borrow(sam, "CC", 1)lib.borrow(sam, "RF", 1)lib.borrow(sam, "DU", 1)lib.return_copy("DU-1", 20)class FinePolicy { static final int PER_DAY = 5;
int fine(Loan loan, int dayBack) { return Math.max(0, dayBack - loan.due) * PER_DAY; }}
class StudentFinePolicy extends FinePolicy { @Override int fine(Loan loan, int dayBack) { return Math.max(0, dayBack - loan.due) * 2; }}
class Member { String name; List<Loan> loans = new ArrayList<>(); int fines = 0;
Member(String name) { this.name = name; }
int limit() { return 3; }
FinePolicy finePolicy() { return new FinePolicy(); }}
class StudentMember extends Member { StudentMember(String name) { super(name); }
@Override int limit() { return 5; }
@Override FinePolicy finePolicy() { return new StudentFinePolicy(); }}
// in main:Library lib = new Library();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 sam = new StudentMember("Sam");lib.borrow(sam, "CC", 1);lib.borrow(sam, "CC", 1);lib.borrow(sam, "RF", 1);lib.borrow(sam, "DU", 1);lib.returnCopy("DU-1", 20);Who works out the fine?
Sam returns DU-1 five days late. Which object does the arithmetic that gives 10?
One more kind
Next the library adds staff: they may borrow 10 books and pay no fines. With the student design in place, what do you add or change?
- Move a rule that depends on the kind of member into the member, so the library asks instead of checking.
- Write a second fine rule as a new class built on the first, with its own fine method.
- Predict a run with a new kind of member, and add a further kind without changing the library.