// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   CartManager.java

package com.keyshop.shop.order.model;

import com.keyshop.pub.model.PubBean;
import com.keyshop.shop.product.bo.ProductBO;
import com.keyshop.shop.product.model.Product;
import com.keyshop.shop.user.model.User;
import java.util.ArrayList;
import java.util.List;

public class CartManager extends PubBean
{
    public class CartManagerProduct
    {

        String id;
        int number;
        Product product;

        public void setId(String thisId)
        {
            id = thisId;
        }

        public void setProduct(Product product)
        {
            this.product = product;
        }

        public void setNumber(int number)
        {
            this.number = number;
        }

        public String getId()
        {
            return id;
        }

        public Product getProduct()
        {
            return product;
        }

        public int getNumber()
        {
            return number;
        }

        public CartManagerProduct()
        {
            id = "";
            number = 1;
            product = null;
        }
    }


    List products;

    public CartManager()
    {
        products = new ArrayList();
    }

    public void setProducts(List products)
    {
        this.products = products;
    }

    public List getProducts()
    {
        return products;
    }

    public double getTotalAmount(User user)
    {
        double totalAmount = 0.0D;
        if(products != null && products.size() > 0)
        {
            for(int i = 0; i < products.size(); i++)
            {
                CartManagerProduct product = (CartManagerProduct)products.get(i);
                if(user == null)
                    totalAmount += product.getProduct().getUnitPrice() * (double)product.getNumber();
                else
                    totalAmount += product.getProduct().getUnitPriceByUserLevel(user) * (double)product.getNumber();
            }

        }
        return totalAmount;
    }

    public void addProduct(String productId, int number)
    {
        if(productId == null || productId.length() < 0)
            return;
        try
        {
            ProductBO bo = new ProductBO();
            Product product = (Product)bo.get(productId);
            if(product != null)
            {
                if(products != null && products.size() > 0)
                {
                    for(int i = 0; i < products.size(); i++)
                    {
                        CartManagerProduct tp = (CartManagerProduct)products.get(i);
                        if(tp.getId().equals(product.getId()))
                        {
                            tp.setNumber(tp.getNumber() + number);
                            return;
                        }
                    }

                }
                CartManagerProduct cproduct = new CartManagerProduct();
                cproduct.setId(product.getId());
                cproduct.setProduct(product);
                cproduct.setNumber(number);
                products.add(cproduct);
            }
        }
        catch(Exception exception) { }
    }

    public void removeProduct(String productId)
    {
        if(productId == null || productId.length() < 0)
            return;
        try
        {
            if(products != null && products.size() > 0)
            {
                for(int i = 0; i < products.size(); i++)
                {
                    CartManagerProduct tp = (CartManagerProduct)products.get(i);
                    if(tp.getId().equals(productId))
                    {
                        products.remove(tp);
                        return;
                    }
                }

            }
        }
        catch(Exception exception) { }
    }
}
