
package cart;

import java.util.*;
import database.*;

public class ShoppingCart {
    Hashtable items = null;
    int numberOfItems = 0;

    public ShoppingCart() {
        items = new Hashtable();
    }

    public void add(String Id, Object obj, String type) {
        if(items.containsKey(Id)) {
            ShoppingCartItem scitem = (ShoppingCartItem) items.get(Id);
            scitem.incrementQuantity();
        } else {
            ShoppingCartItem newItem = new ShoppingCartItem(obj,type);
            items.put(Id, newItem);
        }

        numberOfItems++;
    }

    public void remove(String Id) {
        if(items.containsKey(Id)) {
            ShoppingCartItem scitem = (ShoppingCartItem) items.get(Id);
            scitem.decrementQuantity();

            if(scitem.getQuantity() <= 0)
                items.remove(Id);

            numberOfItems--;
        }
    }

    public Object getItemByKey(String Key) {
        if(items.containsKey(Key)) {
            ShoppingCartItem scitem = (ShoppingCartItem) items.get(Key);
	    return(scitem);
        }
	return(null);
    }

    public Enumeration getItems() {
        return items.elements();
    }

    protected void finalize() throws Throwable {
        items.clear();
    }

    public int getNumberOfItems() {
        return numberOfItems;
    }

    public void clear() {
        items.clear();
        numberOfItems = 0;
    }
}
