public BasketItem(int productId, int quantity, Pounds priceInPounds) { Contract.Require(productId > 0, ErrorMessage.ProductIdGreaterThanZero); Contract.Require(quantity > 0, ErrorMessage.QuantityGreaterThanZero); Contract.Require(priceInPounds != null, ErrorMessage.PriceRequired); Contract.Require(!priceInPounds.IsZero, ErrorMessage.PriceCannotBeZero); ProductId = productId; Quantity = quantity; PriceInPounds = priceInPounds; }
public virtual BasketItem AddItem(int productId, int quantity, Pounds priceInPounds) { BasketItem basketItem; bool itemAlreadyExists = _items.Any(i => i.ProductId == productId); if (!itemAlreadyExists) { basketItem = new BasketItem(productId, quantity, priceInPounds); _items.Add(basketItem); } else { basketItem = _items.First(i => i.ProductId == productId); int newQuantity = basketItem.Quantity + quantity; basketItem.ChangeQuantity(newQuantity); } return(basketItem); }