示例#1
0
        public decimal Total()
        {
            var basketTotal = 0.0M;
            var basketItems = BasketCache.GetBasketContent();

            foreach (KeyValuePair <int, int> item in basketItems)
            {
                var inventoryItem        = InventoryCache.GetInventoryItemById(item.Key);
                var itemQuantityInBasket = item.Value;
                if (inventoryItem != null)
                {
                    if (inventoryItem.AvailableDeal != null)
                    {
                        if (inventoryItem.AvailableDeal.RequiredQuantity <= itemQuantityInBasket)
                        {
                            // Find out how many times this deal could be applied.
                            int timesApplyDiscount = itemQuantityInBasket / inventoryItem.AvailableDeal.RequiredQuantity;
                            // How many single items left.
                            int remainingSingleItems = itemQuantityInBasket - (inventoryItem.AvailableDeal.RequiredQuantity * timesApplyDiscount);

                            basketTotal += (timesApplyDiscount * inventoryItem.AvailableDeal.DiscountedPrice) + (remainingSingleItems * inventoryItem.Price);
                        }
                        else
                        {
                            basketTotal += itemQuantityInBasket * inventoryItem.Price;
                        }
                    }
                    else
                    {
                        basketTotal += itemQuantityInBasket * inventoryItem.Price;
                    }
                }
            }
            return(basketTotal);
        }
示例#2
0
        public void CheckoutSingle_Success(string itemName, decimal expectedResult)
        {
            BasketCache.ClearAddToBasketCahce();
            var itemFromInventory = InventoryCache.GetInventoryContent().Where(i => i.Value.Name.ToLower() == itemName.ToLower()).FirstOrDefault();

            Assert.IsNotNull(itemFromInventory, "Item does not exist in inventory");

            _checkoutService.Scan(itemFromInventory.Value);

            Assert.IsNotNull(BasketCache.GetBasketContent()[itemFromInventory.Value.Id], "Item does not exist in inventory");

            var total = _checkoutService.Total();

            Assert.AreEqual(expectedResult, total);
        }
 public BasketController(IConfiguration configuration, BasketCache cache)
 {
     // _connectionString = configuration.GetConnectionString ("DefaultConnection");
     _cache = cache;
 }
示例#4
0
 public void Scan(Item item)
 {
     BasketCache.AddToBasketCahce(item);
 }