Activity 5.6 - E-commerce class hierarchy with abstract classes

Topic

This activity makes use of a hierarchy of classes representing e-commerce items, and is loosely based on that described in the unit.

Materials

In addition to the Solution document, the project for the activity contains the following hierarchy of classes. Those shaded are abstract.

This figure is a UML diagram illustrating the inheritance relationships between six classes related to E-commerce items.  Each class is shown as a
rectangle containing the name of the class.  Inheritance relationships are shown by an open-headed arrow pointing from the subclass to the superclass.
The rectangle for each abstract class is shown shaded - all the other (concrete) classes are shown unshaded.  The topmost class of the hierarchy is an abstract
class, named Item.  This has two subclasses, MultiMediaItem, which is abstract and PrintedItem, which is not abstract.  MultiMediaItem has two concrete subclasses,
named  MusicCD and VideoCD.  The class PrintedItem has one concrete subclass named Book.

These classes are already fully coded and are included in the project.

The project also contains a class TestClass for testing the application. The project is missing a class Bill that you are asked to create and write the code for.

Task

The class Bill is to fulfil the following specification.

Instance variable

List<Item> itemList 

Constructor

Bill() 

Creates an ArrayList<Item> and makes itemList point to it.

Methods

void addItem(Item item) 

Adds an item to the array list.

void removeItem(Item item) 

Removes an item from the array list. This method is provided so that errors can be corrected, or so that the user can amend their order.

void printBill() 

Prints out the name of each item in the array list, with its unit cost in pence, calculates the total amount as it goes along, and prints the total, also in pence, at the end of the list, e.g.

Item 1: The Java Collection Classes cost 1200
Item 2: Favourites from the Palm Court Orchestra cost 399
Total amount: 1599
Thank you for visiting us. Please come again. 

This method makes use of methods getUnitPrice and getItemName specified in Item.

For simplicity we work entirely in pence. In a real-life application we would of course have to use pounds and pence, properly formatted, but for the purposes of this activity we will ignore this complication.

Instructions

Create a new class Bill that implements the specification given above. You can open the New Java Class wizard in the same way as you did in Activity 4 of the NetBeans Guide, or right-click on the package node in the Projects window and select New||Java Class. Make sure the new class is part of the project's package.

Run the project to test your code. It should compile automatically, and TestClass is already selected as the main class.

Note

Do not forget to include the appropriate import statement in Bill.