// Assignment 5 // CS U213 Fall 2008 // bookstore.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)) */ /* +-------+ | 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(); // was this book written by the given author? boolean writtenBy(String author); // was this book written by the same author as the given book? boolean sameAuthor(IBook that); } // 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 METHODS: ... this.salePrice() ... -- int ... this.writtenBy(String) ... -- boolean ... this.sameAuthor(IBook) ... -- boolean */ // compute the discounted sale price for this printed book int salePrice(){ return this.price - 3 * (this.price / 10); } // was this book written by the given author? boolean writtenBy(String author){ return this.author.equals(author); } // was this book written by the same author as the given book? boolean sameAuthor(IBook that){ return that.writtenBy(this.author); } } // 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 METHODS: ... this.salePrice() ... -- int ... this.writtenBy(String) ... -- boolean ... this.sameAuthor(IBook) ... -- boolean */ // compute the discounted sale price for this audio book int salePrice(){ return this.price - 2 * (this.price / 10); } // was this book written by the given author? boolean writtenBy(String author){ return this.author.equals(author); } // was this book written by the same author as the given book? boolean sameAuthor(IBook that){ return that.writtenBy(this.author); } } // 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: FIELDS: ... this.title ... -- String ... this.author ... -- String ... this.price ... -- int ... this.url ... -- int METHODS: ... this.salePrice() ... -- int ... this.writtenBy(String) ... -- boolean ... this.sameAuthor(IBook) ... -- boolean */ // compute the discounted sale price for this online book int salePrice(){ return this.price; } // was this book written by the given author? boolean writtenBy(String author){ return this.author.equals(author); } // was this book written by the same author as the given book? boolean sameAuthor(IBook that){ return that.writtenBy(this.author); } } 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 ll = new PrintBook("LL", "MF", 20); IBook omsAudio = new AudioBook("Old Man and the Sea", "EH", 10, 2); IBook omsOnline = new OnlineBook("Old Man and the Sea", "EH", 10, "oms.com"); 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); // test the method writtenBy in the class Book boolean testWrittenBy = (check this.oms.writtenBy("EH") expect true) && (check this.eos.writtenBy("EH") expect false) && (check this.htdp.writtenBy("MF") expect true); // test the method sameAuthor in the class Book boolean testSameAuthor = (check this.oms.sameAuthor(this.eos) expect false) && (check this.eos.sameAuthor(this.ll) expect false) && (check this.htdp.sameAuthor(this.ll) expect true); }