// Lecture 5 // CS U213 Fall 2008 // bookstore-methods3.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{ /* the sale price of the book depends on the daily discounts these may differ depending on the kind of book suppose today we have the following discounts: there is 30% discount on print books there is 20% discount on audio books online books sell at full price */ // compute the discounted sale price for this book int salePrice(); } // 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; } /* TEMPLATE: ... this.title ... -- String ... this.author ... -- String ... this.price ... -- int ... this.salePrice() ... -- int */ // compute the discounted sale price for this printed book int salePrice(){ return this.price - 3 * (this.price / 10); } } // 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; } /* TEMPLATE: ... this.title ... -- String ... this.author ... -- String ... this.price ... -- int ... this.noCDs ... -- int ... this.salePrice() ... -- int */ // compute the discounted sale price for this audio book int salePrice(){ return this.price - 2 * (this.price / 10); } } // 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; } /* TEMPLATE: ... this.title ... -- String ... this.author ... -- String ... this.price ... -- int ... this.url ... -- int ... this.salePrice() ... -- int */ // compute the discounted sale price for this online book int salePrice(){ return this.price; } } 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 omsOnline = new OnlineBook("Old Man and the Sea", "EH", 10, 2); IBook htdpOnline = new OnlineBook("HtDP", "MF", 0, "htdp.org"); // test the method salePrice in the class Book boolean testSalePrice = (check this.oms.salePrice() expect 7) && (check this.omsAudio.salePrice() expect 8) && (check this.omsOnline.salePrice() expect 10) && (check this.htdpOnline.salePrice() expect 0); }