This activity makes use of a hierarchy of classes representing e-commerce items, and is loosely based on that described in the unit.
In addition to the Solution document, the project for the activity contains the following hierarchy of classes. Those shaded are abstract.
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.
The class Bill is to fulfil the following specification.
List<Item> itemList
Bill()
Creates an ArrayList<Item> and makes itemList point to it.
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.
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.
Do not forget to include the appropriate import statement in Bill.