computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

📚

🎓

⏱ 45 min

Free

Project: Library Manager

Design a mini library system with classes, a LinkedHashMap and exceptions.

🎯 What you will learn

  • Design classes for a real system
  • Combine collections and OOP
  • Handle errors in user actions

👋 Let’s get started!

Time to think like a software engineer 📚! You’ll design a library where books can be added, borrowed and returned — with clean classes and safe error handling.

🪜 Step by step

1. 🧩 Design

Two classes: Book (title, author, available) and Library (books, borrow, return).

2. 📗 Book class

Keep fields private with simple methods.

class Book {
    private final String title;
    private boolean available = true;
    Book(String title) { this.title = title; }
    String getTitle() { return title; }
    boolean isAvailable() { return available; }
    void setAvailable(boolean a) { available = a; }
}
3. 🏛️ Library class

Store books in a LinkedHashMap by title: quick lookup, and it remembers the order books were added.

class Library {
    private final LinkedHashMap<String, Book> books = new LinkedHashMap<>();
    void add(Book b) { books.put(b.getTitle(), b); }
}

🧪 Try it!

Read the code, guess what it prints, then press 👀 Show output.

Library in action

class Library {
    private final LinkedHashMap<String, Book> books = new LinkedHashMap<>();

    void add(Book b) { books.put(b.getTitle(), b); }

    void borrow(String title) {
        Book b = books.get(title);
        if (b == null) throw new IllegalArgumentException("No such book: " + title);
        if (!b.isAvailable()) throw new IllegalStateException(title + " is already borrowed");
        b.setAvailable(false);
        System.out.println("📗 Borrowed " + title);
    }
}

// main:
Library lib = new Library();
lib.add(new Book("Hatchet"));
lib.borrow("Hatchet");
try {
    lib.borrow("Hatchet");
} catch (IllegalStateException e) {
    System.out.println("⚠️ " + e.getMessage());
}

Return a book

void giveBack(String title) {
    Book b = books.get(title);
    b.setAvailable(true);
    System.out.println("Returned " + title);
}

📖 Key words

Tap a card to flip it over!

Design
Planning classes before coding 🧩
final
Can’t be changed after it’s set 🔏
IllegalStateException
Action not allowed right now 🚫
Refactor
Improving code without changing what it does 🧹

🏆 Challenge

Extend the project: add a listAvailable() method that prints every available title. Here is the whole program — run it with the free Java JDK (ask a grown-up to install it) or an online Java editor your school approves, then predict and check the output.

Challenge

import java.util.LinkedHashMap;

class Book {
    private final String title;
    private boolean available = true;
    Book(String title) { this.title = title; }
    String getTitle() { return title; }
    boolean isAvailable() { return available; }
    void setAvailable(boolean a) { available = a; }
}

class Library {
    private final LinkedHashMap<String, Book> books = new LinkedHashMap<>();
    void add(Book b) { books.put(b.getTitle(), b); }
    void borrow(String title) { books.get(title).setAvailable(false); }
    void listAvailable() {
        for (Book b : books.values()) {
            if (b.isAvailable()) {
                System.out.println("✅ " + b.getTitle());
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Library lib = new Library();
        lib.add(new Book("Hatchet"));
        lib.add(new Book("Wonder"));
        lib.add(new Book("Holes"));
        lib.borrow("Hatchet");
        lib.listAvailable();
    }
}

Knowledge Check Quiz

  1. Why store books in a LinkedHashMap?
  2. What should borrow() do for a missing book?
  3. Which design is better?
← Previous lesson