Skip to content

Instantly share code, notes, and snippets.

@pavsaund
Created October 19, 2016 11:40
Show Gist options
  • Save pavsaund/4027e5dedc348c14bcf09bc99fcab62f to your computer and use it in GitHub Desktop.
Save pavsaund/4027e5dedc348c14bcf09bc99fcab62f to your computer and use it in GitHub Desktop.
public class ShoppingCart
{
public IList<CartItem> Items { get; set; }
public decimal CartTotal { get { return Items.Sum(item => item.Total); } }
public void AddCartItemAndDoTracking(CartItem item)
{
AddOrIncreaseQuantityFor(item);
TrackForLoggingDebuggingAndCustomerServiceCenter(CustomerAction.CartItemAddedToCart);
}
private void AddOrIncreaseQuantityFor(CartItem addedItem)
{
if (CartContainsStockKeepingUnit(addedItem.StockKeepingUnit))
{
var itemInCart = Items.First(inCart => inCart.StockKeepingUnit == addedItem.StockKeepingUnit);
itemInCart.Quantity += addedItem.Quantity;
}
else
{
Items.Add(item);
}
}
bool CartContainsStockKeepingUnit(StockKeepingUnit sku)
{
return Items.Any(x => x.Sku == sku)
}
void TrackForLoggingDebuggingAndCustomerServiceCenter(Action action)
{
CustomerTrackingService.SignalTotalInShoppingCartUpdated(action, CartTotal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment