private double ApplyStrategy(IPromotionStrategy strategy, Item item)
        {
            switch (item.ItemID)
            {
            case "A":
                _discountQuantity = 3;
                _price
                    = _price
                      + ((item.UnitPrice * item.OrderItems)
                         - ((item.OrderItems / _discountQuantity) * strategy.GetPromotionDiscount()));

                break;

            case "B":
                _discountQuantity = 2;
                _price
                    = _price
                      + ((item.UnitPrice * item.OrderItems)
                         - ((item.OrderItems / _discountQuantity) * strategy.GetPromotionDiscount()));
                break;

            case "C":
            case "D":
                _price = _price + ((item.UnitPrice * item.OrderItems));
                break;

            case "CD":
                _discountQuantity = item.OrderItems;
                _price            = _price - (_discountQuantity * strategy.GetPromotionDiscount());
                break;
            }

            return(_price);
        }
 public void Setup()
 {
     _promotionStrategy   = new ComboOffer();
     _productWithoutOffer = new List <ProductCheckout>()
     {
         new ProductCheckout()
         {
             ProductCode = "C", Quantity = 1, DefaultPrice = 20
         }
     };
     _productWithOffer = new List <ProductCheckout>()
     {
         new ProductCheckout()
         {
             ProductCode = "C", Quantity = 1, DefaultPrice = 20
         }, new ProductCheckout()
         {
             ProductCode = "D", Quantity = 1, DefaultPrice = 15
         }
     };
     _promotions = new List <Promotion>()
     {
         new Promotion()
         {
             Type = "Single", ProductCode = "A", Price = 130, Quantity = 3
         }, new Promotion()
         {
             Type = "Single", ProductCode = "B", Price = 45, Quantity = 2
         }, new Promotion()
         {
             Type = "Combo", ProductCode = "C;D", Price = 30, Quantity = 3
         }
     };
 }
 public void Setup()
 {
     _promotionStrategy = new AdditionalItemOffer();
     _productWithOffer  = new ProductCheckout()
     {
         ProductCode = "A", Quantity = 3, DefaultPrice = 50
     };
     _productWithOfferExtra = new ProductCheckout()
     {
         ProductCode = "A", Quantity = 4, DefaultPrice = 50
     };
     _productWithoutOffer = new ProductCheckout()
     {
         ProductCode = "A", Quantity = 2, DefaultPrice = 50
     };
     _promotions = new List <Promotion>()
     {
         new Promotion()
         {
             Type = "Single", ProductCode = "A", Price = 130, Quantity = 3
         }, new Promotion()
         {
             Type = "Single", ProductCode = "B", Price = 45, Quantity = 2
         }, new Promotion()
         {
             Type = "Combo", ProductCode = "C;D", Price = 30, Quantity = 3
         }
     };
 }
示例#4
0
        public void GetQuntityStrategyResultForOneItem()
        {
            var cartList = new List <CartWithPromotionType>();

            cartList.Add(new CartWithPromotionType()
            {
                cart = new Cart()
                {
                    Quantity = 1,
                    SkuId    = 'A'
                },
                promotionType = PromotionTypes.Quantity
            });
            promotionStrategy = new QuantityPromotion(query);
            var result = promotionStrategy.ApplyPromotion(cartList);

            Assert.AreEqual(50, result);
        }
示例#5
0
        public void GetComboStrategyResultOnlyFirtOfCombo()
        {
            var cartList = new List <CartWithPromotionType>();

            cartList.Add(new CartWithPromotionType()
            {
                cart = new Cart()
                {
                    Quantity = 1,
                    SkuId    = 'C'
                },
                promotionType = PromotionTypes.Combo
            });
            promotionStrategy = new ComboPromotion(query);


            var result = promotionStrategy.ApplyPromotion(cartList);

            Assert.AreEqual(20, result);
        }
            public override decimal CalculateSumPrice(Guid?couponId)
            {
                decimal             price = 0;
                List <ShopCartItem> items = new List <ShopCartItem>();    //todo:: 从数据源获取购物车items

                foreach (var item in items)
                {
                    //计算每一项购物车item的价格
                    var isChecked = true;     //todo::检查是否选中状态的购物车项
                    if (isChecked)
                    {
                        PromotionType type   = PromotionType.NonePromotion; //todo:: 从item中获取促销类型
                        Guid          itemId = Guid.Empty;                  //todo:: 从item中获取id

                        IPromotionStrategy strategy = PromotionStrategyCreater.Factory(type);
                        price += strategy.CalculatePromotionPrice(itemId);
                    }
                }

                return(price);
            }
        public double GetCheckoutPrice()
        {
            Item itemCD  = new Item();
            int  countC  = 0;
            int  countD  = 0;
            int  countCD = 0;
            IPromotionStrategy strategy   = null;
            double             finalprice = 0;


            foreach (Item item in _items)
            {
                if (item.ItemID == "C")
                {
                    countC = countC + item.OrderItems;
                }
                if (item.ItemID == "D")
                {
                    countD = countD + item.OrderItems;
                }
                strategy   = this.GetStrategy(item.OrderItems, item.ItemID);
                finalprice = this.ApplyStrategy(strategy, item);
            }

            //Special logic for C & D
            if (countC > 0 && countD > 0)
            {
                countCD = (countC > countD) ? countD : countC;
                itemCD  = new Item()
                {
                    ItemID = "CD", UnitPrice = 35, OrderItems = countCD
                };
                strategy   = this.GetStrategy(itemCD.OrderItems, itemCD.ItemID);
                finalprice = this.ApplyStrategy(strategy, itemCD);
            }

            return(finalprice);
        }
 public PromotionContext(IPromotionStrategy promotionStrategy)
 {
     _promotionStrategy = promotionStrategy;
 }
 public void SetStrategy(IPromotionStrategy promotionStrategy)
 {
     _promotionStrategy = promotionStrategy;
 }
示例#10
0
 public PromotionEngine(Dictionary <SKU, double> priceList, IPromotionStrategy promotionStrategy)
 {
     _priceList = priceList;
     this._promotionStrategy = promotionStrategy;
 }