Stop 05 · Inheritance, Polymorphism & InterfacesProjects · 1/1

Library Management System

Part of the Java Roadmap

In short

This project pulls the whole phase together: an abstract base for items, interfaces for rules, composition inside the Library class and a strategy for fines. It's also a classic interview machine-coding question.

A console app for a college library that manages books, members, issues and returns with fines. It practises inheritance, interfaces, polymorphism, composition and a design pattern or two.

The jaadu sits at a college library issue desk stamping a due date into a book while a student waits and shelves of books rise behind it.

Think of it like this

Your college library desk

Think of the issue desk at your college library. Books go out for 14 days, magazines for 7. The librarian records who has what, and charges a fine for each late day. Your program plays that librarian: a catalogue, a loans register and a fine rule, each a separate piece working together.

Watch it happen

Returning a book three days late

Member
Library
FinePolicy

returnItem("B101")

1/4Rahul returns his book. Library finds his loan, removes it and counts 3 late days with java.time.

The big ideas

What you need to know

Abstract item type

LibraryItem holds id and title. Book and Magazine extend it and each decides loanDays().

Fine rule as an interface

FinePolicy is a Strategy. Library receives it through the constructor, following dependency inversion.

Library composes the parts

It has a catalogue map and an active loans map. It is not a subclass of anything.

Loan as a record

A Loan is pure data (item, member, due date), so a record keeps it short and immutable.

How it works

Build it in this order

  1. 1

    Model items

    Write abstract LibraryItem with Book and Magazine subclasses. Test loanDays() on each.

  2. 2

    Add members and loans

    Create a Member class and a Loan record holding item, member id and due date.

  3. 3

    Write the Library

    Add a catalogue, issue() and returnItem(). Throw an exception for unavailable items.

  4. 4

    Plug in fines

    Create the FinePolicy interface and a PerDayFine class, and pass it to Library.

  5. 5

    Console menu

    Use Scanner for a menu: add item, issue, return, list available. Then push it to GitHub with a README.

See it in code

Walk through the code

java
1import java.time.LocalDate;2import java.time.temporal.ChronoUnit;3import java.util.HashMap;4import java.util.Map;5 6abstract class LibraryItem {7    private final String id;8    private final String title;9 10    protected LibraryItem(String id, String title) {11        this.id = id;12        this.title = title;13    }14 15    String id() { return id; }16    String title() { return title; }17    abstract int loanDays();18}19 20class Book extends LibraryItem {21    Book(String id, String title) { super(id, title); }22    @Override int loanDays() { return 14; }23}24 25class Magazine extends LibraryItem {26    Magazine(String id, String title) { super(id, title); }27    @Override int loanDays() { return 7; }28}29 30interface FinePolicy {31    long fineFor(long daysLate);32}33 34record Loan(LibraryItem item, String memberId, LocalDate dueDate) {}35 36class Library {37    private final Map<String, LibraryItem> catalogue = new HashMap<>();38    private final Map<String, Loan> activeLoans = new HashMap<>();39    private final FinePolicy finePolicy;40 41    Library(FinePolicy finePolicy) {42        this.finePolicy = finePolicy;43    }44 45    void add(LibraryItem item) {46        catalogue.put(item.id(), item);47    }48 49    void issue(String itemId, String memberId, LocalDate today) {50        LibraryItem item = catalogue.get(itemId);51        if (item == null || activeLoans.containsKey(itemId)) {52            throw new IllegalStateException("Not available: " + itemId);53        }54        activeLoans.put(itemId, new Loan(item, memberId, today.plusDays(item.loanDays())));55    }56 57    long returnItem(String itemId, LocalDate today) {58        Loan loan = activeLoans.remove(itemId);59        if (loan == null) {60            throw new IllegalStateException("Not on loan: " + itemId);61        }62        long daysLate = ChronoUnit.DAYS.between(loan.dueDate(), today);63        return daysLate > 0 ? finePolicy.fineFor(daysLate) : 0;64    }65}

1/5The abstract base. Each item type must say how long it can be borrowed.

Library lib = new Library(days -> days * 10); A fine policy can be a class or, once you learn lambdas in the next phases, a one-line lambda.

Save yourself the pain

Common mistakes

Oops

Making Library extend HashMap so it can store books.

Do this instead

The library has a catalogue. Keep the map as a private field and expose only add, issue and return.

Oops

Hard-coding the fine as daysLate * 10 inside returnItem().

Do this instead

Put it behind FinePolicy so a new rule, like a student waiver, is just a new class.

Oops

Letting the same book be issued twice.

Do this instead

Check activeLoans before issuing and throw a clear exception if it's already out.

Your mission

Ship the library

~120 min

Quick check

Why is FinePolicy an interface passed into Library's constructor instead of a calculation inside returnItem()?

Go deeper

  1. 1Model Book, Member and Loan classes, with StudentMember and FacultyMember sharing an abstract parent.
  2. 2Use an interface for fine calculation so each member type can have its own rule.
  3. 3Write down which SOLID principles your design follows and where it could improve.

Also part of these roadmaps

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge