Esempio n. 1
0
        public void Run()
        {
            Category electronic = new Category(CategoryType.Electronic);
            Category book       = new Category(CategoryType.Books);

            Product television      = new Product("Television", 1000, electronic);
            Product essentialCSharp = new Product("TestBook", 400, book);

            _shoppingCart.AddProduct(television, 6);
            _shoppingCart.AddProduct(essentialCSharp, 5);


            Coupon coupon = CouponFactory.GenerateCoupon(1000, 20, CouponType.Rate);
            // I have created 2 discounts type for electronic and book categories.
            // the expectation is to find the best discount option in the same category.
            Campaign campaign3 = CampaignFactory.GenerateCampaign(electronic, 5, 50, DiscountType.Rate);
            Campaign campaign  = CampaignFactory.GenerateCampaign(electronic, 2, 20, DiscountType.Rate);

            Campaign campaign2 = CampaignFactory.GenerateCampaign(book, 2, 5, DiscountType.Amount);
            Campaign campaign4 = CampaignFactory.GenerateCampaign(book, 4, 15, DiscountType.Amount);

            Campaign[] campaigns = { campaign2, campaign, campaign3, campaign4 };

            _shoppingCart.ApplyCampaigns(campaigns);
            _shoppingCart.ApplyCoupon(coupon);
            _shoppingCart.Print();

            var deliveryCost            = _costCalculator.CalculateCost(50, 2, _shoppingCart);
            var totalCartAmount         = _shoppingCart.GetTotalAmount();
            var appliedCampaignDiscount = _shoppingCart.GetCampaignDiscount();
            var appliedCouponDiscount   = _shoppingCart.GetCouponDiscount();

            WriteSummaryText(deliveryCost, totalCartAmount, appliedCampaignDiscount, appliedCouponDiscount);
        }
        public void ApplyDiscount_ShouldNotDescreaseTotalAmountValue_IfCartIsEmpty()
        {
            ShoppingCart shoppingCart = new ShoppingCart();
            Coupon       coupon       = CouponFactory.GenerateCoupon(-1, 20, DiscountType.Amount);

            coupon.ApplyDiscount(shoppingCart);

            Assert.Equal(0, shoppingCart.DiscountedTotalAmount);
        }
Esempio n. 3
0
        public void ProduceCoupon_WhenCalledForRateCoupon_ReturnsRateCouponObject()
        {
            //Arrange
            CouponFactory factory = new CouponFactory();

            //Act
            ICoupon coupon = factory.ProduceCoupon(50, 10, DiscountType.Rate);

            //Assert
            Assert.IsType <RateCoupon>(coupon);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public void Run()
        {
            #region Get Delivery cost values from config.

            var costPerDelivery = _config.GetValue <double>("Delivery:CostPerDelivery");
            var costPerProduct  = _config.GetValue <double>("Delivery:CostPerProduct");
            var fixedCost       = _config.GetValue <double>("Delivery:FixedCost");

            #endregion

            #region Category and product creation

            var computerCategory = new Category.Category("Computer");
            var phoneCategory    = new Category.Category("Phone");

            var ipone   = new Iphone(7500, phoneCategory);
            var macbook = new Macbook(18500, computerCategory);

            #endregion

            #region Shopping cart creation

            IShoppingCart cart = new Cart.ShoppingCart(new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost));
            cart.AddItem(ipone, 10);
            cart.AddItem(macbook, 5);

            #endregion

            #region Get campaign type from CampaignFactory with CampaignTypes enum.

            CampaignFactory campaignFactory = new CampaignFactory();
            var             campaign2       = campaignFactory.Get(computerCategory, 1500, 1, CampaignTypes.Amount);
            var             campaign3       = campaignFactory.Get(phoneCategory, 2, 1, CampaignTypes.Rate);

            #endregion

            #region Apply first campaign discount then coupon

            cart.ApplyDiscounts(campaign2, campaign3);

            CouponFactory couponFactory = new CouponFactory();
            var           coupon        = couponFactory.Get(500, 150, CouponTypes.Amount);

            cart.ApplyCoupon(coupon);

            #endregion

            Console.WriteLine(cart.Print());

            Console.ReadLine();
        }
Esempio n. 6
0
        public void ApplyCoupon_ShouldApplyDiscount_IfMinimumAmountSatisfied(DiscountType discountType, decimal expected)
        {
            Category     electronicCategory = new Category(CategoryType.Electronic);
            Category     foodCategory       = new Category(CategoryType.Food);
            Product      television         = new Product("Television", 1000, electronicCategory);
            Product      apple        = new Product("Apple", 5, foodCategory);
            ShoppingCart shoppingCart = new ShoppingCart();
            Coupon       coupon       = CouponFactory.GenerateCoupon(4000, 20, discountType);

            shoppingCart.AddProduct(television, 4);
            shoppingCart.AddProduct(apple, 200);
            shoppingCart.ApplyCoupon(coupon);

            Assert.Equal(expected, shoppingCart.DiscountedTotalAmount);
        }
Esempio n. 7
0
        public void GetDiscount_WhenCalledForRateCoupon_ReturnsPercentageOfDiscount()
        {
            //Arrange
            double        discountRate     = 10;
            double        givenAmount      = 200;
            double        expectedDiscount = 20;
            CouponFactory factory          = new CouponFactory();
            RateCoupon    coupon           = factory.ProduceCoupon(50, discountRate, DiscountType.Rate) as RateCoupon;

            //Act
            double discount = coupon.GetDiscount(givenAmount);

            //Assert
            Assert.Equal(expectedDiscount, discount);
        }
Esempio n. 8
0
        public void Run()
        {
            Category electronic = new Category(CategoryType.Electronic);
            Category book       = new Category(CategoryType.Books);

            Product television      = new Product("Television", 1000, electronic);
            Product essentialCSharp = new Product("EssentialCSharp", 400, book);

            _shoppingCart.AddProduct(television, 5);
            _shoppingCart.AddProduct(essentialCSharp, 1);


            Coupon   coupon   = CouponFactory.GenerateCoupon(1000, 20, DiscountType.Rate);
            Campaign campaign = CampaignFactory.GenerateCampaign(electronic, 2, 10, DiscountType.Amount);


            _shoppingCart.ApplyCampaigns(campaign);
            _shoppingCart.Print();

            var deliveryCost    = _costCalculator.CalculateCost(50, 2, _shoppingCart);
            var totalCartAmount = _shoppingCart.GetTotalAmount();

            WriteClosingText(deliveryCost, totalCartAmount);
        }