示例#1
0
        public void Given_ParcelTypeListWithCandidateForSmallDiscountAndMixedParcels_Should_ReturnTheValidDiscountList()
        {
            //Arrange
            var parcelCosts = new List <ParcelDeliveryCost>
            {
                new ParcelDeliveryCost("Parcel #1", ParcelType.Small, 8),
                new ParcelDeliveryCost("Parcel #2", ParcelType.Small, 8),
                new ParcelDeliveryCost("Parcel #3", ParcelType.Small, 8),
                new ParcelDeliveryCost("Parcel #4", ParcelType.Small, 10),
                new ParcelDeliveryCost("Parcel #7", ParcelType.Large, 20),
                new ParcelDeliveryCost("Parcel #8", ParcelType.XLarge, 25),
                new ParcelDeliveryCost("Parcel #9", ParcelType.XLarge, 25),
                new ParcelDeliveryCost("Parcel #10", ParcelType.Heavy, 50),
                new ParcelDeliveryCost("Parcel #5", ParcelType.Medium, 15),
                new ParcelDeliveryCost("Parcel #6", ParcelType.Medium, 20)
            };

            var sut = new DiscountCalculator();

            //Act
            var result = sut.CalculateDiscount(parcelCosts).ToArray();

            //Assert
            result.Should().HaveCount(2)
            .And.Contain(m => m.Discount == 8m && m.Description == "Small - Discount")
            .And.Contain(m => m.Discount == 15m && m.Description == "Mixed - Discount");
        }
        public void TypeDiscount_DiscountTrue_Test()
        {
            //1. Arange
            _calc = new DiscountCalculator();

            var beasts = new List <Beast>
            {
                new Beast
                {
                    Name  = "Koe",
                    Price = 100,
                    Type  = "Boerderij"
                },
                new Beast
                {
                    Name  = "Paard",
                    Price = 100,
                    Type  = "Boerderij"
                },
                new Beast
                {
                    Name  = "Varken",
                    Price = 100,
                    Type  = "Boerderij"
                }
            };

            //2. Act
            var result = _calc.TypeDiscount(beasts);

            //3. Assert
            Assert.AreEqual(10, result.PercentageDiscount);
        }
        public void CalculatorExecutionTest()
        {
            // Arrange

            ICanDiscountStrategy canDiscountStrategy
                = new DayOfWeekCanDiscountStrategy(DayOfWeek.Friday);

            ICalculateDiscountStrategy calculateDiscountStrategy
                = new PercentageCalculateDiscountStrategy(0.5m);


            IDiscountCalculator discountCalculator
                = new DiscountCalculator(canDiscountStrategy, calculateDiscountStrategy);

            Order order = CreateOrderWith1Product("2019-10-18", 1000);

            // Act
            Action act = () => discountCalculator.CalculateDiscount(order);

            // Asserts
            act
            .ExecutionTime()
            .Should()
            .BeLessOrEqualTo(500.Milliseconds());
        }
示例#4
0
        public Discount DateDiscount([PexAssumeUnderTest] DiscountCalculator target, DateTime date)
        {
            Discount result = target.DateDiscount(date);

            return(result);
            // TODO: add assertions to method DiscountCalculatorTest.DateDiscount(DiscountCalculator, DateTime)
        }
示例#5
0
        public void GetTotal_returns_correct_price_where_multiple_discounts_are_applied()
        {
            // Arrange
            var offers = new List <IDiscountOffer>
            {
                new FreeMilkOffer(),
                new HalfPriceBreadOffer()
            };

            var basket  = new Basket();
            var calc    = new DiscountCalculator(offers);
            var handler = new CheckoutHandler(calc);

            basket.AddItem(new Butter());
            basket.AddItem(new Butter());
            basket.AddItem(new Bread());

            for (int i = 0; i < 8; i++)
            {
                basket.AddItem(new Milk());
            }

            // Act
            var result = handler.CalculateBasketTotal(basket);

            // Assert
            Assert.Equal(9M, result);
        }
示例#6
0
        public void CalculateEmployeeDiscount( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Employee,
                StartDate    = DateTime.Now,
            };

            // items Total $100
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 50M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 50M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            // Input amount is $100, expected output is $65
            // $30 discount from 30% discount + 5$ discount for $100
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(65M, amountOwed);
        }
示例#7
0
        public void GetBestPriceForZeroBooks()
        {
            var discountCalculator = new DiscountCalculator();

            discountCalculator.GetBestPrice(null);
            Assert.AreEqual(0, discountCalculator.GetBestPrice(null));
        }
示例#8
0
        public void CalculateLargeAmountDiscount2( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Customer,
                StartDate    = new DateTime(2011, 1, 1),
            };

            // items Total $1000
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 900M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 100M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            //Input amount is $1000,
            //discount % is then $50,
            //discount amount is $50
            //output should be $900
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(900M, amountOwed);
        }
示例#9
0
        public void CalculateLargeAmountDiscountForOldCustomer( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Customer,
                StartDate    = new DateTime(2011, 1, 1),
            };

            // items Total $990
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 900M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 90M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            //Input amount is $990,
            //Discount % =  $49.5
            //Bulk discount = $45
            //output should be $895.5
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(895.5M, amountOwed);
        }
示例#10
0
        public void CalculateLargeAmountDiscount( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Customer,
                StartDate    = DateTime.Now
            };

            // items Total $990
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 900M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 90M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            //Input amount is $990, discount is then $45, output should be $945
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(945M, amountOwed);
        }
示例#11
0
        public void CalculateCustomerDiscount2( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Customer,
                StartDate    = new DateTime(2011, 1, 1),
            };

            // items Total $50
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 20M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 30M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            //Input amount is $50 (less than $100) so expected output is also $47.5
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(47.5M, amountOwed);
        }
示例#12
0
        public void CalculateAffiliateDiscount( )
        {
            var customer = new Customer
            {
                CustomerType = CustomerType.Affiliate,
                StartDate    = DateTime.Now
            };

            // items Total $100
            var items = new List <Item>
            {
                new Item {
                    ItemType = ItemType.Other, Value = 50M
                },
                new Item {
                    ItemType = ItemType.Other, Value = 50M
                }
            };

            var shoppingCart = new ShoppingCart
            {
                Customer = customer,
                Items    = items
            };

            // Input amount is $100, expected output is $85
            // $10 discount from the 10% + 5$ discount for being $100
            var calculator = new DiscountCalculator( );
            var amountOwed = calculator.CalculateDiscount(shoppingCart);

            Assert.AreEqual(85M, amountOwed);
        }
示例#13
0
        Given_ParcelTypeListWithCandidateForMediumDiscountWithNotEligibleDiscountParcels_Should_ReturnTheValidDiscountList()
        {
            //Arrange
            var parcelCosts = new List <ParcelDeliveryCost>
            {
                new ParcelDeliveryCost("Parcel #1", ParcelType.Medium, 8),
                new ParcelDeliveryCost("Parcel #2", ParcelType.Medium, 8),
                new ParcelDeliveryCost("Parcel #3", ParcelType.Medium, 8),
                new ParcelDeliveryCost("Parcel #4", ParcelType.Medium, 10),
                new ParcelDeliveryCost("Parcel #5", ParcelType.Medium, 10),
                new ParcelDeliveryCost("Parcel #6", ParcelType.Medium, 10),
                // Not Eligible Discounts
                new ParcelDeliveryCost("Parcel #7", ParcelType.Medium, 10),
                new ParcelDeliveryCost("Parcel #8", ParcelType.Medium, 10)
            };

            var sut = new DiscountCalculator();

            //Act
            var result = sut.CalculateDiscount(parcelCosts).ToArray();

            //Assert
            result.Should().HaveCount(2)
            .And.Contain(m => m.Discount == 8m && m.Description == "Medium - Discount")
            .And.Contain(m => m.Discount == 10m && m.Description == "Medium - Discount");
        }
        public void TypeDiscount_DiscountFalse_Test()
        {
            //1. Arange
            _calc = new DiscountCalculator();

            var beasts = new List <Beast>
            {
                new Beast
                {
                    Name  = "Koe",
                    Price = 100,
                    Type  = "Boerderij"
                },
                new Beast
                {
                    Name  = "Paard",
                    Price = 100,
                    Type  = "Boerderij"
                },
                new Beast
                {
                    Name  = "Hagedis",
                    Price = 200,
                    Type  = "Woestijn"
                }
            };

            //2. Act
            var result = _calc.TypeDiscount(beasts);

            //3. Assert
            Assert.IsNull(result);
        }
        public void CheckMax()
        {
            IDiscountCalculator discountCalculator1 = new DiscountCalculator(new Func <ShoppingBasket, DiscountOfProducts>((ShoppingBasket shoppingBasket) => d1));
            IDiscountCalculator discountCalculator2 = new DiscountCalculator(new Func <ShoppingBasket, DiscountOfProducts>((ShoppingBasket shoppingBasket) => d2));
            IDiscountCalculator discountCalculator3 = discountCalculator1.Max(discountCalculator2);

            Assert.AreEqual(20, discountCalculator3.CalcDiscount(shoppingBasket).Discount);
        }
示例#16
0
 public ShoppingCartService(ProductService productService,
                            ShoppingCartRepository shoppingCartRepository,
                            DiscountCalculator discountCalculator)
 {
     _productService         = productService;
     _shoppingCartRepository = shoppingCartRepository;
     _discountCalculator     = discountCalculator;
 }
示例#17
0
        public void CalculateDiscount_IfTotalSumPriceIsLessThen100_Return0()
        {
            var discountCalculator = new DiscountCalculator();

            var result = discountCalculator.CalculateDiscount(TestProducts2());

            Assert.AreEqual(0, result);
        }
 public With_all_discount_rules_applied()
 {
     _discountCalculator = new DiscountCalculator(new List <IDiscountRule>
     {
         new DiscountRuleFiveToTenItems(),
         new DiscountRuleTenToFifteenItems()
     });
 }
        public void CalculateDiscount_throws_null_argument_exception_when_passed_null_object()
        {
            // Arrrange
            var calc = new DiscountCalculator(new List <IDiscountOffer>());

            // Assert
            Assert.Throws <ArgumentNullException>(() => calc.CalculateDiscount(null));
        }
    private decimal GetDiscountLine(ICartItem[] items, out IList <decimal> discountLines)
    {
        CartItemGroup group = new CartItemGroup(items);

        DiscountCalculator cal = new DiscountCalculator();

        return(cal.GetDiscount(group, StoreContext.CheckoutDetails.Coupon, StoreContext.Customer, out discountLines));
    }
示例#21
0
 public void SetUp()
 {
     _discountCalculator = new DiscountCalculator(new List <IDiscountRule>
     {
         new DiscountRuleFiveToTenItems(),
         new DiscountRuleTenToFifteenItems()
     });
 }
示例#22
0
        protected override void TestSetUp()
        {
            IDiscountStrategy tieredDiscountStrategy = DiscountStrategyBuilder.BuildTieredStrategy()
                                                       .Where.OrdersGreaterThanOrEqualTo(100.dollars()).GetDiscountOf(10.Percent())
                                                       .Build();

            _calculator = new DiscountCalculator(tieredDiscountStrategy);
        }
        public When_calculating_a_tiered_discount()
        {
            IDiscountStrategy tieredDiscountStrategy = DiscountStrategyBuilder.BuildTieredStrategy()
                                                       .Where.OrdersGreaterThanOrEqualTo(100.dollars()).GetDiscountOf(10.Percent())
                                                       .Build();

            _calculator = new DiscountCalculator(tieredDiscountStrategy);
        }
示例#24
0
        public void calculate_tshirt_price_two_day_delivery_with_no_discount()
        {
            var tshirtPrice        = new TshirtPrice(_tshirtPriceProvider.TshirtPrice("2"));
            var discountCalculator = new DiscountCalculator(tshirtPrice, _discountProvider.Discount("None"));
            var price = discountCalculator.CalculatePrice(100);

            Assert.AreEqual(120, price);
        }
示例#25
0
        public void calculate_tshirt_price_two_day_delivery_with_studentmember_discount()
        {
            var tshirtPrice        = new TshirtPrice(_tshirtPriceProvider.TshirtPrice("2"));
            var discountCalculator = new DiscountCalculator(tshirtPrice, _discountProvider.Discount("StudentMember"));
            var price = discountCalculator.CalculatePrice(100);

            Assert.AreEqual(84, price);
        }
 public void SetUp()
 {
     _discountCalculator = new DiscountCalculator(new List<IDiscountRule>
         {
             new DiscountRuleFiveToTenItems(),
             new DiscountRuleTenToFifteenItems()
         });
 }
示例#27
0
        public void calculate_tshirt_price_one_day_delivery_with_student_discount()
        {
            var tshirtPrice        = new TshirtPrice(_tshirtPriceProvider.TshirtPrice("1"));
            var discountCalculator = new DiscountCalculator(tshirtPrice, _discountProvider.Discount("Student"));
            var price = discountCalculator.CalculatePrice(100);

            Assert.AreEqual(117, price);
        }
示例#28
0
        public void TestTotalOrder()
        {
            DiscountCalculator dc = new DiscountCalculator(new Promotion());

            Order newOrder = dc.CalculateDiscount(GetOrder());

            Assert.AreEqual(320.0, newOrder.TotalPrice, 0.001);
        }
        public ActionResult Step4()
        {
            var calc    = new DiscountCalculator();
            var booking = _boekingRepository.TempBooking;

            booking.Discounts = calc.CalculateTotalDiscount(booking.Booking);
            booking.Price     = calc.CalculateTotalPrice(booking.Booking);
            return(View(booking));
        }
示例#30
0
        public void applyCoupon(Coupon coupon)
        {
            DiscountCalculator discountCalculator = new DiscountCalculator(ShoppingCartItemList);
            double             maxCampaignAmount  = discountCalculator.GetDiscountAmount(coupon);

            Coupon                   = coupon;
            CouponDiscount           = maxCampaignAmount;
            TotalAmountAfterDiscount = TotalAmount - CouponDiscount;
        }
示例#31
0
 public void CalculateTotals()
 {
     try
     {
         if (view.CurrentWeightTicket.IsEntranceWeightTicket)
         {
             view.CurrentWeightTicket.EntranceNetWeight = view.CurrentWeightTicket.EntranceWeightKg - view.CurrentWeightTicket.ExitWeightKg;
             view.CurrentWeightTicket.ExitNetWeight     = 0;
             view.CurrentWeightTicket.NetWeight         = view.CurrentWeightTicket.EntranceNetWeight;
         }
         else
         {
             view.CurrentWeightTicket.ExitNetWeight     = view.CurrentWeightTicket.ExitWeightKg - view.CurrentWeightTicket.EntranceWeightKg;
             view.CurrentWeightTicket.EntranceNetWeight = 0;
             view.CurrentWeightTicket.NetWeight         = view.CurrentWeightTicket.ExitNetWeight;
         }
         if (view.CurrentWeightTicket.ApplyHumidity)
         {
             view.CurrentWeightTicket.HumidityDiscount = DiscountCalculator.GetDiscount(DiscountType.Humidity, view.CurrentWeightTicket.Humidity, view.CurrentWeightTicket.NetWeight, view.CurrentWeightTicket.IsEntranceWeightTicket);
         }
         else
         {
             view.CurrentWeightTicket.HumidityDiscount = 0;
         }
         if (view.CurrentWeightTicket.ApplyImpurities)
         {
             view.CurrentWeightTicket.ImpuritiesDiscount = DiscountCalculator.GetDiscount(DiscountType.Impurities, view.CurrentWeightTicket.Impurities, view.CurrentWeightTicket.NetWeight, view.CurrentWeightTicket.IsEntranceWeightTicket);
         }
         else
         {
             view.CurrentWeightTicket.ImpuritiesDiscount = 0;
         }
         view.CurrentWeightTicket.TotalWeightToPay = view.CurrentWeightTicket.NetWeight - view.CurrentWeightTicket.HumidityDiscount - view.CurrentWeightTicket.ImpuritiesDiscount;
         view.CurrentWeightTicket.SubTotal         = view.CurrentWeightTicket.Price * (decimal)view.CurrentWeightTicket.TotalWeightToPay;
         if (view.CurrentWeightTicket.ApplyDrying)
         {
             view.CurrentWeightTicket.DryingDiscount = DiscountCalculator.GetDiscount(DiscountType.Drying, view.CurrentWeightTicket.Humidity, view.CurrentWeightTicket.NetWeight, view.CurrentWeightTicket.IsEntranceWeightTicket);
         }
         else
         {
             view.CurrentWeightTicket.DryingDiscount = 0;
         }
         view.CurrentWeightTicket.TotalToPay = view.CurrentWeightTicket.SubTotal - (decimal)view.CurrentWeightTicket.DryingDiscount
                                               - (decimal)view.CurrentWeightTicket.BrokenGrainDiscount - (decimal)view.CurrentWeightTicket.CrashedGrainDiscount
                                               - (decimal)view.CurrentWeightTicket.DamagedGrainDiscount - (decimal)view.CurrentWeightTicket.SmallGrainDiscount;
         view.CurrentWeightTicket.RaiseUpdateProperties();
     }
     catch (Exception ex)
     {
         StackTrace st = new StackTrace();
         StackFrame sf = st.GetFrame(0);
         MethodBase currentMethodName = sf.GetMethod();
         Guid       errorId           = Guid.NewGuid();
         //Log error here
         view.HandleException(ex, currentMethodName.Name, errorId);
     }
 }
示例#32
0
 public decimal GetPriceOfUniqueBooks(int numberOfUniqueBooks)
 {
     var discountCalculator = new DiscountCalculator();
     switch (numberOfUniqueBooks)
     {
         case (1):
             return discountCalculator.GetDiscount(0);
         case (3):
             return discountCalculator.GetDiscount(10);
         case (4):
             return discountCalculator.GetDiscount(20);
         case (5):
             return discountCalculator.GetDiscount(25);
         default:
             return discountCalculator.GetDiscount(5);
     }
 }
示例#33
0
 public Basket(DiscountCalculator discountCalculator, TotalCalculator totalCalculator)
 {
     _discountCalculator = discountCalculator;
     _totalCalculator = totalCalculator;
 }
示例#34
0
 public Checkout(ICanTellDisplayTheTotal itemPriceTotaler, DiscountCalculator discountCalculator)
 {
     _itemPriceTotaler = itemPriceTotaler;
     _bob = discountCalculator;
 }
示例#35
0
 public Checkout(TotalCalculator totalCalculator, DiscountCalculator discountCalculator)
 {
     _basket = new Basket(discountCalculator, totalCalculator);
 }