FileDocCategorySizeDatePackage
ShoppingCart.javaAPI DocExample2516Tue Dec 12 18:59:36 GMT 2000cart

ShoppingCart

public class ShoppingCart extends Object

Fields Summary
HashMap
items
int
numberOfItems
Constructors Summary
public ShoppingCart()


      
        items = new HashMap();
    
Methods Summary
public voidadd(java.lang.String bookId, database.BookDetails book)

        if(items.containsKey(bookId)) {
            ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
            scitem.incrementQuantity();
        } else {
            ShoppingCartItem newItem = new ShoppingCartItem(book);
            items.put(bookId, newItem);
        }

        numberOfItems++;
    
public voidclear()

        items.clear();
        numberOfItems = 0;
    
protected voidfinalize()

        items.clear();
    
public java.util.CollectiongetItems()

        return items.values();
    
public intgetNumberOfItems()

        return numberOfItems;
    
public doublegetTotal()

        double amount = 0.0;

        for(Iterator i = getItems().iterator(); i.hasNext(); ) {
            ShoppingCartItem item = (ShoppingCartItem) i.next();
            BookDetails bookDetails = (BookDetails) item.getItem();

            amount += item.getQuantity() * bookDetails.getPrice();
        }
        return roundOff(amount);
    
public voidremove(java.lang.String bookId)

        if(items.containsKey(bookId)) {
            ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
            scitem.decrementQuantity();

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

            numberOfItems--;
        }
    
private doubleroundOff(double x)

        long val = Math.round(x*100); // cents
        return val/100.0;