RHS   Allan   SW construction Exercises ECP02


 

ECP02

Now let's implement the AddProductToCatalog use-case!

From SW Design you have the following diagrams:

   
   

From this (and other) use-case we can assume the following class-diagram:

   

ProductCatalog class
So we'll build a ProductCatalog class which can hold a collection of Products (either Books or CDs). As the Java-library holds a number of different implementations of the type List (which itself is an interface) we choose ArrayList.

Remember to add one or more constructor(s)!

The methods addBook() and addCD() must take the necessary parameters for creating an object.
For addBook() this is: productIdentifier, price, title, author and publisher.
For addCD() this is: productIdentifier, price, title, artist, label and composer.
They should both create an object and add it to the Product-list.

get() takes an int value which is the index of the Product to be returned. It uses the private method validIndex() which checks that the index is valid - greater or equal to zero and less than the number of objects in the list.
If the index-value is valid the corresponding Product object is returned otherwise null is returned.
Consider why validIndex() is private?

getProductCount() returns the number of Product objects in the list.

toString() should as usual return the state of an object. As a ProductCatalog object will have its own ArrayList which holds a number (possibly zero) of Product objects we must construct a String that holds information of every Product object in the list. So you must loop through the ArrayList and in each iteration get information for the corresponding Product object which you append to a String. When the loop is finished you can return the String.

TestProductCatalog
To test if the ProductCatalog works as expected we'll only build a GUI for entering Books (no CDs for now!).
Soon we'll build the actual GUI-classes to be used for adding products to the catalog.
But for now something like the window below is fine.

Add a (private) variable to hold the ProductCatalog.
In the constructor you should create the ProductCatalog object.
When the "Add" button is clicked the values should be read from the text fields and the addBook() method should be called (ativated). Finally the text fields should all be cleared.
When the "Show list" button is clicked the catalogs toString() method should be shown in the text area

   

Create the TestProductCatalog class as described above and use it to test the ProductCatalog class.


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04