JOOPs Through Java
All lessons

Unit 5 · Applying Java OOP · 12 min

Putting it together: a small library system

A complete beginner project combining classes, encapsulation, inheritance, ArrayList and methods.

Why this matters

Concepts only click when you use several of them at once to solve a real problem.

Example

Java
import java.util.ArrayList;

class Item {
    private final String title;
    private boolean issued = false;

    Item(String title) {
        this.title = title;
    }

    public String getTitle() { return title; }
    public boolean isIssued() { return issued; }

    public void issue() {
        if (issued) {
            System.out.println(title + " is already issued");
        } else {
            issued = true;
            System.out.println("Issued: " + title);
        }
    }

    public void returnItem() {
        issued = false;
        System.out.println("Returned: " + title);
    }
}

class Book extends Item {
    private final String author;

    Book(String title, String author) {
        super(title);
        this.author = author;
    }

    public void describe() {
        System.out.println(getTitle() + " by " + author
            + (isIssued() ? " [issued]" : " [available]"));
    }
}

public class Library {
    public static void main(String[] args) {
        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("Code Garden", "Mira Sen"));
        books.add(new Book("Object Trails", "Dev Nair"));

        books.get(0).issue();
        books.get(0).issue();

        for (Book b : books) {
            b.describe();
        }
    }
}

Output

Issued: Code Garden
Code Garden is already issued
Code Garden by Mira Sen [issued]
Object Trails by Dev Nair [available]

Step by step

  1. 1Item holds what every library item shares: a title and an issued flag, both private (encapsulation).
  2. 2Book extends Item and adds an author — inheritance.
  3. 3`super(title)` passes the title up to the parent constructor.
  4. 4Because title is private in Item, Book reads it through getTitle().
  5. 5issue() protects against issuing the same book twice — business rules live inside the class.
  6. 6ArrayList<Book> stores the collection and the for-each loop describes each one.

Key points

  • Design classes around real things: Item, Book, Member
  • Keep data private and rules inside methods
  • Inheritance removes duplicate code
  • A collection plus a loop is the backbone of most small projects

Common mistakes

  • Putting all the logic in main instead of in classes
  • Making fields public 'just to finish quickly'
  • Forgetting super() in the child constructor

Try it yourself

  • Add a Member class that can borrow up to 3 books.
  • Add a Magazine class that also extends Item.
Open the practice notebook