示例#1
0
        static void Main(string[] args)
        {
            Category category1 = new Category("category1");
            Category category2 = new Category("category2");


            Product product1 = new Product("Product1", 100, category1);
            Product product2 = new Product("Product2", 50, category1);
            Product product3 = new Product("Product3", 40, category2);



            ICampaign rateCampaign   = new RateCampaign(20.0, 5, category1);
            ICampaign amountCampaing = new AmountCampaign(10.0, category2);

            IShoppingCart shoppingcart = new ShoppingCart(new DeliveryCost(1.5, 10));

            shoppingcart.AddProduct(product1, 12);
            shoppingcart.AddProduct(product2, 10);
            shoppingcart.AddProduct(product3, 11);

            List <ICampaign> campaigns = new List <ICampaign>();

            campaigns.Add(rateCampaign);
            campaigns.Add(amountCampaing);
            ICoupon rateCoupon = new RateCoupon(5.0, 3);

            shoppingcart.AddCampaign(campaigns);
            shoppingcart.AddCoupon(rateCoupon);
            double totalAmount = shoppingcart.GetTotalAmountAfterDiscounts();

            System.Console.WriteLine(shoppingcart.Print());
            System.Console.ReadLine();
        }
示例#2
0
        public void Get_Campaign_Discount_With_Rate_Campaign_Should_Return_The_Percentage_Of_The_Specified_Rate_Of_The_Applied_Campaign()
        {
            var calc = new Mock <IDeliveryCostCalculator>();

            Type       type         = typeof(Core.ShoppingCart);
            var        shoppingCart = Activator.CreateInstance(type, calc.Object);
            MethodInfo method       = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
                                      .Where(x => x.Name == "GetCampaignDiscount" && x.IsPrivate).First();

            var tShirtCategory = new Category("TShirt");
            var poloTShirt     = new Product("Polo TShirt", 200, tShirtCategory);

            ((Core.ShoppingCart)shoppingCart).AddItem(poloTShirt);

            var campaigns = new List <Discount>();

            var campaign = new RateCampaign(50, 1, tShirtCategory);

            campaigns.Add(campaign);

            ((Core.ShoppingCart)shoppingCart).ApplyDiscounts(campaigns);

            var output = method.Invoke(shoppingCart, new object[] { });

            Assert.True(Convert.ToInt32(output) == 100);
        }
示例#3
0
        public void Get_Total_Amount_After_Discounts_With_Campaign_Should_Return_Correct_Price()
        {
            var calc         = new Mock <IDeliveryCostCalculator>();
            var shoppingCart = new Core.ShoppingCart(calc.Object);

            var tShirtCategory = new Category("TShirt");
            var jacketCategory = new Category("Jacket");

            var poloTShirt    = new Product("Polo TShirt", 120, tShirtCategory);
            var kotonTShirt   = new Product("Koton TShirt", 40, tShirtCategory);
            var lacosteTShirt = new Product("Lacoste TShirt", 220, tShirtCategory);
            var zaraJacket    = new Product("Zara Jacket", 260, jacketCategory);

            shoppingCart.AddItem(poloTShirt, 2);
            shoppingCart.AddItem(kotonTShirt);
            shoppingCart.AddItem(lacosteTShirt);
            shoppingCart.AddItem(zaraJacket);

            var campaigns = new List <Discount>();

            var tShirtCampaign = new RateCampaign(20, 3, tShirtCategory);

            campaigns.Add(tShirtCampaign);

            shoppingCart.ApplyDiscounts(campaigns);

            var totalAmount = shoppingCart.GetTotalAmountAfterDiscounts();

            // ( ( (120 * 2) + 40 + 220 ) * %20 ) + 260 => 400 + 260 => 660

            Assert.True(totalAmount == 660);
        }
示例#4
0
        public void It_should_apply_discounts_first_then_coupons()
        {
            //Arrange
            Category category = new Category("food");

            var campaign1 = new RateCampaign(category, 40, 1);
            var campaign2 = new AmountCampaign(category, 50.0, 1);

            var coupon = new AmountCoupon(100, 60);

            var     cart  = new Cart();
            Product apple = new Product("Elma", 200, category);
            Product armut = new Product("Armut", 500, category);

            cart.AddItem(apple, 5);
            cart.AddItem(armut, 2);

            //Act
            cart.ApplyCoupon(coupon);
            cart.ApplyDiscounts(campaign1, campaign2);

            //Verify

            cart.RawTotal.Should().Be(2000);
            cart.CampaignDiscount.Should().Be(800);
            cart.TotalAmountAfterDiscounts.Should().Be(1140);
        }
示例#5
0
        public void CantUseCampaign_If_TotalAmount_Less_Than_CouponMinimumAmount()
        {
            RateCampaign rateCampaign = new RateCampaign(1, 5, 150, 3);
            Cart         cart         = new Cart();
            bool         result       = rateCampaign.IsUsable(cart);

            Assert.False(result);
        }
示例#6
0
        public void It_should_be_Defined_With_Rate_and_minimum_amount_Type_Discount()
        {
            //Arrange
            int discountRate         = 60;
            var minimumPurchaseCount = 5;
            var category             = new Category("food");

            //Act
            var campaign = new RateCampaign(category, discountRate, minimumPurchaseCount);

            //Verify
            // todo: verify
        }
示例#7
0
        public void GetDiscount_WhenCalledForAmountCampaign_ReturnsPercentageOfDiscount()
        {
            //Arrange
            double          discountRate     = 10;
            double          totalPrice       = 200;
            double          expectedDiscount = 20;
            Category        category         = new Category("Food");
            CampaignFactory factory          = new CampaignFactory();
            RateCampaign    campaign         = factory.ProduceCampaign(category, discountRate, 3, DiscountType.Rate) as RateCampaign;

            //Act
            double discount = campaign.GetDiscount(totalPrice);

            //Assert
            Assert.Equal(expectedDiscount, discount);
        }
示例#8
0
        static void Main()
        {
            try
            {
                var tShirtCategory      = new Category("TShirt");
                var sportTShirtCategory = new Category("Sport TShirt", tShirtCategory);
                var jacketCategory      = new Category("Jacket");

                var campaigns = new List <Discount>();

                var tShirtCampaign = new RateCampaign(20, 4, tShirtCategory);

                campaigns.Add(tShirtCampaign);

                var coupon = new AmountCoupon(50, 300);

                var poloTShirt    = new Product("Polo TShirt", 120, tShirtCategory);
                var kotonTShirt   = new Product("Koton TShirt", 40, tShirtCategory);
                var lacosteTShirt = new Product("Lacoste TShirt", 220, tShirtCategory);

                var hummelTShirt        = new Product("Hummel Sport TShirt", 120, sportTShirtCategory);
                var premiumHummelTShirt = new Product("Premium Hummel Sport TShirt", 500, sportTShirtCategory);

                var zaraJacket = new Product("Zara Jacket", 260, jacketCategory);

                var cart = new Core.ShoppingCart(new DeliveryCostCalculator(3.5, 2));

                cart.AddItem(poloTShirt, 1);
                cart.AddItem(hummelTShirt, 1);
                cart.AddItem(premiumHummelTShirt);
                cart.AddItem(kotonTShirt);
                cart.AddItem(lacosteTShirt);

                cart.AddItem(zaraJacket);

                cart.ApplyDiscounts(campaigns);
                cart.ApplyCoupon(coupon);

                Console.WriteLine(cart.Print());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
示例#9
0
        public void It_should_not_apply_rate_discounts_when_minimum_purchase_not_met()
        {
            //Arrange
            Category category  = new Category("food");
            var      campaign1 = new RateCampaign(category, 40, 101);

            var     cart  = new Cart();
            Product apple = new Product("Elma", 20, category);

            cart.AddItem(apple, 5);

            //Act
            cart.ApplyDiscounts(campaign1);

            //Verify
            cart.RawTotal.Should().Be(100);
            cart.TotalAmountAfterDiscounts.Should().Be(100);
        }
示例#10
0
        public void Apply_Discounts_With_A_Campaign_That_Does_Not_Satisfy_Minimum_Product_Quantity_Should_Not_Add_The_Discount_To_The_Cart()
        {
            var calc         = new Mock <IDeliveryCostCalculator>();
            var shoppingCart = new Core.ShoppingCart(calc.Object);

            var tShirtCategory = new Category("TShirt");
            var poloTShirt     = new Product("Polo TShirt", 120, tShirtCategory);

            var campaigns = new List <Discount>();

            shoppingCart.AddItem(poloTShirt);

            var tShirtCampaign = new RateCampaign(20, 2, tShirtCategory);

            campaigns.Add(tShirtCampaign);

            shoppingCart.ApplyDiscounts(campaigns);

            Assert.Null(shoppingCart.Campaign);
        }
示例#11
0
        static void Run(ICart cart, IDeliveryCostCalculator deliveryCostCalculator)
        {
            ICategory categoryConsumerElectronics = new Category("Consumer Electronics");
            IProduct  productAppleWatch           = new Product(categoryConsumerElectronics, "Apple Watch", 2000.0);
            IProduct  productAppleMacbookPro      = new Product(categoryConsumerElectronics, "Apple Macbook Pro", 10000.0);

            ICategory categoryToysAndGames = new Category("Toys & Games");
            IProduct  productLego          = new Product(categoryToysAndGames, "Lego Classic Pack", 100.0);

            cart.Add(productAppleWatch, 2);
            cart.Add(productAppleMacbookPro, 1);
            cart.Add(productLego, 10);

            ICollection <ICampaign> campaigns = new Collection <ICampaign>();
            ICollection <ICoupon>   coupons   = new Collection <ICoupon>();

            IRateCampaign   inapplicableRateCampaign = new RateCampaign(categoryToysAndGames, "80% discount on Toys & Games category", .5, true, true, 50000.0, 20);
            IAmountCampaign lowAmountCampaign        = new AmountCampaign(categoryConsumerElectronics, "100 TL discount on Toys & Games category", 100, false, false, 0.0, 0);
            IAmountCampaign bestAmountCampaign       = new AmountCampaign(categoryConsumerElectronics, "1400 TL discount on Toys & Games category", 1400, false, true, 0.0, 3);
            IAmountCoupon   inapplicableAmountCoupon = new AmountCoupon(5000, "5000 TL discount coupon", true, true, 50000, 50);
            IRateCoupon     lowRateCoupon            = new RateCoupon(.1, "10% discount coupon", false, false, 0.0, 0);
            IRateCoupon     bestRateCoupon           = new RateCoupon(.5, "50% discount coupon", true, true, 10000, 13);

            campaigns.Add(inapplicableRateCampaign);
            campaigns.Add(lowAmountCampaign);
            campaigns.Add(bestAmountCampaign);
            coupons.Add(inapplicableAmountCoupon);
            coupons.Add(lowRateCoupon);
            coupons.Add(bestRateCoupon);

            cart.ApplyDiscounts(campaigns, coupons);

            cart.DeliveryCost = deliveryCostCalculator.Calculate(cart);

            var response = cart.Print();

            Console.Write(response);
        }
示例#12
0
        public void It_should_apply_discounts()
        {
            //Arrange
            Category category = new Category("food");

            var campaign1 = new RateCampaign(category, 40, 1);
            var campaign2 = new AmountCampaign(category, 50.0, 1);
            var campaign3 = new AmountCampaign(category, 5.0, 1);

            var     cart  = new Cart();
            Product apple = new Product("Elma", 20, category);
            Product armut = new Product("Armut", 50, category);

            cart.AddItem(apple, 5);
            cart.AddItem(armut, 2);

            //Act
            cart.ApplyDiscounts(campaign1, campaign2, campaign3);

            //Verify
            cart.RawTotal.Should().Be(200);
            cart.TotalAmountAfterDiscounts.Should().Be(120);
        }
示例#13
0
        public void Apply_Discounts_Should_Add_A_Discount_To_The_Cart_Which_Has_The_Maximum_Discount_Among_All_Campaigns()
        {
            var calc         = new Mock <IDeliveryCostCalculator>();
            var shoppingCart = new Core.ShoppingCart(calc.Object);

            var tShirtCategory = new Category("TShirt");
            var poloTShirt     = new Product("Polo TShirt", 100, tShirtCategory);
            var kotonTShirt    = new Product("Koton TShirt", 40, tShirtCategory);

            var campaigns = new List <Discount>();

            shoppingCart.AddItem(poloTShirt, 2);
            shoppingCart.AddItem(kotonTShirt, 5);

            var tShirtCampaign  = new RateCampaign(10, 3, tShirtCategory);
            var tShirtCampaign2 = new AmountCampaign(100, 5, tShirtCategory);

            campaigns.Add(tShirtCampaign);
            campaigns.Add(tShirtCampaign2);

            shoppingCart.ApplyDiscounts(campaigns);

            Assert.True(shoppingCart.Campaign == tShirtCampaign2);
        }