public void PercentageDiscount_CalculateAppliedDiscount_With10PercentDiscount() { var basket = new List <ProductQuantity>(); var discounts = new List <IDiscount>(); var product = new Product() { Id = 1, Name = "Apples", Price = 100m }; var productQuantity = new ProductQuantity() { Quantity = 1, Product = product }; basket.Add(productQuantity); var discountRate = 0.10m; var percentageDiscount = new PercentageDiscount(product, discountRate); discounts.Add(percentageDiscount); var sut = new DiscountService(); // Act var result = sut.CalculateBasketDiscounts(basket, discounts); // Assert Assert.Equal(result.Any(), true); Assert.Equal(result[0].Type, DiscountType.Percentage); Assert.Equal(result[0].Amount, 10.00m); Assert.Equal(result[0].Description, "Apples 10% OFF: - 10p"); }
public static void Run() { var costs = new List <decimal> { 5000, 2000, 30000, 50000, 15000 }; var invoice = new Invoice(costs); DisplayOverrideResultInfo("Original Balance Due", invoice.GetBalanceDue()); const int twoKDiscount = 2000; var dollarDiscount = new DollarDiscount(invoice, twoKDiscount); DisplayOverrideResultInfo(" After 2K Discount", dollarDiscount.GetBalanceDue()); const decimal fivePercent = 0.05m; var percentageDiscount = new PercentageDiscount(dollarDiscount, fivePercent); DisplayOverrideResultInfo(" After 5 Percent Off", percentageDiscount.GetBalanceDue()); const int ninetyKCap = 90000; var cap = new Cap(percentageDiscount, ninetyKCap); DisplayOverrideResultInfo(" After 90K Cap", cap.GetBalanceDue()); var overdueFee = new OverdueFee(cap); DisplayOverrideResultInfo(" After Overdue Fee", overdueFee.GetBalanceDue()); }
public void PercentageDiscountStrategy_WhenPrice100and10Percent_Then90() { const decimal expectedResult = 90; // Arrange var percentageDiscount = new PercentageDiscount(10); // Act var result = new Sale(percentageDiscount, 100).GetTotal(); // Assert Assert.That(expectedResult, Is.EqualTo(result)); }
public void TestCalculatePercentageDiscount() { Product product = new Product("Apples", 2, UnitType.bag); IDiscount discount = new PercentageDiscount("prodId", 20); string message = ""; decimal discountValue = discount.CalculateDiscount(product, out message); Assert.AreEqual(0.4m, discountValue); Console.WriteLine(message); Assert.AreEqual("Apples 20% off: -40p", message); }
public void PercentageDiscount_CalculateAppliedDiscount_With50PercentDiscount() { // Arrange var percentageDiscount = new PercentageDiscount(DiscountHelper.CreateDiscountedProducts(), 0.50m); // Act var result = percentageDiscount.DiscountsApplicable(ProductQuantityHelper.CreateProducts()).ToArray(); // Assert Assert.AreEqual(result.Any(), true); Assert.AreEqual(result[0].Type, DiscountType.Percentage); Assert.AreEqual(result[0].Amount, 0.16m); Assert.AreEqual(result[0].Text, "Apples 50% OFF: - 16p"); }
public void PercentageDiscount_NoDiscountApplied_With10PercentDiscount() { // Arrange var percentageDiscount = new PercentageDiscount(new DiscountedProduct { ProductId = 4, Name = "Milk" }, 0.10m); // Act var result = percentageDiscount.DiscountsApplicable(ProductQuantityHelper.CreateProducts()).ToArray(); // Assert Assert.AreEqual(result.Any(), false); }
internal void AddNewMedicine() { if (DisountType == "FixedAmountDiscount") { Discount = new FixedAmountDiscount { Amount = AmountOrPercentage }; } else { Discount = new PercentageDiscount { Amount = AmountOrPercentage }; } if (Url == null) { Url = "no-image.png"; } try { var medicine = new Medicine { Name = Name, Description = Description, Image = new MedicineImage { Url = Url }, Price = Price, PriceDiscount = Discount }; _medicineService.AddANewMedicine(medicine); foreach (string s in CategoriesId) { int CatId = Convert.ToInt32(s); _medicineService.AddMedicineCategory(CatId, medicine); } Notification = new NotificationModel("Success!", "Category added successfully.", Notificationtype.Success); } catch (InvalidOperationException iex) { Notification = new NotificationModel("Failed!", "Failed to add category, please provide valid name.", Notificationtype.Fail); } }
public ActionResult Create([Bind(Include = "BeginDate,EndDate,DiscountType,Value,Items")] DiscountViewModel discount) { if (ModelState.IsValid) { if (discount.Items != null) { discount.ItemIds = discount.Items.Split(',').Select(x => int.Parse(x)).ToList(); } Discount discountToCreate = null; if (discount.DiscountType == DiscountType.Absolute) { discountToCreate = new AbsoluteDiscount() { AbsoluteValue = (int)discount.Value, BeginDate = discount.BeginDate, EndDate = discount.EndDate }; _logger.InfoFormat("Create absolute [{0}] discount for items [{1}]. Begin date : [{2}]. End date : [{3}].", discount.Value, discount.Items, discount.BeginDate.ToString(), discount.EndDate.ToString()); } else { discountToCreate = new PercentageDiscount() { Percentage = discount.Value, BeginDate = discount.BeginDate, EndDate = discount.EndDate }; _logger.InfoFormat("Create percentage [{0}] discount for items [{1}]. Begin date : [{2}]. End date : [{3}].", discount.Value, discount.Items, discount.BeginDate.ToString(), discount.EndDate.ToString()); } _discountManagementService.CreateDiscount(discountToCreate, discount.ItemIds); _logger.InfoFormat("Discount sucessfully created"); return(RedirectToAction("Index")); } var view = View(discount); view.ViewData["Categories"] = _categoryService.GetAllCategories(); view.ViewData["ItemsWithNoCategory"] = _itemQueryService.GetAllItems().Where(x => x.CategoryId == null).ToList(); return(view); }
public void Given_Cart_When_NoDiscountedProductFound_Then_NoDiscountApplies() { // Arrange var items = new List <CartItem> { new CartItem(new Product("Milk", 1.0m), 1), new CartItem(new Product("Tomato", 0.50m), 1) }; var sut = new PercentageDiscount("Apples", 10, new DateTime(2018, 11, 19)); // Act var result = sut.Apply(items); // Assert Assert.Empty(result); }
public void Given_Cart_When_DiscountExpires_Then_RemoveDiscount() { var expires = Substitute.For <IDateTime>(new DateTime(2018, 11, 19)); var items = new List <CartItem> { new CartItem(new Product("Milk", 1.0m), 1), new CartItem(new Product("Tomato", 0.50m), 1), new CartItem(new Product("Apples", 1.0m), 1) }; var sut = new PercentageDiscount("Apples", 10, new DateTime(2018, 11, 19)); // Act var result = sut.Apply(items); // Assert Assert.Equal(expires, result); }
private void CreateDiscounts() { Product apples = Store.GetProductByName("apples"); if (apples != null) { IDiscount applesDiscount = new PercentageDiscount(apples.Id, 10); Store.AddDiscount(applesDiscount); } Product beans = Store.GetProductByName("beans"); Product bread = Store.GetProductByName("bread"); DiscountRule breadDiscountRule = new DiscountRule(beans.Id, 2); if (beans != null && bread != null) { IDiscount breadDiscount = new MixtPercentageDiscount(bread.Id, 50, breadDiscountRule); Store.AddDiscount(breadDiscount); } }
public async Task <List <IDiscount> > GetProductDiscounts() { var result = new List <IDiscount>(); await Task.Delay(2); var productDiscont1 = new PercentageDiscount(GetProductById(4), 0.10m); result.Add(productDiscont1); var productDiscont2 = new HalfPriceDiscount(GetProductById(2), new ProductQuantity() { Product = GetProductById(1), Quantity = 2 }); result.Add(productDiscont2); return(result); }
public void Given_Cart_When_DiscountedProductFound_Then_ApplyDiscount(int quantity, decimal expectedDiscount, string expectedDescription) { // Arrange var items = new List <CartItem> { new CartItem(new Product("Milk", 1.0m), 1), new CartItem(new Product("Tomato", 0.50m), 1), new CartItem(new Product("Apples", 1.0m), quantity) }; var sut = new PercentageDiscount("Apples", 10, new DateTime(2018, 11, 19)); // Act var result = sut.Apply(items); // Assert Assert.Collection(result, d => { Assert.Equal(expectedDescription, d.Description); Assert.Equal(expectedDiscount, d.Amount); }); }
internal void EditMedicine() { if (DisountType == "FixedAmountDiscount") { Discount = new FixedAmountDiscount { Amount = AmountOrPercentage }; } else { Discount = new PercentageDiscount { Amount = AmountOrPercentage }; } try { _medicineService.EditMedicine(new Medicine { Id = Id, Name = Name, Description = Description, Image = new MedicineImage { Url = Url }, Price = Price, PriceDiscount = Discount }); Notification = new NotificationModel("Success!", "Medicine edited successfully.", Notificationtype.Success); } catch (InvalidOperationException iex) { Notification = new NotificationModel("Failed!", "Failed to edit medicine, please provide valid name.", Notificationtype.Fail); } }
public override int GetHashCode() { int hashCode = 1536175793; if (Scope != null) { hashCode += Scope.GetHashCode(); } if (DiscountType != null) { hashCode += DiscountType.GetHashCode(); } if (PercentageDiscount != null) { hashCode += PercentageDiscount.GetHashCode(); } if (CatalogObjectIds != null) { hashCode += CatalogObjectIds.GetHashCode(); } if (FixedDiscountMoney != null) { hashCode += FixedDiscountMoney.GetHashCode(); } if (MaxDiscountMoney != null) { hashCode += MaxDiscountMoney.GetHashCode(); } return(hashCode); }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj == this) { return(true); } return(obj is LoyaltyProgramRewardDefinition other && ((Scope == null && other.Scope == null) || (Scope?.Equals(other.Scope) == true)) && ((DiscountType == null && other.DiscountType == null) || (DiscountType?.Equals(other.DiscountType) == true)) && ((PercentageDiscount == null && other.PercentageDiscount == null) || (PercentageDiscount?.Equals(other.PercentageDiscount) == true)) && ((CatalogObjectIds == null && other.CatalogObjectIds == null) || (CatalogObjectIds?.Equals(other.CatalogObjectIds) == true)) && ((FixedDiscountMoney == null && other.FixedDiscountMoney == null) || (FixedDiscountMoney?.Equals(other.FixedDiscountMoney) == true)) && ((MaxDiscountMoney == null && other.MaxDiscountMoney == null) || (MaxDiscountMoney?.Equals(other.MaxDiscountMoney) == true))); }
public Bundle(Cheese cheese, Screwdriver screwdriver, PercentageDiscount percentageDiscount) { _cheese = cheese; _screwdriver = screwdriver; _percentageDiscount = percentageDiscount; }
public void PercentageDiscount_DiscountedItems_IsNull_ExceptionThrown() { // Arrange + Act + Assert var percentageDiscount = new PercentageDiscount(null, 0.10m); }
public void RandomProductFor_PercentageDiscount_CalculateAppliedDiscount_With10PercentDiscount(List <Product> products, PercentageDiscount percentageDiscount) { var discountRate = 0.10m; // Arrange percentageDiscount._percentage = discountRate; var productNames = products.Select(x => x.Name).ToList(); productNames.Add(percentageDiscount._discountedProduct.Name); var disconts = new List <IDiscount>(); disconts.Add(percentageDiscount); _productService.Setup(x => x.GetProductByName(percentageDiscount._discountedProduct.Name)).Returns(percentageDiscount._discountedProduct); foreach (var item in products) { _productService.Setup(x => x.GetProductByName(item.Name)).Returns(item); } var sut = new ShoppingBasketService(_productService.Object); var basket = sut.AddProducts(productNames.ToArray()); var sutD = new DiscountService(); // Act var result = sutD.CalculateBasketDiscounts(basket, disconts); var subTotalPrice = basket.Sum(item => item.Product.Price * item.Quantity); var totalPrice = subTotalPrice - result.Sum(item => item.Amount); var discontMessage = string.Join(" --- ", result.Select(x => x.Description)); // Assert Assert.Equal(subTotalPrice - totalPrice, percentageDiscount._discountedProduct.Price * percentageDiscount._percentage); }