// Lecture 4 // CS U213 Fall 2008 // bookstore-methods2.java /* ;; A Book is (make-book String Author Num Symbol) ;; There are three kinds of books: fiction, nonfiction, textbook ;; represented by symbols 'F 'N 'T (define-struct book (title author price kind)) ;; An Author is (make-author String Num) (define-struct author (name yob)) ;; Examples of authors (define eh (make-author "Hemingway" 1900)) (define ebw (make-author "White" 1920)) (define mf (make-author "MF" 1970)) ;; Examples of books (define oms (make-book "Old Man and the Sea" eh 10 'F)) (define eos (make-book "Elements of Style" ebw 20 'N)) (define htdp (make-book "HtDP" mf 60 'T)) */ /* +---------------+ | Book | +---------------+ | String title | | Author author |--+ | int price | | | char kind | | +---------------+ | v +-------------+ | Author | +-------------+ | String name | | int yob | +-------------+ */ // to represent a book in a bookstore class Book{ String title; Author author; int price; char kind; Book(String title, Author author, int price, char kind){ this.title = title; this.author = author; this.price = price; this.kind = kind; } /* TEMPLATE: ... this.title ... -- String ... this.author ... -- Author ... this.price ... -- int ... this.kind ... -- char ... this.author.sameName(String) ... -- boolean */ // was this book written by the given author? boolean writtenBy(String author){ return this.author.sameName(author); } // was the author of this book born before the author of that book? boolean olderAuthor(Book that){ /* TEMPLATE: ... this.title ... ... that.title ... -- String ... this.author ... ... that.title ... -- Author ... this.price ... ... that.price ... -- int ... this.kind ... ... that.kind ... -- char ... this.author.sameName(String) ... -- boolean ... this.author.bornBefore(Author) ... -- boolean */ return this.author.bornBefore(that.author); } } class Author{ String name; int yob; Author(String name, int yob){ this.name = name; this.yob = yob; } /* TEMPLATE: ... this.name ... -- String ... this.yob ... -- Author ... this.sameName(String) ... -- boolean ... this.bornBefore(Author) ... -- boolean */ // is this author's name the same as the given name? boolean sameName(String name){ return this.name.equals(name); } // was this author born before the given author? boolean bornBefore(Author that){ return this.yob < that.yob; } } class Examples{ Examples(){} Author eh = new Author("Hemingway", 1900); Author ebw = new Author("White", 1920); Author mf = new Author("MF", 1970); Book oms = new Book("Old Man and the Sea", this.eh, 10, 'F'); Book eos = new Book("Elements of Style", this.ebw, 20, 'N'); Book htdp = new Book("HtDP", this.mf, 60, 'T'); // test the method writtenBy in the class Book boolean testWrittenBy = (check this.oms.writtenBy("Hemingway") expect true) && (check this.eos.writtenBy("Hemingway") expect false) && (check this.htdp.writtenBy("White") expect false); // test the method sameName in the class Author boolean testSameName = (check this.eh.sameName("Hemingway") expect true) && (check this.ebw.sameName("Hemingway") expect false) && (check this.mf.sameName("MF") expect true); // test the method olderAuthor in the class Book boolean testOlderAuthor = (check this.oms.olderAuthor(this.htdp) expect true) && (check this.htdp.olderAuthor(this.oms) expect false) && (check this.eos.olderAuthor(this.oms) expect false); // test the method bornBefore in the class Author boolean testBornBefore = (check this.eh.bornBefore(this.mf) expect true) && (check this.ebw.bornBefore(this.eh) expect false) && (check this.mf.bornBefore(this.eh) expect false); }