public void BuyTwoGetOneFree(decimal price, int count, int expectedCount, decimal expectedPrice) { var product = new ProductEntity { Price = price }; var record = new BasketRecordEntity { Count = count, Product = product }; var basket = new BasketEntity { Records = new[] { record } }; var percentageDiscount = new DiscountEntity { Type = DiscountEntityType.BuyTwoGetOneForFree, TargetProduct = product, RewardProduct = product, }; var dbPrice = _sut.CalculatePrice(basket); var basketModel = _sut.CalculatePriceWithDiscounts(basket, new[] { percentageDiscount }); var basketModelPrice = _sut.CalculatePrice(basketModel); var basketModelProductCount = basketModel.Records[0].Count + basketModel.Records[0].FreeProducts.Count; dbPrice.Should().Be(price * record.Count); basketModelPrice.Should().Be(expectedPrice); basketModelProductCount.Should().Be(expectedCount); }
public void Percentage(decimal price, decimal percentage, decimal expected) { var product = new ProductEntity { Price = price }; var record = new BasketRecordEntity { Count = 1, Product = product }; var basket = new BasketEntity { Records = new[] { record } }; var percentageDiscount = new DiscountEntity { Type = DiscountEntityType.Percentage, Percentage = percentage, TargetProduct = product }; var dbPrice = _sut.CalculatePrice(basket); var basketModel = _sut.CalculatePriceWithDiscounts(basket, new[] { percentageDiscount }); var basketModelPrice = _sut.CalculatePrice(basketModel); dbPrice.Should().Be(price); basketModelPrice.Should().Be(expected); }
public BasketRecordModel Apply(BasketRecordEntity record, DiscountEntity entity) { var discountAmount = record.Product.Price * entity.Percentage / 100; record.Product.Price -= discountAmount; return(new BasketRecordModel(record.Count, new ProductModel(record.Product.Price))); }
public BasketRecordModel Apply(BasketRecordEntity record, DiscountEntity entity) { var recordModel = new BasketRecordModel(record.Count, new ProductModel(record.Product.Price)); var amountOfItemsToDiscount = (int)Math.Floor(record.Count / 2f); for (int i = 0; i < amountOfItemsToDiscount; i++) { recordModel.FreeProducts.Add(new ProductModel(entity.RewardProduct.Price)); } return(recordModel); }
public bool IsApplicable(BasketRecordEntity record, DiscountEntity entity) { return(record.Product.Id == entity.TargetProduct.Id); }