public ShoppingCartItem AddProductToCart(string shoppingCartId, Product product) { lock (_lock) { ShoppingCart shoppingCart = GetById(shoppingCartId); if (shoppingCart == null) { shoppingCart = new ShoppingCart(new List<ShoppingCartItem>()) { ShoppingCartId = shoppingCartId, Currency = "USD", TaxRate = .09 }; _shoppingCarts[shoppingCartId] = shoppingCart; } ShoppingCartItem item = shoppingCart.ShoppingCartItems.FirstOrDefault(c => c.Product.ProductNumber == product.ProductNumber); if (item == null) { item = new ShoppingCartItem { Id = product.ProductNumber, Product = product, Quantity = 1, Currency = shoppingCart.Currency }; shoppingCart.ShoppingCartItems.Add(item); } else { item.Quantity++; } UpdatePrices(shoppingCart); return item; } }
public bool RemoveItemFromCart(ShoppingCart shoppingCart, string itemId) { lock (_lock) { if (shoppingCart == null) { throw new ArgumentNullException("shoppingCart"); } ShoppingCartItem item = shoppingCart.ShoppingCartItems.FirstOrDefault(i => i.Id == itemId); bool itemRemoved = shoppingCart.ShoppingCartItems.Remove(item); if (itemRemoved) { UpdatePrices(shoppingCart); } return itemRemoved; } }
private static void UpdatePrices(ShoppingCart shoppingCart) { double fullPrice = 0, discount = 0; foreach (var shoppingCartItem in shoppingCart.ShoppingCartItems) { fullPrice += shoppingCartItem.Product.ListPrice * shoppingCartItem.Quantity; shoppingCart.FullPrice = fullPrice; shoppingCart.TotalDiscount = discount; shoppingCart.TotalPrice = fullPrice - discount; } }
bool IShoppingCartRepository.RemoveItemFromCart(ShoppingCart shoppingCart, string itemId) { return RemoveItemFromCartDelegate(shoppingCart, itemId); }