public override Discount GetDiscount(KeyValuePair <Product, double> productQuantity, SupermarketCatalog catalog, Offer offer) { Product product = productQuantity.Key; double quantity = productQuantity.Value; var quantityAsInt = (int)productQuantity.Value; Discount discount = null; if (quantityAsInt >= 2) { var total = offer.Argument * (quantityAsInt / 2) + quantityAsInt % 2 * catalog.GetUnitPrice(product); var discountN = catalog.GetUnitPrice(product) * quantity - total; discount = new Discount(product, "2 for " + offer.Argument, -discountN); } return(discount); }
public override Discount GetDiscount(KeyValuePair <Product, double> productQuantity, SupermarketCatalog catalog, Offer offer) { Discount discount = null; Product product = productQuantity.Key; double quantity = productQuantity.Value; var quantityAsInt = (int)quantity; int numOfX = quantityAsInt / 3; if (quantityAsInt > 2) { var discountAmount = quantity * catalog.GetUnitPrice(product) - (numOfX * 2 * catalog.GetUnitPrice(product) + quantityAsInt % 3 * catalog.GetUnitPrice(product)); discount = new Discount(productQuantity.Key, "3 for 2", -discountAmount); } return(discount); }
public override Discount GetDiscount(KeyValuePair <Product, double> productQuantity, SupermarketCatalog catalog, Offer offer) { Discount discount = null; double quantity = productQuantity.Value; Product product = productQuantity.Key; if ((int)quantity >= 5) { int numOfX = (int)quantity / 5; var discountTotal = catalog.GetUnitPrice(product) * quantity - (offer.Argument * numOfX + (int)quantity % 5 * catalog.GetUnitPrice(product)); discount = new Discount(product, 5 + " for " + offer.Argument, -discountTotal); } return(discount); }
public void HandleOffers(Receipt receipt, IEnumerable <IOffer> offers, SupermarketCatalog catalog) { foreach (var offer in offers) { var discount = offer.GetDiscount(_productQuantities, catalog); if (discount != null) { receipt.AddDiscount(discount); } } }
public override Discount GetDiscount(KeyValuePair <Product, double> productQuantity, SupermarketCatalog catalog, Offer offer) { double quantity = productQuantity.Value; Product product = productQuantity.Key; return(new Discount(product, offer.Argument + "% off", -quantity * catalog.GetUnitPrice(product) * offer.Argument / 100.0)); }
public void HandleOffers(Receipt receipt, Dictionary <Product, Offer> offers, SupermarketCatalog catalog) { foreach (var p in _productQuantities.Keys) { var quantity = _productQuantities[p]; var quantityAsInt = (int)quantity; if (offers.ContainsKey(p)) { var offer = offers[p]; var unitPrice = catalog.GetUnitPrice(p); Discount discount = null; var x = 1; if (offer.OfferType == SpecialOfferType.ThreeForTwo) { x = 3; } else if (offer.OfferType == SpecialOfferType.TwoForAmount) { x = 2; if (quantityAsInt >= 2) { var total = offer.Argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice; var discountN = unitPrice * quantity - total; discount = new Discount(p, "2 for " + offer.Argument, discountN); } } if (offer.OfferType == SpecialOfferType.FiveForAmount) { x = 5; } var numberOfXs = quantityAsInt / x; if (offer.OfferType == SpecialOfferType.ThreeForTwo && quantityAsInt > 2) { var discountAmount = quantity * unitPrice - (numberOfXs * 2 * unitPrice + quantityAsInt % 3 * unitPrice); discount = new Discount(p, "3 for 2", discountAmount); } if (offer.OfferType == SpecialOfferType.TenPercentDiscount) { discount = new Discount(p, offer.Argument + "% off", quantity * unitPrice * offer.Argument / 100.0); } if (offer.OfferType == SpecialOfferType.FiveForAmount && quantityAsInt >= 5) { var discountTotal = unitPrice * quantity - (offer.Argument * numberOfXs + quantityAsInt % 5 * unitPrice); discount = new Discount(p, x + " for " + offer.Argument, discountTotal); } if (discount != null) { receipt.AddDiscount(discount); } } } }
public Teller(SupermarketCatalog catalog) { _catalog = catalog; }
public abstract Discount GetDiscount(KeyValuePair <Product, double> productQuantity, SupermarketCatalog catalog, Offer offer);
public virtual Discount GetDiscount(IDictionary <Product, double> quantitiesByProduct, SupermarketCatalog catalog) { if (!quantitiesByProduct.ContainsKey(_product)) { return(null); } var unitPrice = catalog.GetUnitPrice(_product); var quantity = quantitiesByProduct[_product]; return(_discounter.GetDiscount(_product, quantity, unitPrice)); }
public Discount GetDiscount(IDictionary <Product, double> quantitiesByProduct, SupermarketCatalog catalog) { var nbBundles = _products.Select(product => { var found = quantitiesByProduct.TryGetValue(product, out var quantity); return(found ? quantity : 0); }).Max(); var bundlePriceWithoutDiscount = _products.Select(catalog.GetUnitPrice).Sum(); return(_discounter.GetDiscount(new Product("bundle", ProductUnit.Each), nbBundles, bundlePriceWithoutDiscount)); }
public void SetUp() { catalog = new FakeCatalog(); cart = new ShoppingCart(); }