Final Lab Test

Posted: January 13, 2009

Today’s job, intrepid programmers, is to help finish creating a product- and order-tracking system for a large online book store. Most of the code has already been written, but your expertise is needed in a few key places. Please be sure your work meets the specifications exactly (and that everything compiles) or else this business will fail. Fortunately, you have your computer and internet connection in great working order, not to mention a strong understanding of Java!

Task 1 Create a Catalog class. Be sure to name the file correctly and put in all the necessary code to define the class in such a way that it is usable by anyone. The Catalog class will keep track of all the different books we sell in our store. The books are represented by instances of class Book, which has already been created. Download Book.java and add it to your project.

Task 2 Give your Catalog class a way to keep track of a whole bunch of Books. We can’t predict how many Books we might have for sale at any given moment, so be sure your way of storing this data can handle changes. Place a comment after the member variable declaration that explains why you chose the particular type.

Task 3 Create a method in Catalog called addBook that takes a single Book parameter and returns nothing. The Book that is passed in should be added to the Catalog. Create another method called listAllBooks that takes nothing and prints, on one line per book, the title and author.

Task 4 Complete the following method and add it to Catalog:

boolean removeByTitle( String x )

This method should look for any Books with titles that contain the word x and remove all of them. If any such Books were found, the method should return true. Otherwise it should return false.

Task 5 Complete the following method and add it to Catalog:

void setDiscount( double percentDiscount )

This method should reduce the price of every Book in the Catalog by whatever percentage is passed in. You are guaranteed that percentDiscount will be a number between 1.0 and 99.0. For example, if the original price of a particular Book is 5.25 and the percentDiscount is 20.0 then the price of the Book after this method acts on it should be 5.25 - 1.05 = 4.20. The actual prices stored in each Book’s member variable price should be changed after this method completes.

Task 6 Create a subclass of Book called Omnibus. (This is what you call a single book that contains the text of two or more previously-published books.) Your subclass should have a way to store up to 10 Strings that are the titles of the books in the omnibus. It should provide an addTitle( String t ) method, and you are guaranteed that this method will not be called more than 10 times. Override getTitle() to return any and all Strings previously passed in to addTitle, separated by commas.

Consider the following code as if it were a test drive of your Omnibus class:

Omnibus o = new Omnibus();
o.addTitle("Murder in the Cathedral");
o.addTitle("The Waste Land");
o.addTitle("Four Quartets");
System.out.println( o.getTitle() );

And, if you’ve implemented this class correctly, the following output should be printed:

Murder in the Cathedral, The Waste Land, Four Quartets

Keep in mind that, as a subclass of Book, instances of Omnibus can be used anywhere that instances of Book were used previously.

Task 7 Create a new class called ShoppingCart. Complete the following method:

public void addToCart( Book b, int q )

The addToCart method, which may be called any number of times, should cause your class to keep track of both the Book b and quantity q that the customer wants to order. Be sure that whatever way you store this information will allow you to complete the following method:

public double getTotal()

The getTotal method should return the sum of the prices of all Books in the ShoppingCart, with each price first multiplied by the quantity specified when the book was passed in to addToCart.

Extra Credit Add methods to your ShoppingCart that will print a detailed invoice, showing the title, price, and quantity of each book in the order along with the extended cost for each book (this means price times quantity), and the total for the whole order.