edu.uci.ics.inf111.dvdvendor.app
Class ProductDB

java.lang.Object
  extended by edu.uci.ics.inf111.dvdvendor.app.ProductDB

public class ProductDB
extends java.lang.Object

The ProductDB class encapsulates the list of all products sold in the store. In a real system, this would likely be a wrapper around a database of products which would be managed elsewhere. In our sample system, we have a method which can provide a sample DB, and the capability to add items to the DB using an addItem() method. The ProductDB is instantiated only once and it uses the singleton pattern to guarantee this characteristic.


Field Summary
private static ProductDB instance
          Private instance of this class
private  java.util.Hashtable<java.lang.String,Product> productsHT
          This Hashtable is the core of our sample DB.
 
Constructor Summary
private ProductDB()
          Constructs an empty database.
 
Method Summary
 void addItem(Product item)
          This method is called to add items directly to the database in our example.
static ProductDB getInstance()
          It returns a new class if the instance is null, else it returns the instance already created and initialized
 void initializeTestDB()
          This test method constructs a sample database which is useful for testing purposes.
 java.util.Hashtable<java.lang.String,Product> listAll()
          This method returns a copy of the ProductDB Hashtable.
 Product lookUpItem(BarCode barCode)
          This method looks up a product in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

private static ProductDB instance
Private instance of this class


productsHT

private java.util.Hashtable<java.lang.String,Product> productsHT
This Hashtable is the core of our sample DB. In a real implementation, the actual data would likely be in a separate database, which we would access using database queries.

Constructor Detail

ProductDB

private ProductDB()
Constructs an empty database. This method should not be used by outside classes

Method Detail

getInstance

public static ProductDB getInstance()
                             throws java.lang.Exception
It returns a new class if the instance is null, else it returns the instance already created and initialized

Throws:
java.lang.Exception

initializeTestDB

public void initializeTestDB()
                      throws java.lang.Exception
This test method constructs a sample database which is useful for testing purposes. Products may also be added individually using the addItem() method.

Throws:
java.lang.Exception

listAll

public java.util.Hashtable<java.lang.String,Product> listAll()
This method returns a copy of the ProductDB Hashtable. If we provided the original, external code could modify the DB directly.


lookUpItem

public Product lookUpItem(BarCode barCode)
This method looks up a product in the database.

Parameters:
code - The bar code of the product.
Returns:
The Product class of the corresponding product, or null if no such product.

addItem

public void addItem(Product item)
             throws ProductDBException
This method is called to add items directly to the database in our example. This method checks if the product already exist in the DB. If the product exists, a ProductDBException will be thrown, else the product will be added to the database. In a real implementation, this would likely be done directly to the product database using a separate piece of software.

Parameters:
item - The product to be added.
Throws:
ProductDBException - If the product already exist in the DB.