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_Total_Amount_After_Discounts_With_Coupon_Should_Return_Correct_Price() { var calc = new Mock <IDeliveryCostCalculator>(); var shoppingCart = new Core.ShoppingCart(calc.Object); var couponDiscountAmount = 50; 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 coupon = new AmountCoupon(couponDiscountAmount, 300); shoppingCart.ApplyDiscounts(campaigns); shoppingCart.ApplyCoupon(coupon); var totalAmount = shoppingCart.GetTotalAmountAfterDiscounts(); // ( (120 * 2) + 40 + 220 ) + 260 => 760 - 50 => 710 Assert.True(totalAmount == shoppingCart.TotalItemPrice - couponDiscountAmount); }
public void ApplicableCoupon_InapplicableCampaign() { ICart cart = new ShoppingCart.Core.Cart.Services.Cart(); cart.Add(this.product1Category1, 1); cart.Add(this.product2Category1, 5); cart.Add(this.product3Category2, 10); ICollection <ICampaign> campaigns = new Collection <ICampaign>() { new AmountCampaign(this.category1, "", 100.0, true, false, double.MaxValue, 0), new RateCampaign(this.category1, "", 0.5, false, true, 0, int.MaxValue) }; IAmountCoupon applicableCoupon = new AmountCoupon(100, "", true, true, 1000, 1); ICollection <ICoupon> coupons = new Collection <ICoupon>() { applicableCoupon, 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.PricePerProduct * x.Quantity), cart.TotalAmountAfterDiscounts); Assert.Equal(cart.TotalAmount - cart.TotalCampaignDiscountAmount, cart.TotalAmountAfterCampaignDiscount); Assert.Equal(0, cart.TotalCampaignDiscountAmount); Assert.Equal(applicableCoupon.DiscountAmount, cart.TotalCouponDiscountAmount); Assert.Null(cart.AppliedCampaign); Assert.Equal(applicableCoupon, cart.AppliedCoupon); }
public void GetTotalAmountTest() { AmountCoupon rateCoupon = new AmountCoupon("AmountTest", 150, 77); Cart cart = new Cart(); Product product = new Product(3, "testProduct", 100, 5, 99, ""); cart.AddItem(product, 2); double totalAmount = cart.GetTotalAmount(); Assert.Equal(200, totalAmount); }
public void Apply_Coupon_Should_Add_The_Coupon_To_The_Cart() { var calc = new Mock <IDeliveryCostCalculator>(); var shoppingCart = new Core.ShoppingCart(calc.Object); var coupon = new AmountCoupon(50, 300); shoppingCart.ApplyCoupon(coupon); Assert.NotNull(shoppingCart.Coupon); }
public void GetDiscount_WhenCalledForAmountCoupon_ReturnsDiscount() { //Arrange CouponFactory factory = new CouponFactory(); AmountCoupon coupon = factory.ProduceCoupon(50, 10, DiscountType.Amount) as AmountCoupon; //Act double discount = coupon.GetDiscount(120); //Assert Assert.Equal(coupon.Discount, discount); }
public void It_should_create_coupon_with_amount_and_minimum_amount() { //Arrange double minimumCartAmount = 100; double amountOfDiscount = 10; //Act var discountType = DiscountType.Amount; var coupon = new AmountCoupon(minimumCartAmount, amountOfDiscount); //Verify coupon.DiscountType.Should().Be(discountType); }
public void Apply_Coupon_Should_Replace_The_Previous_Coupon_In_The_Cart() { var calc = new Mock <IDeliveryCostCalculator>(); var shoppingCart = new Core.ShoppingCart(calc.Object); var coupon = new AmountCoupon(50, 300); shoppingCart.ApplyCoupon(coupon); var coupon2 = new AmountCoupon(50, 200); shoppingCart.ApplyCoupon(coupon2); Assert.True(shoppingCart.Coupon.MinimumAmountToApply == 200); }
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(); }
public void UseAmountCoupon() { AmountCoupon rateCoupon = new AmountCoupon("AmountTest", 150, 77); Cart cart = new Cart(); Product product = new Product(3, "testProduct", 100, 5, 99, ""); cart.AddItem(product, 2); bool result = cart.ApplyCoupon(rateCoupon); Assert.True(result); double totalAmountAfterDiscounts = cart.GetTotalAmountAfterDiscounts(); Assert.Equal(123, totalAmountAfterDiscounts); double totalCouponDiscount = cart.GetCouponDiscount(); Assert.Equal(77, totalCouponDiscount); }
public void Get_Coupon_Discount_With_Total_Price_Is_Smaller_Than_Minimum_Amount_Should_Return_Zero() { var calc = new Mock <IDeliveryCostCalculator>(); Type type = typeof(Core.ShoppingCart); var shoppingCart = Activator.CreateInstance(type, calc.Object); var coupon = new AmountCoupon(50, 300); ((Core.ShoppingCart)shoppingCart).ApplyCoupon(coupon); MethodInfo method = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) .Where(x => x.Name == "GetCouponDiscount" && x.IsPrivate).First(); var output = method.Invoke(shoppingCart, new object[] { 120 }); Assert.True(Convert.ToInt32(output) == 0); }
public void Get_Coupon_Discount_With_Amount_Coupon_Should_Return_The_Discount_Amount_Of_The_Applied_Coupon() { var calc = new Mock <IDeliveryCostCalculator>(); Type type = typeof(Core.ShoppingCart); var shoppingCart = Activator.CreateInstance(type, calc.Object); int discountAmount = 150; var coupon = new AmountCoupon(discountAmount, 600); ((Core.ShoppingCart)shoppingCart).ApplyCoupon(coupon); MethodInfo method = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) .Where(x => x.Name == "GetCouponDiscount" && x.IsPrivate).First(); var output = method.Invoke(shoppingCart, new object[] { 600 }); Assert.True(Convert.ToInt32(output) == discountAmount); }
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 CreateCoupon_Success(string couponName) { var coupon = new AmountCoupon(couponName, 2, 2); Assert.IsTrue(!string.IsNullOrEmpty(coupon.Description)); }