How do you handle fines in the library management system?
Fast recall
Quick revision
Core answer
Can you handle temporal calculations and state management for financial transactions, showing understanding of date arithmetic and business rules
Freshers often calculate fine as a simple subtraction without handling edge cases like partial days, weekends, or grace periods, or fail to block borrowing for outstanding fines
Mention grace period (first day free), weekend/holiday exclusion from fine calculation, or progressive fine rates (higher after 30 days overdue)
Interview-ready guide
Interview answer
Fines are calculated when a book is returned. I compare the return date with the due date, and if the book is overdue, the fine is the number of overdue days times a daily rate. I'd compute the days with ChronoUnit.DAYS.between, and I'd use BigDecimal for the money, never double, because floating point rounding causes real problems with currency.
The part that makes this more than arithmetic is that the rate isn't fixed — it's a policy. A standard member, a premium member, and a student might all have different daily rates, and the library might run a fine-free week. So I'd wrap the rate in a FineStrategy interface with implementations like StandardFine and PremiumFine. The return service takes whichever strategy applies to that member and asks it for the rate. Adding a new tier is then a new strategy class, not a change to the return logic.
The other half is the member side. The Member entity tracks an outstanding fine balance. When a fine is incurred, it's added to that balance, and crucially the member's canBorrow method checks that the balance is clear before allowing new loans. That makes 'you can't borrow until you've paid your fines' an invariant the design enforces, rather than a rule someone has to remember to apply. For the interview scope I'd compute fines on return or on demand rather than building a nightly batch job, and mention the batch approach only if asked about scale.
How do you handle fines in the library management system
Direct Answer
Calculate fine during book return by comparing return date to due date. If overdue, fine equals overdue days multiplied by daily rate. Use a FineCalculator strategy to encapsulate rate logic (standard, premium members might have different rates). Member entity tracks outstanding fine balance that must be cleared before new borrowing is allowed.
Deep Dive & Architecture
The loan supplies the dates
A Loan records its due date and actual return date. A FinePolicy receives those values and the member type, calculates overdue days, applies any grace period or maximum cap, and returns a money value. Keeping the policy outside Member prevents borrowing state from becoming mixed with pricing rules.
A small calculation makes the rule visible
Suppose a copy was due on 10 June and returned on 13 June. With no grace period and a daily rate of ₹5, the overdue period is three days and the fine is ₹15. A return on or before 10 June produces zero. Boundary tests should cover the due date itself, the first overdue day, a grace period, and a capped fine.
The balance is a ledger, not a loose number
The system should record why each charge or payment changed the member's balance. BigDecimal is appropriate for money, and an injected clock makes date tests repeatable. Borrowing rules can consult the outstanding balance, but paying a fine should create a payment entry instead of silently replacing the total.
Code Example
interface FineStrategy { BigDecimal dailyRate(); }
class StandardFine implements FineStrategy { public BigDecimal dailyRate() { return new BigDecimal("5"); } }
class PremiumFine implements FineStrategy { public BigDecimal dailyRate() { return new BigDecimal("2"); } }
class ReturnService {
BigDecimal returnBook(BookLending loan, LocalDate returnDate, FineStrategy strategy) {
long overdue = ChronoUnit.DAYS.between(loan.due(), returnDate);
BigDecimal fine = BigDecimal.ZERO;
if (overdue > 0) {
fine = strategy.dailyRate().multiply(BigDecimal.valueOf(overdue));
loan.member().addFine(fine); // updates outstanding balance
}
loan.item().markAvailable();
return fine;
}
}
Rate is pluggable per member tier; BigDecimal keeps money exact.
Common Mistakes & Pitfalls
- Hard-coding one rate — different tiers need different rates; use a
FineStrategy. doublefor money — rounding errors. UseBigDecimal.- Computing fines on a schedule — compute on return (or on demand), not via a nightly job for the interview scope.
- Letting members borrow with unpaid fines —
canBorrow()must check the outstanding balance.
Check your understanding
Answer these without looking back at the article.
- Design a library management system. What are the key classes?
- How do you handle the book reservation system in LLD?
- How do you model search functionality in the library system?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.