ELEC 372
Engineering Software Structures

Assignment 3: Items in an Electronic Shopping Basket



This assignment is to be performed individually. Informal discussions with classmates are acceptable; however, all design documents, code and other submitted work are to be your individual work.

Assignments are to be submitted in the ELEC 372 assignment drop box, located outside Goodwin 235, by Monday March 12 at 1:00 PM. A solution will be posted shortly after the assignments are collected, so we will not be able to accept late assignments. Note that printers often become backed up shortly before assignment submission deadlines - please take this into account in your planning.

An Electronic Shopping Basket

Many e-commerce web sites allow people to purchase goods on-line. Examples include Amazon and Chapters on-line. Such sites use the metaphor of an electonic shopping cart or basket to aid people in making their purchases. When a customer selects a new item for purchase, it is placed in the shopping basket. When the customer has finished shopping, he/she proceeds to "checkout" and charges the contents of the shopping basket to his/her credit card.

A difficulty with e-commerce sites is that local tax rules should be followed. For example, if the site is in Ontario, local GST+PST should be charged, while if the site is in Alberta (where there is no PST), only GST should be charged.

In this assignment, you are to implement a shopping basket. You are to apply two design patterns in your design: the abstract factory pattern to abstract the tax rules used in items purchased, and the object adapter pattern to allow reuse of Java's Hashtable class in your implementation.

To Do

Part 1

Items that people can purchase must implement the Item interface:
/*
 *   Represents an item purchased at an e-commerce web site. An item has
 *   associated with it an item id (the unique stock number for this item),
 *   an item name (a string name for the item), a quantity (the number of
 *   units of the item) and a unit price (the price of each unit of the
 *   item.) Note that implementations of this interface should use
 *   their constructors to set the initial values of these attributes.
 */
public interface Item {
   /*
    *    Returns the number of units of this item.
    *    @return Number of units of this item.
    */
   public int getQuantity ();

   /*
    *    Returns the stock number for this item.
    *    @return The stock number (unique id) for this item.
    */
   public String getItemId ();

   /*
    *    Returns the (non-unique) string name of this item.
    *    @return String name of this item
    */
   public String getItemName ();

   /*
    *    Returns the unit price of this item (before tax.) E.g., if there
    *    are three units of the item, and each costs $2.99, this method
    *    will return 2.99.
    *    @return Unit price of item
    */
   public float getUnitPrice ();

   /*
    *    Returns the price of this item. This is simply the product of the
    *       (unit cost) * (number of units)
    *    with applicable taxes added.
    *    @return Price of this item
    */
   public float getTotalPrice ();

   /*
    *    Returns this item in string form suitable for displaying.
    *    @return Displayable form of item.
    */
   public String toString();
}

In Ontario, the taxes to be added to items include the provincial sales tax (PST) of 8%, together with the federal sales tax (GST) of 7%. In Alberta, there is no provincial sales tax, so prices include only the GST. Provide two implementations of the Item interface, one for Ontario and one for Alberta. Provide a class diagram showing these classes and their attributes. Hint: you may implement a common superclass for items which your Ontario and Alberta classes extend. You may not modify the Item interface.

Part 2

Using the abstract factory design pattern, create an ItemFactory interface, and implementations of this interface for the Ontario and Alberta item classes. Provide a class diagram showing how the abstract factory pattern was applied. This interface is as follows:
public interface ItemFactory {
    /*
     *    Creates an Item with the given stock id, name and quantity.
     */
    public Item createItem (String id, String name, int quantity, float unitPrice);
}

Part 3

A shopping basket class should implement the following interface:
import java.util.Enumeration;
public interface ShoppingBasket {
    /*
     *   Adds an item into the shopping basket. If the item
     *   is already in the basket, the number of units of this item
     *   are added to the quantity of the existing item.
     *   @param newItem The item to be added.
     */
    public void addItem (Item newItem);

    /*
     *    Removes the given item from the shopping basket. The
     *    number of units in deletedItem are removed from the
     *    basket. E.g., if there are 10 units of the item in the
     *    basket, and we remove 3 units of the item, 7 units will
     *    remain.
     */
    public void removeItem (Item deletedItem);

    /*
     *    Returns an enumeration allowing the shopping list to be
     *    traversed.
     */
    public Enumeration elements ();

    /*
     *    Returns a version of the shopping basket in string form
     *    suitable for displaying.
     */
    public String toString ();
}

Provide an implementation of this interface. Your implementation should use the object adapter design pattern, based on Java's Hashtable class. Provide a class diagram showing how the object adapter pattern was applied. In case of ambiguity in the description of the Shopping Basket interface above, include a note in your report explaining the ambiguity and how you resolved it.

Hints:

Part 4

Create a UML sequence diagram showing how a client program would use your shopping basket class. Specifically, show the steps by which a client program creates a new item and then adds it to an existing shopping basket. Assume that Ontario tax rules are in effect. Your sequence diagram should include the client object, any factory objects used, the shopping basket and the Hashtable, and all messages passing between them.

To Hand In

Your report should consist of the following clearly labeled sections:
  1. [30%] The class diagrams created in parts 1, 2 and 3
  2. [10%] The sequence diagram created in part 4
  3. [50%] The code implemented in parts 1, 2 and 3
  4. [10%] A simple driver program showing how the shopping basket is instantiated and how items are added to the basket.
Your class diagrams may be drawn by hand or using a CASE tool such as Argo/UML.