5.3  Example: Finding a book with some title in a list of books

1. Data Analysis and Problem Analysis

We need is the list of books and the title of the book to look for.

2. Purpose and Contract/Header:

  // PURPOSE AND CONTRACT:
  // determine whether a book with the given title is in this list
  abstract boolean find(String title);

We develop the methods in the two classes concurrently, starting with examples.

3. Examples:

  Book b1 = new Book("HtDP", "Matthias", 60, 2001);
  Book b2 = new Book("Beach Music", "Conroy", 20, 1996);
  Book b3 = new Book("3 Com", "Remarque", 15, 1937);
  ALoB mtlob = new MtLoB();
  ALoB lob1 = new ConsLoB(b1, mtlob);
  ALoB lob2 = new ConsLoB(b2, lob1);
  ALoB lob3 = new ConsLoB(b3, lob2);
  ...
  mtlob.find("Beach Music")  -- expected: false
  lob1.find("Beach Music")   -- expected: false
  lob2.find("Beach Music")   -- expected: true
  lob3.find("HtDP")          -- expected: true

4. Template:

There is a separate template for each of the two subclasses.

  /* TEMPLATE for the MtLoB class:
  boolean find(String title) {
    ...  }  */

 /* TEMPLATE for the ConsLoB class:
  boolean find(String title) {
    ... this.fst ...
        ... this.fst.title ...
        ... this.fst.author ...
        ... this.fst.price ...
        ... this.fst.year ...
    ... this.rst.find(title) ... }  */

5. Program:

Again, we have two different methods, one for each class.

  // PROGRAM for the class MtLoB: 
  boolean find(String title) {
    return false; }  

  // PROGRAM for the class ConsLoB: 
  boolean find(String title) {
    return (title.equals(this.fst.title) || this.rst.find(title)); }

6. Tests:

  mtlob.find("Beach Music")  == false
  lob1.find("Beach Music")   == false
  lob2.find("Beach Music")   == true
  lob3.find("HtDP")          == true