示例#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());
        }
        public void ApplyPromotion()
        {
            //arrange
            IPromotionEngine promotionEngine = new PromotionEngine();
            var cartItems = new List <CartItem> {
                new CartItem()
                {
                    UnitId = "A", UnitCount = 1, UnitPrice = 50
                },
                new CartItem()
                {
                    UnitId = "B", UnitCount = 1, UnitPrice = 30
                },
                new CartItem()
                {
                    UnitId = "C", UnitCount = 1, UnitPrice = 20
                },
            };
            decimal expectedResult = 100;

            //act
            var result = promotionEngine.ApplyPromotion(cartItems);

            //assert
            Assert.AreEqual(expectedResult, result);
        }
示例#3
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());
        }
示例#4
0
        public void TestAddNewPromotionRule()
        {
            PromotionEngine promotionEngine     = new PromotionEngine();
            CartManager     promotedCartManager = new CartManager(new PromotionEngine());
            Product         product1            = new Product()
            {
                Id = 1, Name = "A", Price = 50
            };
            Product product2 = new Product()
            {
                Id = 2, Name = "B", Price = 30
            };
            Product product3 = new Product()
            {
                Id = 3, Name = "C", Price = 20
            };
            Product product4 = new Product()
            {
                Id = 4, Name = "D", Price = 15
            };

            Cart cart = new Cart()
            {
                Id = 1
            };

            cart.AddItems(new CartItem()
            {
                Product = product1, Quantity = 1
            });
            cart.AddItems(new CartItem()
            {
                Product = product2, Quantity = 1
            });
            cart.AddItems(new CartItem()
            {
                Product = product3, Quantity = 1
            });
            int expected = 100;
            int actual;

            actual = promotionEngine.ApplyPromotion(cart);
            Assert.AreEqual(expected, actual);
        }
示例#5
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();
        }