A book is not a copy
After this: Split a title from the copies on the shelf, and search the titles.
On this page · 4 sections · 3 checks
The library owns two copies of Clean Code. Ana borrows one of them. Is Clean Code "on loan" now? The question has no single answer, because one copy is out and the other is on the shelf. This lesson shows how the classes keep those two facts apart.
Two kinds of thing called "book"
People say "book" for two different things:
- The title: Clean Code by Robert Martin, with its ISBN. There is only one of these, however many the library buys.
- The thing on the shelf, with a sticker on it. The library may have several, and each is in or out on its own.
The program needs a class for each. Book holds the title, the author and the ISBN. BookCopy is one thing on the shelf: it has its own id, such as CC-1, and it points at the Book it is a copy of. Its status says where it is: available, on loan, or on hold for someone.
Run the diagram below to see the four boxes appear.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
In code
Here are the first classes. The library makes the copies itself when a book is added, and numbers them CC-1, CC-2, and so on.
class Book: def __init__(self, isbn, title, author): self.isbn = isbn self.title = title self.author = author
class BookCopy: def __init__(self, copy_id, book): self.copy_id = copy_id self.book = book self.status = "available" # "available", "on loan" or "on hold"
class Library: def __init__(self): self.books = {} self.copies = {}
def add_book(self, book, how_many): self.books[book.isbn] = book self.copies[book.isbn] = [] for i in range(1, how_many + 1): self.copies[book.isbn].append(BookCopy(f"{book.isbn}-{i}", book))
def search(self, word): word = word.lower() found = [] for b in self.books.values(): if word in b.title.lower() or word in b.author.lower(): found.append(b.title) return found
lib = Library()lib.add_book(Book("CC", "Clean Code", "Robert Martin"), 2)lib.add_book(Book("DU", "Dune", "Frank Herbert"), 1)lib.add_book(Book("RF", "Refactoring", "Martin Fowler"), 1)print(", ".join(lib.search("dune")))for copy in lib.copies["CC"]: print(copy.copy_id, "is a copy of", copy.book.title)Dune CC-1 is a copy of Clean Code CC-2 is a copy of Clean Code
import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;
enum CopyStatus { AVAILABLE, ON_LOAN, ON_HOLD }
class Book { String isbn; String title; String author;
Book(String isbn, String title, String author) { this.isbn = isbn; this.title = title; this.author = author; }}
class BookCopy { String copyId; Book book; CopyStatus status = CopyStatus.AVAILABLE;
BookCopy(String copyId, Book book) { this.copyId = copyId; this.book = book; }}
class Library { private Map<String, Book> books = new LinkedHashMap<>(); Map<String, List<BookCopy>> copies = new LinkedHashMap<>();
void addBook(Book book, int howMany) { books.put(book.isbn, book); List<BookCopy> list = new ArrayList<>(); for (int i = 1; i <= howMany; i++) { list.add(new BookCopy(book.isbn + "-" + i, book)); } copies.put(book.isbn, list); }
List<String> search(String word) { String w = word.toLowerCase(); List<String> found = new ArrayList<>(); for (Book b : books.values()) { if (b.title.toLowerCase().contains(w) || b.author.toLowerCase().contains(w)) { found.add(b.title); } } return found; }}
public class Main { public static void main(String[] args) { Library lib = new Library(); lib.addBook(new Book("CC", "Clean Code", "Robert Martin"), 2); lib.addBook(new Book("DU", "Dune", "Frank Herbert"), 1); lib.addBook(new Book("RF", "Refactoring", "Martin Fowler"), 1); System.out.println(String.join(", ", lib.search("dune"))); for (BookCopy copy : lib.copies.get("CC")) { System.out.println(copy.copyId + " is a copy of " + copy.book.title); } }}Dune CC-1 is a copy of Clean Code CC-2 is a copy of Clean Code
Read it class by class:
Bookkeeps three pieces of text and does nothing else.BookCopykeeps its id, the book it belongs to, and its status. Every copy starts as available.Librarykeeps two maps, both keyed by ISBN: the books, and a list of copies for each book. add_bookaddBook stores the book it is given, then makes how_manyhowMany copies in a loop. The book is made outside and handed in, so the diagram draws a hollow diamond fromLibrarytoBook. The library makes each copy itself, so the link toBookCopyhas a filled diamond.searchturns the word into lower case, then goes through the books, not the copies. A title that matches is added once, however many copies it has.- The last lines search for "dune" and list the copies of Clean Code. Each copy reaches its title through its book.
Why not one class?
You could keep one Book class with a status field and make one object per copy. It runs. But the title and author are now stored in every copy, and a search that goes through the objects finds Clean Code twice. If the author's name is spelled wrong, you fix it in two places. Keeping the shared facts in Book and the per-copy facts in BookCopy means each fact is written once.
Check yourself
Ana borrows CC-1. The other copy of Clean Code, CC-2, is still on the shelf. Where must the words “on loan” be kept?
The classes are the catalogue above: Clean Code by Robert Martin has 2 copies, Dune by Frank Herbert 1, Refactoring by Martin Fowler 1. What does this print?
lib = Library()lib.add_book(Book("CC", "Clean Code", "Robert Martin"), 2)lib.add_book(Book("DU", "Dune", "Frank Herbert"), 1)lib.add_book(Book("RF", "Refactoring", "Martin Fowler"), 1)print(", ".join(lib.search("martin")))Library lib = new Library();lib.addBook(new Book("CC", "Clean Code", "Robert Martin"), 2);lib.addBook(new Book("DU", "Dune", "Frank Herbert"), 1);lib.addBook(new Book("RF", "Refactoring", "Martin Fowler"), 1);System.out.println(String.join(", ", lib.search("martin")));A classmate keeps one class, Book, with a status field, and makes two Book objects for the two copies of Clean Code. What goes wrong first?
- Keep a title (Book: ISBN, title, author) apart from each thing on the shelf (BookCopy: its own id and status).
- Put the status on the copy, because two copies of one book can be in different states at once.
- Write a search that goes through the books, not the copies, and compares lower-case text.
- Use a Java enum for a fixed set of states where Python uses a string.