コード例 #1
0
        /// <summary>
        /// Adds a product in the cart or increment its quantity in the cart if already added
        /// </summary>//
        public void AddItem(Product product, int quantity)
        {
            // Dilip:DONE
            var cartItems = Lines as List <CartLine>;

            //check if the product alredy exists in the cart
            CartLine cartItem = cartItems.FirstOrDefault(item => item.Product.Id == product.Id);

            if (cartItem != null)
            {
                //update quantity if product already exists in the cart
                cartItems.Remove(cartItem);
                cartItem.Quantity += quantity;
                cartItems.Add(cartItem);
            }
            else
            {
                //add the product
                cartItems.Add(new CartLine()
                {
                    Product = product, Quantity = quantity
                });
            }
        }
コード例 #2
0
ファイル: Cart.cs プロジェクト: Atomic-C/DotNetEnglishP2
        /// <summary>
        /// Adds a product in the cart or increment its quantity in the cart if already added
        /// </summary>//
        public void AddItem(Product product, int quantity)
        {
            var productInCartLines = FindProductInCartLines(product.Id);

            if (productInCartLines != null)
            {
                foreach (var cartLine in GetCartLineList())
                {
                    if (cartLine.Product.Id == product.Id)
                    {
                        cartLine.Quantity++;
                    }
                }
            }
            else
            {
                CartLine cartItem = new CartLine();

                cartItem.Product  = product;
                cartItem.Quantity = quantity;

                _cartLines.Add(cartItem);
            }
        }
コード例 #3
0
        /// <summary>
        /// Looks after a given product in the cart and returns if it finds it
        /// </summary>
        public Product FindProductInCartLines(int productId)
        {
            CartLine cartLine = GetCartLineList().FirstOrDefault(l => l.Product.Id == productId);

            return(cartLine?.Product);
        }