// Lecture 3 // CS U213 Fall 2008 // bookstore4.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; } } class Author{ String name; int yob; Author(String name, int yob){ this.name = name; this.yob = yob; } } interface ILoB{} class MtLoB implements ILoB{ MtLoB(){} } class ConsLoB implements ILoB{ Book first; ILoB rest; ConsLoB(Book first, ILoB rest){ this.first = first; this.rest = rest; } } 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'); ILoB mtlob = new MtLoB(); ILoB blist2 = new ConsLoB(this.oms, new ConsLoB(this.eos, this.mtlob)); ILoB blist3 = new ConsLoB(this.htdp, this.blist2); }