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(); }
static void Main(string[] args) { Category category = new Category("Food"); Product banana = new Product("Banana", 10, category); Product apple = new Product("Apple", 15, category); Category electronicCategory = new Category("Electronic"); Product samsung = new Product("Samsung", 200, electronicCategory); Product nokia = new Product("Nokia", 100, electronicCategory); CampaignFactory campaignFactory = new CampaignFactory(); AmountCampaign campaignFood = campaignFactory.ProduceCampaign(category, 25, 5, DiscountType.Amount) as AmountCampaign; AmountCampaign campaignElectronic = campaignFactory.ProduceCampaign(electronicCategory, 50, 1, DiscountType.Amount) as AmountCampaign; DeliveryCostCalculator calculator = new DeliveryCostCalculator(1, 2, 2.99d); Cart shoppingCart = new Cart(calculator); shoppingCart.AddItem(banana, 8); shoppingCart.AddItem(apple, 5); shoppingCart.AddItem(samsung, 1); shoppingCart.AddItem(nokia, 2); shoppingCart.ApplyDiscounts(campaignFood, campaignElectronic); shoppingCart.Print(); }
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); }
public void Get_Campaign_Discount_With_Amount_Campaign_Should_Return_The_Discount_Amount_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", 120, tShirtCategory); ((Core.ShoppingCart)shoppingCart).AddItem(poloTShirt); var campaigns = new List <Discount>(); var campaign = new AmountCampaign(50, 1, tShirtCategory); campaigns.Add(campaign); ((Core.ShoppingCart)shoppingCart).ApplyDiscounts(campaigns); var output = method.Invoke(shoppingCart, new object[] { }); Assert.True(Convert.ToInt32(output) == 50); }
public void ApplicableCampaign_InapplicableCoupon() { ICart cart = new ShoppingCart.Core.Cart.Services.Cart(); cart.Add(this.product1Category1, 1); cart.Add(this.product2Category1, 5); cart.Add(this.product3Category2, 10); IAmountCampaign applicableCampaign = new AmountCampaign(this.category1, "", 100.0, true, false, 1000, 0); ICollection <ICampaign> campaigns = new Collection <ICampaign>() { applicableCampaign, new RateCampaign(this.category1, "", 0.5, false, true, 0, int.MaxValue) }; ICollection <ICoupon> coupons = new Collection <ICoupon>() { new AmountCoupon(1000, "", true, false, double.MaxValue, 0), new RateCoupon(.5, "", false, true, 0, int.MaxValue) }; cart.ApplyDiscounts(campaigns, coupons); Assert.NotEqual(cart.Items.Sum(x => x.TotalPrice), cart.TotalAmount); Assert.NotEqual(cart.Items.Sum(x => x.PricePerProduct * x.Quantity), cart.TotalAmount); Assert.Equal(cart.Items.Sum(x => x.TotalPrice), cart.TotalAmountAfterCampaignDiscount); Assert.Equal(cart.Items.Sum(x => x.PricePerProduct * x.Quantity), cart.TotalAmountAfterDiscounts); Assert.Equal(cart.TotalAmount - cart.TotalCampaignDiscountAmount, cart.TotalAmountAfterCampaignDiscount); Assert.Equal(cart.TotalAmount - cart.TotalCampaignDiscountAmount, cart.TotalAmountAfterDiscounts); Assert.Equal(applicableCampaign.DiscountAmount, cart.TotalCampaignDiscountAmount); Assert.Equal(0, cart.TotalCouponDiscountAmount); Assert.Equal(applicableCampaign, cart.AppliedCampaign); Assert.Null(cart.AppliedCoupon); }
public void AddCampaign_WhenNewCampaignCreated_AddThatCampaignToCategory() { //Arrange & Act Category foodCategory = new Category("Food"); CampaignFactory campaignFactory = new CampaignFactory(); AmountCampaign campaign = campaignFactory.ProduceCampaign(foodCategory, 10, 2, Business.DiscountType.Amount) as AmountCampaign; //Assert Assert.Contains(campaign, foodCategory.campaigns); }
public void It_should_be_Defined_With_Amount_and_minimum_amount_Type_Discount() { //Arrange double amountOfDiscount = 60; var minimumPurchaseCount = 5; var category = new Category("food"); //Act var campaign = new AmountCampaign(category, amountOfDiscount, minimumPurchaseCount); //Verify // todo: verify }
public void GetDiscount_WhenCalledForAmountCampaign_ReturnsDiscount() { //Arrange double discountAmount = 10; double totalPrice = 200; double expectedDiscount = 10; Category category = new Category("Food"); CampaignFactory factory = new CampaignFactory(); AmountCampaign campaign = factory.ProduceCampaign(category, 20, 3, DiscountType.Amount) as AmountCampaign; //Act double discount = campaign.GetDiscount(totalPrice); //Assert Assert.Equal(expectedDiscount, discountAmount); }
public void It_should_not_apply_amount_discounts_when_minimum_discount_not_met() { //Arrange double amountToBeDiscounted = 49; Category category = new Category("food"); var campaign1 = new AmountCampaign(category, amountToBeDiscounted, 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(cart.RawTotal); }
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); }
public void It_should_apply_discount_more_than_category_total_amount() { //Arrange Category category = new Category("food"); Category electronics = new Category("electronics"); var electronicsAmountCampaign = new AmountCampaign(electronics, 580.0, 1); var cart = new Cart(); var apple = new Product("Elma", 200, category); var laptop = new Product("Laptop", 500, electronics); cart.AddItem(apple, 5); cart.AddItem(laptop, 1); //Act cart.ApplyDiscounts(electronicsAmountCampaign); //Verify cart.RawTotal.Should().Be(1500); cart.CampaignDiscount.Should().Be(500); cart.TotalAmountAfterDiscounts.Should().Be(1000); }
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); }
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); }
public void It_should_apply_amount_discounts() { //Arrange double amountToBeDiscounted = 49; Category category = new Category("food"); int minimumPurcahsedItemCount = 4; var campaign1 = new AmountCampaign(category, amountToBeDiscounted, minimumPurcahsedItemCount); 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); var expectedTotalAfterCampaignApplied = cart.RawTotal - amountToBeDiscounted; cart.TotalAmountAfterDiscounts.Should().Be(expectedTotalAfterCampaignApplied); }