public void CalculateFor_ShouldBeZero_EmptyCart()
        {
            var shoppingCart = new Mock <IShoppingCart>();

            shoppingCart.Setup(sc => sc.GetNumberOfDeliveries()).Returns(0);
            shoppingCart.Setup(sc => sc.GetNumberOfProducts()).Returns(0);

            Assert.Zero(_deliveryCostCalculator.CalculateFor(shoppingCart.Object));
        }
Beispiel #2
0
        private void SetupData()
        {
            //cart printer sorts products by category. so we need predefined to prevent flaky tests
            var categoryID1 = Guid.Parse("BA38E701-024B-4968-ABED-510742481F0D");
            var category1   = new Category(categoryID1, Guid.NewGuid(), "Category 1");

            var categoryID2 = Guid.Parse("FD843460-7343-4C32-A2A8-493678AABA63");
            var category2   = new Category(categoryID2, Guid.NewGuid(), "Category 2");

            cart = new Cart(Guid.NewGuid());
            cart.AddItem(new Product(Guid.NewGuid(), "Title A", 100m, categoryID1), 3);
            cart.AddItem(new Product(Guid.NewGuid(), "Title B", 40m, categoryID2), 2);

            var campaign = new Campaign(Guid.NewGuid(), categoryID1, 1, DiscountType.Amount, 10m);

            cart.ApplyCampaign(campaign);

            var coupon = new Coupon(Guid.NewGuid(), 20m, DiscountType.Amount, 20m);

            cart.ApplyCoupon(coupon);

            var deliveryCostCalculator = new DeliveryCostCalculator(10m, 5m);
            var deliveryCost           = deliveryCostCalculator.CalculateFor(cart);

            cart.SetDeliveryCost(deliveryCost);

            categoryReader.Setup(r => r.GetByIDs(new List <Guid> {
                categoryID1, categoryID2
            }))
            .Returns(new List <Category> {
                category1, category2
            });

            printer = new CartPrinter(categoryReader.Object);
        }
Beispiel #3
0
        public void Calculate_Delivery_Cost()
        {
            var cart = BuildShoppingCart();
            var deliveryCostCalculator = new DeliveryCostCalculator(2, 3, 2.99);

            Assert.Equal(18.99, deliveryCostCalculator.CalculateFor(cart));
        }
Beispiel #4
0
        public void CreateCart()
        {
            Product apple  = new Product("Apple", 100.0, food);
            Product almond = new Product("Almonds", 150.0, food);


            cart.AddItem(apple, 3);
            cart.AddItem(almond, 1);


            // discounts rules can be 20% on a category if bought more than 3 items
            Campaign campaign1 = new Campaign(food, 20.0, DiscountType.Rate, 3);
            // another campaign rule 50% on category if bought more than 5 items
            Campaign campaign2 = new Campaign(food, 50.0, DiscountType.Rate, 5);
            // another campaign rule 5 TL amount discount on a category if bought more than 7 items
            Campaign campaign3 = new Campaign(food, 5.0, DiscountType.Amount, 7);

            // Cart should apply the maximum amount of discount to the cart.
            cart.ApplyDiscounts(campaign1, campaign2, campaign3);


            Coupon coupon = new Coupon(100, 10, DiscountType.Rate);

            cart.ApplyCoupon(coupon);

            double fixedCost = 2.99;

            //i didn't understand costPerDelivery and costPerProduct value in documention that's why assign static value
            int costPerDelivery = 10;
            int costPerProduct  = 20;

            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost);

            deliveryCostCalculator.CalculateFor(cart);
        }
        public void CalculateFor_Ensure_Calculate_For_Cart_Returns_True()
        {
            var deliveryCostCalculator = new DeliveryCostCalculator(_costPerDelivery, _costPerProduct, _fixedCost);

            // assert
            Assert.AreEqual(deliveryCostCalculator.CalculateFor(_cart), 6.79);
        }
        public double GetDeliveryCost()
        {
            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(8, 9);
            double deliveryCost = deliveryCostCalculator.CalculateFor(Cart);

            return(deliveryCost);
        }
Beispiel #7
0
        public void CalculateFor_ShouldReturnFixedCost_When_CartIsEmpty()
        {
            calculator = new DeliveryCostCalculator(5, 10);
            shoppingCart.Setup(cart => cart.GetNumberOfDeliveries()).Returns(0);
            shoppingCart.Setup(cart => cart.GetNumberOfProducts()).Returns(0);

            Assert.Equal(CostConstants.FixedRate, calculator.CalculateFor(shoppingCart.Object));
        }
        public void CalculateFor_CartWithNoProduct_ReturnsFixedCost()
        {
            deliveryCostCalculator = new DeliveryCostCalculator(5, 10);
            cart.Setup(m => m.GetNumberOfDeliveries()).Returns(0);
            cart.Setup(m => m.GetNumberOfProducts()).Returns(0);

            Assert.That(deliveryCostCalculator.CalculateFor(cart.Object) == 2.99);
        }
        public void Test_NoProducts()
        {
            deliveryCostCalculator = new DeliveryCostCalculator(3, 7);
            shoppingCart.Setup(m => m.GetNumberOfDeliveries()).Returns(0);
            shoppingCart.Setup(m => m.GetNumberOfProducts()).Returns(0);

            Assert.That(deliveryCostCalculator.CalculateFor(shoppingCart.Object) == 39);
        }
        public void CalculateFor_ShouldReturnExpectedCorrectDeliveryCalculation_WhenGivenCosts(double costPerDelivery, double costPerProduct, double fixedCost, double expected)
        {
            _deliveryCostCalculator = new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost);
            _shoppingCart.Setup(p => p.GetNumberOfDeliveries()).Returns(2);
            _shoppingCart.Setup(p => p.GetNumberOfProducts()).Returns(3);
            var result = _deliveryCostCalculator.CalculateFor(_shoppingCart.Object);

            Assert.True(expected == result);
        }
Beispiel #11
0
        /// <summary>
        /// Calculate and returns delivery cost of product
        /// </summary>
        /// <returns>delivery cost of products</returns>
        public double GetDeliveryCost()
        {
            const double costPerDelivery        = 1.20;
            const double costPerProduct         = 1.30;
            const double fixedCost              = 2.99;
            var          deliveryCostCalculator = new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost);

            return(deliveryCostCalculator.CalculateFor(_shoppingCart));
        }
Beispiel #12
0
        public void Test_Calculate_For(double costPerDelivery, double costPerProduct, double fixedCost, Cart cart, double expectedCalculatedCost)
        {
            var deliveryCostCalculator = new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost);

            var calculatedCost = deliveryCostCalculator.CalculateFor(cart);

            //calculatedCost.ShouldBe(expectedCalculatedCost);
            throw new NotImplementedException(); //not implemented.Tests cases are not true, needs to be calculated
        }
Beispiel #13
0
        public void CalculateFor_ShouldReturnValidCost_When_CartHasOneDeliveryAndOneProduct()
        {
            calculator = new DeliveryCostCalculator(5, 10);
            shoppingCart.Setup(cart => cart.GetNumberOfDeliveries()).Returns(1);
            shoppingCart.Setup(cart => cart.GetNumberOfProducts()).Returns(1);

            double expected = (5 * 1) + (10 * 1) + CostConstants.FixedRate;

            Assert.Equal(expected, calculator.CalculateFor(shoppingCart.Object));
        }
        public void CalculateFor_CartWithOneDeliveryOneProduct_ReturnsValidCalculation()
        {
            deliveryCostCalculator = new DeliveryCostCalculator(5, 10);
            cart.Setup(m => m.GetNumberOfDeliveries()).Returns(1);
            cart.Setup(m => m.GetNumberOfProducts()).Returns(1);

            double expected = (5 * 1) + (10 * 1) + 2.99;

            Assert.That(deliveryCostCalculator.CalculateFor(cart.Object) == expected);
        }
Beispiel #15
0
        public void CalculateFor_WhenCalledWithNullParameter_ThrowsException()
        {
            //Arrange
            DeliveryCostCalculator calculator = new DeliveryCostCalculator(1, 2, 2.99d);

            //Act && Assert
            var exception = Assert.Throws <InvalidOperationException>(() => calculator.CalculateFor(null));

            Assert.Equal("There is no cart!", exception.Message);
        }
        public void Should_Calculate()
        {
            DeliveryCostCalculator = new DeliveryCostCalculator(5, 10);

            var cart = new Cart(DeliveryCostCalculator);



            DeliveryCostCalculator.CalculateFor(cart).ShouldBe(2.99);
        }
        public void Calculate_For_With_Empty_Cart_Should_Return_Zero()
        {
            var shoppingCart = new Mock <IShoppingCart>();

            shoppingCart.Setup(cart => cart.NumberOfDeliveries).Returns(0);
            shoppingCart.Setup(cart => cart.NumberOfProducts).Returns(0);

            var deliveryCostCalculator = new DeliveryCostCalculator(3.5, 2);

            Assert.True(deliveryCostCalculator.CalculateFor(shoppingCart.Object) == 0);
        }
Beispiel #18
0
        public void CalculateFor_WhenCalledWithEmptyCart_ThrowsException()
        {
            //Arrange
            DeliveryCostCalculator calculator = new DeliveryCostCalculator(1, 2, 2.99d);
            Cart shoppingCart = new Cart(calculator);

            //Act && Assert
            var exception = Assert.Throws <InvalidOperationException>(() => calculator.CalculateFor(shoppingCart));

            Assert.Equal("Shopping Cart is empty!", exception.Message);
        }
Beispiel #19
0
        public void DeliveryCostCalculatorShouldReturnFixedCostWhenCartHasNoItems()
        {
            Mock <IShoppingCart> cart = new Mock <IShoppingCart>();

            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(10.0, 5.0, 2.99);

            cart.Setup(x => x.GetTotalItemAmount()).Returns(0);
            cart.Setup(x => x.GetTotalDeliveryAmount()).Returns(0);

            Assert.Equal(2.99, deliveryCostCalculator.CalculateFor(cart.Object));
        }
Beispiel #20
0
        public void CalculateFor_CartWithNoProduct_ReturnsFixedCost()
        {
            Mock <IShoppingCart> cart = new Mock <IShoppingCart>();

            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(10.0, 5.0, 2.99);

            cart.Setup(x => x.GetTotalItemAmount()).Returns(2);
            cart.Setup(x => x.GetTotalDeliveryAmount()).Returns(1);

            Assert.Equal(22.99, deliveryCostCalculator.CalculateFor(cart.Object), 2);
        }
        public void Calculate_For_With_Custom_Price_Should_Return_Correct_Result()
        {
            var shoppingCart = new Mock <IShoppingCart>();

            shoppingCart.Setup(cart => cart.NumberOfDeliveries).Returns(2);
            shoppingCart.Setup(cart => cart.NumberOfProducts).Returns(3);

            var deliveryCostCalculator = new DeliveryCostCalculator(3.5, 2, 4);

            // (3.5 * 2) + (3 * 2) + 4 Custom Price
            Assert.True(deliveryCostCalculator.CalculateFor(shoppingCart.Object) == 17);
        }
        public void delivery_cost_should_be_calculated_correctly()
        {
            var cart = new Cart(Guid.NewGuid());

            cart.AddItem(new Product(Guid.NewGuid(), "TitleA", 43m, Guid.NewGuid()), 1);
            cart.AddItem(new Product(Guid.NewGuid(), "TitleB", 17m, Guid.NewGuid()), 1);

            var calculator = new DeliveryCostCalculator(3m, 2m);

            var expectedCost = 12.99m;
            var cost         = calculator.CalculateFor(cart);

            cost.Should().Be(expectedCost);
        }
Beispiel #23
0
        public string Print()
        {
            //This business can be moved in a different object or structure but there is no request to make that new object reusable/extensible etc. Can be implemented here for now.

            //Structure => <categoryTitle , products in this category>
            var groupedProductsByCategory = CartItems
                                            .GroupBy(x => x.Value.Product.Category.Title)
                                            .ToDictionary(x => x.Key, x => x.ToList());

            var builder = new StringBuilder();

            builder.AppendLine($"{FormatInfoField("Category Name")}" +
                               $"{FormatInfoField("Product Name")}" +
                               $"{FormatInfoField("Quantity")}" +
                               $"{FormatInfoField("Unit Price")}" +
                               $"{FormatInfoField("TotalPrice")}" +
                               $"{FormatInfoField("Campaign Discount")}");

            if (groupedProductsByCategory.Any())
            {
                foreach (var categoryGroup in groupedProductsByCategory)
                {
                    var shoppingCartItems = categoryGroup.Value.Select(x => x.Value);
                    foreach (var shoppingCartItem in shoppingCartItems)
                    {
                        builder.AppendLine($"{FormatInfoField(categoryGroup.Key)}" +
                                           $"{FormatInfoField(shoppingCartItem.Product.Title)}" +
                                           $"{FormatInfoField(shoppingCartItem.OrderQuantity.ToCurrencyString())}" +
                                           $"{FormatInfoField(shoppingCartItem.Product.UnitPrice.ToCurrencyString())}" +
                                           $"{FormatInfoField(shoppingCartItem.TotalPrice.ToCurrencyString())}" +
                                           $"{FormatInfoField(shoppingCartItem.BestCampaignDiscount.DiscountAmount.ToCurrencyString())}");
                        //In request, its wanted but we cannot add coupon discount here because a coupon can be applied on cart - not on each shopping item
                    }
                }
            }
            else
            {
                builder.AppendLine($"{"No item in the cart.",80}");
            }

            builder.AppendLine($"{Environment.NewLine}" +
                               $"{FormatFooterField("Cart Total Amount", GetTotalAmount(), true)}" +
                               $"{FormatFooterField("Cart Net Amount", GetTotalAmountAfterDiscounts(), true)}" +
                               $"{FormatFooterField("Campaign Discounts", GetCampaignDiscount(), true)}" +
                               $"{FormatFooterField("Coupon Discount", GetCouponDiscount(), true)}" +
                               $"{FormatFooterField("Delivery Cost", DeliveryCostCalculator.CalculateFor(this), false)}");

            return(builder.ToString());
        }
Beispiel #24
0
        static void Main(string[] args)
        {
            // Sample creating a new category
            Category food = new Category("food");

            // Products
            Product apple  = new Product("Apple", 100.0, food);
            Product almond = new Product("Almonds", 150.0, food);


            // Products can be added to shopping cart with quantity
            ShoppingCart cart = new ShoppingCart();

            cart.AddItem(apple, 3);
            cart.AddItem(almond, 1);


            // discounts rules can be 20% on a category if bought more than 3 items
            Campaign campaign1 = new Campaign(food, 20.0, DiscountType.Rate, 3);
            // another campaign rule 50% on category if bought more than 5 items
            Campaign campaign2 = new Campaign(food, 50.0, DiscountType.Rate, 5);
            // another campaign rule 5 TL amount discount on a category if bought more than 7 items
            Campaign campaign3 = new Campaign(food, 5.0, DiscountType.Amount, 7);

            // Cart should apply the maximum amount of discount to the cart.
            cart.ApplyDiscounts(campaign1, campaign2, campaign3);


            Coupon coupon = new Coupon(100, 10, DiscountType.Rate);

            cart.ApplyCoupon(coupon);

            double fixedCost = 2.99;

            //i didn't understand costPerDelivery and costPerProduct value in documention that's why assign static value
            int costPerDelivery = 10;
            int costPerProduct  = 20;

            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(costPerDelivery, costPerProduct, fixedCost);

            deliveryCostCalculator.CalculateFor(cart);

            Console.WriteLine("Total Amount After Discount => " + cart.GetTotalAmountAfterDiscounts());
            Console.WriteLine("Coupon Discount => " + cart.GetCouponDiscount());
            Console.WriteLine("Campaign Discount =>" + cart.GetCampaignDiscount());
            Console.WriteLine("Delivery Cost =>" + cart.GetDeliveryCost());

            cart.Print();
        }
Beispiel #25
0
        public void CalculateFor_WhenCalledWithShoppingCart_CalculatesTheDeliveryCostForCart()
        {
            //Arrange
            Category category = new Category("Food");
            Product  banana   = new Product("Banana", 10, category);
            DeliveryCostCalculator calculator = new DeliveryCostCalculator(1, 2, 2.99d);
            Cart shoppingCart = new Cart(calculator);

            shoppingCart.AddItem(banana, 5);

            //Act
            double deliveryCost = calculator.CalculateFor(shoppingCart);

            //Assert
            Assert.Equal(5.99d, deliveryCost);
        }
Beispiel #26
0
        static void Main(string[] args)
        {
            Category food = new Category("food");

            Product apple  = new Product("Apple", 100.5, food);
            Product almond = new Product("Almonds", 150.0, food);

            ShoppingCart cart = new ShoppingCart();

            cart.AddItem(apple, 3);
            cart.AddItem(almond, 1);

            Campaign campaign1 = new Campaign(food, 20.0, 3, DiscountType.Rate);
            Campaign campaign2 = new Campaign(food, 50.0, 5, DiscountType.Rate);
            Campaign campaign3 = new Campaign(food, 5.0, 5, DiscountType.Amount);

            cart.ApplyDiscounts(campaign1, campaign2, campaign3);

            // Firstly apply campaings after that apply coupons
            Coupon coupon = new Coupon(100, 10, DiscountType.Rate);

            cart.ApplyCoupon(coupon);

            // Delivery part
            // Formula = (CostPerDelivery * NumberOfDeliveries) + (CostPerProduct * NumberOfProducts) + FixedCost
            // FixedCost = 2.99
            // NumberOfDeliveries = # of distinct categories in the cart
            // NumberOfProducts = # of different products in the cart, not the quantity of products
            DeliveryCostCalculator deliveryCostCalculator = new DeliveryCostCalculator(10.0, 5.0, 2.99);

            // Double
            deliveryCostCalculator.CalculateFor(cart);

            cart.GetTotalAmountAfterDiscounts();
            cart.GetCampaignDiscount();
            cart.GetCouponDiscount();
            cart.GetDeliveryCost();

            // Group products by Category and Print the CategoryName, ProductName, Quantity, Unit Price, Total Price, Total Discount
            cart.Print();
        }
Beispiel #27
0
 public double GetDeliveryCost()
 {
     return(_deliveryCostCalculator.CalculateFor(this));
 }
Beispiel #28
0
        private void SetDeliveryCost(Cart cart)
        {
            var cost = deliveryCostCalculator.CalculateFor(cart);

            cart.SetDeliveryCost(cost);
        }
 public void CalculateFor_ShouldArgumentNullException_WhenCartIsNull()
 {
     _deliveryCostCalculator = new DeliveryCostCalculator(12, 8, 2.99);
     Assert.Throws <ArgumentNullException>(() => _deliveryCostCalculator.CalculateFor(null));
 }
Beispiel #30
0
 public void CalculateFor_NullCart_Should_RaiseException()
 {
     calculator = new DeliveryCostCalculator(5, 10);
     Assert.Throws <ArgumentNullException>(() => calculator.CalculateFor(null));
 }