// Lecture 2 // CS U213 Fall 2008 // bookstore3.java /* A Book is one of -- PrintBook -- AudioBook -- OnlineBook A PrintBook is (make-print-book String String Num) (define-struct print-book (title author price)) An AudioBook is (make-audio-book String String Num Num) (define-struct audio-book (title author price no-cds)) An OnlineBook is (make-online-book String String Num String) (define-struct online-book (title author price url)) ;; Examples of books (define oms (make-book "Old Man and the Sea" "EH" 10)) (define eos (make-book "Elements of Style" "EBW" 20)) (define htdp (make-book "HtDP" "MF" 60)) */ /* +-------+ | IBook | +-------+ / \ --- | ------------------------------------------- | | | +---------------+ +---------------+ +---------------+ | PrintBook | | AudioBook | | OnlineBook | +---------------+ +---------------+ +---------------+ | String title | | String title | | String title | | String author | | String author | | String author | | int price | | int price | | int price | +---------------+ | int noCDs | | String url | +---------------+ +---------------+ */ // to represent various books in a bookstore interface IBook{} // to represent a printed book in a bookstore class PrintBook implements IBook{ String title; String author; int price; PrintBook(String title, String author, int price){ this.title = title; this.author = author; this.price = price; } } // to represent an audio book in a bookstore class AudioBook implements IBook{ String title; String author; int price; int noCDs; AudioBook(String title, String author, int price, int noCDs){ this.title = title; this.author = author; this.price = price; this.noCDs = noCDs; } } // to represent an online book in a bookstore class OnlineBook implements IBook{ String title; String author; int price; String url; OnlineBook(String title, String author, int price, String url){ this.title = title; this.author = author; this.price = price; this.url = url; } } class Examples{ Examples(){} IBook oms = new PrintBook("Old Man and the Sea", "EH", 10); IBook eos = new PrintBook("Elements of Style", "EBW", 20); IBook htdp = new PrintBook("HtDP", "MF", 60); IBook omsAudio = new AudioBook("Old Man and the Sea", "EH", 10, 2); IBook htdpOnline = new OnlineBook("HtDP", "MF", 0, "htdp.org"); }