ELEC
372
|
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.
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.
/*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.
* 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();
}
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);
}
import java.util.Enumeration;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.
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 ();
}
Hints: