Time to build something with several classes working together: a small library that lends books. First, design. Read the job and pick out the nouns (likely classes) and verbs (likely methods):
"Members borrow and return books. A book is either available or on loan. A member can have at most two books at once. The library finds books by their code."
Book: a code, a title and a status. A record won't do, since the status changes, so it's a small class.
Status: an enum, AVAILABLE or ON_LOAN.
Member: a name and the books they have.
Library: a map from code to book, and the lend and giveBack methods that enforce the rules, throwing an exception when a rule is broken.
Java
public class Main {
enum Status { AVAILABLE, ON_LOAN }
static class LoanException extends RuntimeException {
LoanException(String why) { super(why); }
}
static class Book {
private final String code, title;
private Status status = Status.AVAILABLE;
Book(String code, String title) { this.code = code; this.title = title; }
String code() { return code; }
String title() { return title; }
Status status() { return status; }
void setStatus(Status s) { status = s; }
}
static class Member {
static final int LIMIT = 2;
private final String name;
private final List<Book> books = new ArrayList<>();
Member(String name) { this.name = name; }
String name() { return name; }
List<Book> books() { return books; }
}
static class Library {
private final Map<String, Book> books = new LinkedHashMap<>();
void add(Book b) { books.put(b.code(), b); }
Book find(String code) {
Book b = books.get(code);
if (b == null) throw new LoanException("no book " + code);
return b;
}
void lend(String code, Member m) {
Book b = find(code);
if (b.status() == Status.ON_LOAN) throw new LoanException(b.title() + " is on loan");
if (m.books().size() >= Member.LIMIT) throw new LoanException(m.name() + " has " + Member.LIMIT + " books already");
b.setStatus(Status.ON_LOAN);
m.books().add(b);
}
void giveBack(String code, Member m) {
Book b = find(code);
if (!m.books().remove(b)) throw new LoanException(m.name() + " doesn't have " + b.title());
b.setStatus(Status.AVAILABLE);
}
long available() {
int n = 0;
for (Book b : books.values()) if (b.status() == Status.AVAILABLE) n++;
return n;
}
}
public static void main(String[] args) {
Library lib = new Library();
lib.add(new Book("B1", "Pond Life"));
lib.add(new Book("B2", "Duck Tales"));
lib.add(new Book("B3", "Rivers"));
Member ana = new Member("Ana"), ben = new Member("Ben");
lib.lend("B1", ana);
lib.lend("B2", ana);
String[][] tries = { {"B3", "Ana"}, {"B1", "Ben"}, {"B9", "Ben"} };
for (String[] t : tries) {
try {
lib.lend(t[0], t[1].equals("Ana") ? ana : ben);
} catch (LoanException e) {
System.out.println("Refused: " + e.getMessage());
}
}
lib.giveBack("B1", ana);
lib.lend("B1", ben);
System.out.println(ana.name() + " has " + ana.books().size() + ", " + ben.name() + " has " + ben.books().size());
System.out.println(lib.available() + " available");
}
}
Prints
Refused: Ana has 2 books already
Refused: Pond Life is on loan
Refused: no book B9
Ana has 1, Ben has 1
1 available
Each class does one job. The rules live in one place, Library, so they can't be skipped, and breaking one throws an exception with a clear reason. LoanException extends RuntimeException, so it's unchecked: callers catch it only where they can do something about it, like the loop in main that tells the user.
Testing it
Printing results and checking them by eye works for a lesson, not for real code. Real projects write unit tests: small methods that run the code and check the answer, so you can rerun hundreds of checks in seconds after every change. The standard tool is JUnit. A test sets things up, does one thing and asserts what should be true:
LibraryTest.java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class LibraryTest {
@Test
void lendingMarksTheBookOnLoan() {
Library lib = new Library();
lib.add(new Book("B1", "Pond Life"));
Member ana = new Member("Ana");
lib.lend("B1", ana);
assertEquals(Status.ON_LOAN, lib.find("B1").status());
assertEquals(1, ana.books().size());
}
@Test
void aBookOnLoanCantBeLentAgain() {
Library lib = new Library();
lib.add(new Book("B1", "Pond Life"));
lib.lend("B1", new Member("Ana"));
LoanException e = assertThrows(LoanException.class, () -> lib.lend("B1", new Member("Ben")));
assertEquals("Pond Life is on loan", e.getMessage());
}
}
assertEquals(expected, actual) fails the test if they differ, and assertThrows checks that the code throws. A build tool (the Advanced Java course covers Maven and Gradle) runs every method marked @Test and lists any that fail. Name tests for the rule they check, so a failure reads like a sentence.
Test the rules and the edges: the third book, returning a book you don't have, a code that doesn't exist. Those are where bugs hide.
Practice
Reading a job description to design classes, what do nouns and verbs usually become?
More info
Quick reference
nouns → classes, verbs → methods
A first pass at a design.
one job per class
Rules live in one place and can't be skipped.
custom RuntimeException
A clear reason when a rule is broken.
unit test
A small method that runs code and checks the result.