//------------------------------------------------------------------- // Program P2 Q1 // // This program simulates a small database of book reviews. I assume // that there only two types of review possible. // // Author : Wei Tsang Ooi // Date : 11 July 1999 //------------------------------------------------------------------- //------------------------------------------------------------------- // class Book // // This class represents a book in our book review database. It // contains the title of the bok, the year of publication, and // reviewer notes. //------------------------------------------------------------------- class Book { private String title; // title of the book private int year; // year of publication private String notes; // reviewer notes about this book. private String summary; // summary of this book. //--------------------------------------------------------------- // constructor // // input : bookTitle - title of this book. // pubYear - the year this book is published. // summaryLine - the summary of this book. //--------------------------------------------------------------- public Book(String bookTitle, int pubYear, String summaryLine) { title = bookTitle; year = pubYear; notes = "No review available"; summary = summaryLine; } //--------------------------------------------------------------- // showReview // // print out an error message since reviews are available for // specific type of books only. //--------------------------------------------------------------- public void showReview() { System.out.println("ERROR : No review for this book."); } //--------------------------------------------------------------- // toString // // print the title, year and summary of this book. //--------------------------------------------------------------- public String toString() { return "TITLE : " + title + "\nYEAR : " + year + "\n" + "SUMMARY : " + summary; } //--------------------------------------------------------------- // static describe // // input : a Book object // print the title, year, summary and review of this book. //--------------------------------------------------------------- public static void describe(Book book) { System.out.println(book.toString()); book.showReview(); System.out.println(""); } //--------------------------------------------------------------- // describe // // alternate implementation of describe //--------------------------------------------------------------- public void describe() { System.out.println(toString()); showReview(); System.out.println(""); } }
//------------------------------------------------------------------- // class Literary // // This class represents a book in with good review. //------------------------------------------------------------------- class Literary extends Book { //------------------------------------------------------------------- // constructor // // Simply call the Book's constructor //------------------------------------------------------------------- public Literary(String name, int year, String summary) { super(name, year, summary); } //------------------------------------------------------------------- // showReview // // Print out a good review. //------------------------------------------------------------------- public void showReview () { System.out.println("REVIEW : Fascinating. Two thumbs up !"); } };
//------------------------------------------------------------------- // class Trashy // // This class represents a book in with bad review. //------------------------------------------------------------------- class Trashy extends Book { //------------------------------------------------------------------- // constructor // // Simply call the Book's constructor //------------------------------------------------------------------- public Trashy(String name, int year, String summary) { super(name, year, summary); } //------------------------------------------------------------------- // showReview // // Print out a bad review. //------------------------------------------------------------------- public void showReview () { System.out.println("REVIEW : This is a bad bad book."); } };
//------------------------------------------------------------------- // class BookReviews // // This simple class demostrates the use of the class Literary and // Trashy. //------------------------------------------------------------------- class BookReviews { //------------------------------------------------------------------- // main // // This method creates three books, and print out their descriptions. //------------------------------------------------------------------- public static void main(String argv[]) { // Create an object of class Book. This will create // a book that does not have any review associated with it. Book behave = new Book( "Oh ! Behave.", 1991, "How to make your children behave at dinner table."); // Create a lietrary book and a trashy book. Literary hannibal = new Literary( "Hannibal", 1999, "Tells the story about the man who destroy Rome."); Trashy java = new Trashy( "Java : I Love It", 1939, "A traveller's diary about the exotic Java island in " + "South East Asia."); // Now print out their descriptions: title, year, summary // and reviews if available. Book.describe(behave); Book.describe(hannibal); Book.describe(java); } }