Esempio n. 1
0
        public void ComputeBillScenarioCTest()
        {
            Item objItemA = new Item()
            {
                ItemId = 1, ItemName = "A", Description = "Test Item A", ItemPrice = 50
            };
            Item objItemB = new Item()
            {
                ItemId = 2, ItemName = "B", Description = "Test Item B", ItemPrice = 30
            };
            Item objItemC = new Item()
            {
                ItemId = 3, ItemName = "C", Description = "Test Item C", ItemPrice = 20
            };
            Item objItemD = new Item()
            {
                ItemId = 4, ItemName = "D", Description = "Test Item D", ItemPrice = 15
            };

            KartItem objKartItem1 = new KartItem(objItemA, 3);
            KartItem objKartItem2 = new KartItem(objItemB, 5);
            KartItem objKartItem3 = new KartItem(objItemC, 1);
            KartItem objKartItem4 = new KartItem(objItemD, 1);

            List <KartItem> objPurchasedItemList = new List <KartItem>();

            objPurchasedItemList.Add(objKartItem1);
            objPurchasedItemList.Add(objKartItem2);
            objPurchasedItemList.Add(objKartItem3);
            objPurchasedItemList.Add(objKartItem4);

            Kart objKart = new Kart(objPurchasedItemList);

            //Now setup the Promotions Rules
            List <KeyValuePair <Item, int> > kvpList = new List <KeyValuePair <Item, int> >();

            kvpList.Add(new KeyValuePair <Item, int>(objItemC, 1));
            kvpList.Add(new KeyValuePair <Item, int>(objItemD, 1));

            IPromotionRule objRuleA = new BuyNQtyAtFixedPrice("Promotion A", 130, 3, "A");
            IPromotionRule objRuleB = new BuyNQtyAtFixedPrice("Promotion B", 45, 2, "B");
            IPromotionRule objRuleC = new BuyNItemsAtFixedPrice("Combination C & D", 30, kvpList);

            //Creating instance of RuleEngine
            IPromotion objPromotion = new PromotionEngine();

            objPromotion.ApplyPromotion(objRuleA, objKart, true);
            objPromotion.ApplyPromotion(objRuleB, objKart, true);
            objPromotion.ApplyPromotion(objRuleC, objKart);

            Assert.AreEqual(280, objKart.CalculateAmountPayable());
        }
Esempio n. 2
0
        public void ComputeBillScenarioBTest()
        {
            Item objItemA = new Item()
            {
                ItemId = 1, ItemName = "A", Description = "Test Item A", ItemPrice = 50
            };
            Item objItemB = new Item()
            {
                ItemId = 2, ItemName = "B", Description = "Test Item B", ItemPrice = 30
            };
            Item objItemC = new Item()
            {
                ItemId = 3, ItemName = "C", Description = "Test Item C", ItemPrice = 20
            };

            KartItem objKartItem1 = new KartItem(objItemA, 5);
            KartItem objKartItem2 = new KartItem(objItemB, 5);
            KartItem objKartItem3 = new KartItem(objItemC, 1);

            List <KartItem> objPurchasedItemList = new List <KartItem>();

            objPurchasedItemList.Add(objKartItem1);
            objPurchasedItemList.Add(objKartItem2);
            objPurchasedItemList.Add(objKartItem3);

            Kart objKart = new Kart(objPurchasedItemList);

            IPromotionRule objRuleA = new BuyNQtyAtFixedPrice("Promotion A", 130, 3, "A");
            IPromotionRule objRuleB = new BuyNQtyAtFixedPrice("Promotion B", 45, 2, "B");

            IPromotion objPromotion = new PromotionEngine();

            objPromotion.ApplyPromotion(objRuleA, objKart, true);
            objPromotion.ApplyPromotion(objRuleB, objKart, true);

            Assert.AreEqual(370, objKart.CalculateAmountPayable());
        }
 public void AddItem(KartItem item)
 {
     Items.Add(KartItem);
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Item objItemA = new Item()
            {
                ItemId = 1, ItemName = "A", Description = "Test Item A", ItemPrice = 50
            };
            Item objItemB = new Item()
            {
                ItemId = 2, ItemName = "B", Description = "Test Item B", ItemPrice = 30
            };
            Item objItemC = new Item()
            {
                ItemId = 3, ItemName = "C", Description = "Test Item C", ItemPrice = 20
            };
            Item objItemD = new Item()
            {
                ItemId = 4, ItemName = "D", Description = "Test Item D", ItemPrice = 15
            };

            KartItem objKartItem1 = new KartItem(objItemA, 8);
            KartItem objKartItem2 = new KartItem(objItemB, 5);
            KartItem objKartItem3 = new KartItem(objItemC, 1);
            KartItem objKartItem4 = new KartItem(objItemD, 1);

            List <KartItem> objPurchasedItemList = new List <KartItem>();

            objPurchasedItemList.Add(objKartItem1);
            objPurchasedItemList.Add(objKartItem2);
            objPurchasedItemList.Add(objKartItem3);
            objPurchasedItemList.Add(objKartItem4);

            Kart objKart = new Kart(objPurchasedItemList);

            IPromotionRule objRule = new BuyNQtyAtFixedPrice("Promotion A", 130, 3, "A");

            IPromotion objPromotion = new PromotionEngine();

            objPromotion.ApplyPromotion(objRule, objKart);
            objRule = new BuyNQtyAtFixedPrice("Promotion B", 45, 2, "B");

            objPromotion.ApplyPromotion(objRule, objKart);

            Console.WriteLine("Total Amount Payable :" + objKart.CalculateAmountPayable());
            List <KeyValuePair <Item, int> > kvpList = new List <KeyValuePair <Item, int> >();

            kvpList.Add(new KeyValuePair <Item, int>(objItemC, 1));
            kvpList.Add(new KeyValuePair <Item, int>(objItemD, 1));

            List <KartItem> objPurchasedItemList1 = new List <KartItem>();

            KartItem objKartItem5 = new KartItem(objItemC, 2);
            KartItem objKartItem6 = new KartItem(objItemD, 1);

            objPurchasedItemList1.Add(objKartItem5);
            objPurchasedItemList1.Add(objKartItem6);

            Kart objKart1 = new Kart(objPurchasedItemList1);

            objRule = new BuyNItemsAtFixedPrice("Combination C & D", 30, kvpList);
            objPromotion.ApplyPromotion(objRule, objKart1);
            objPromotion.ApplyPromotion(objRule, objKart);
            Console.WriteLine("Total Amount Payable for Kart :" + objKart.CalculateAmountPayable());
            Console.WriteLine("Total Amount Payable :" + objKart1.CalculateAmountPayable());
            Console.WriteLine("Hello World!");

            Console.ReadLine();
        }
Esempio n. 5
0
 public void Update(KartItem kart)
 {
     throw new NotImplementedException("Not implemented");
 }
Esempio n. 6
0
 public void Add(KartItem kart)
 {
     _fakeKarts.Add(kart);
 }
Esempio n. 7
0
 public IActionResult Put([FromBody] KartItem kart)
 {
     _kartService.Update(kart);
     return(base.Ok(new NoContentResponse("Ok", "updated kart")));
 }
Esempio n. 8
0
 public IActionResult Post([FromBody] KartItem kart)
 {
     _kartService.Add(kart);
     return(base.Ok(new NoContentResponse("Ok", "created kart")));
 }
Esempio n. 9
0
        private void  ApplyPromotionForItemsCombination(IPromotionRule promotion, IItemList items)
        {
            var discountCombinationItems = promotion.GetItemCombinationList();
            var shoppingCartClone        = items.GetItemList();

            var itemsMarkedForDiscount = new List <KartItem>();

            var eligibleForDiscount = false;

            foreach (var itemsCountsCombination in discountCombinationItems)
            {
                var shoppedItem = items.GetItemList()
                                  .Where(ci => ci.ItemObj == itemsCountsCombination.Key)
                                  .Select(ci => ci)
                                  .Single();

                if (shoppedItem.Equals(new KeyValuePair <Item, int>()))
                {
                    eligibleForDiscount = false;
                    break;
                }

                if (shoppedItem.ItemQuantity >= itemsCountsCombination.Value)
                {
                    eligibleForDiscount = true;
                    itemsMarkedForDiscount.Add(new KartItem(shoppedItem.ItemObj, shoppedItem.ItemQuantity));
                    var prod = shoppedItem.ItemObj;

                    shoppingCartClone.Remove(shoppedItem);
                    var remainingCount = shoppedItem.ItemQuantity % itemsCountsCombination.Value;
                    shoppingCartClone.Add(new KartItem(prod, remainingCount));
                }
            }

            if (eligibleForDiscount)
            {
                int[] arr = new int[itemsMarkedForDiscount.Count];
                int   i   = 0;
                // We need to iterate through the collection to get the multipler for promotional KartItem
                foreach (var it in itemsMarkedForDiscount)
                {
                    arr[i] = it.ItemQuantity;
                    i++;
                }
                int multiplier = arr.Min();

                //Once we get the correct multipler, we need to correct Quantity in original Kart, so that correct amount can be calculated.
                foreach (var it in itemsMarkedForDiscount)
                {
                    var shoppedItem = items.GetItemList()
                                      .Where(ci => ci.ItemObj == it.ItemObj)
                                      .Select(ci => ci)
                                      .Single();

                    shoppedItem.ItemQuantity += it.ItemQuantity - multiplier;
                    shoppedItem.AmountPayable = shoppedItem.ItemQuantity * shoppedItem.ItemObj.ItemPrice;
                }

                //Here we are adding new KartItem for promotion items.
                double amount = promotion.PromotionAmount * multiplier;
                Item   itm    = new Item();
                itm.ItemName  = promotion.PromotionName;
                itm.ItemPrice = promotion.PromotionAmount;
                KartItem ktm = new KartItem(itm, multiplier);
                items.AddKartItem(ktm);
            }
            else //reverse and restore
            {
                itemsMarkedForDiscount.ForEach(it =>
                {
                    shoppingCartClone.Remove(shoppingCartClone.Find(p => p.ItemObj == it.ItemObj));
                    shoppingCartClone.Add(it);
                });
            }
        }