Created
October 19, 2016 11:40
-
-
Save pavsaund/4027e5dedc348c14bcf09bc99fcab62f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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