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, 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 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 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 Receipt ChecksOutArticlesFrom(ShoppingCart theCart) { Receipt receipt = new Receipt(); List <ProductQuantity> productQuantities = theCart.GetItems(); foreach (var pq in productQuantities) { receipt.AddProduct(pq, _catalog.GetUnitPrice(pq.Product)); } handleOffers(theCart.ProductQuantityPairs(), receipt); return(receipt); }
public Receipt ChecksOutArticlesFrom(ShoppingCart theCart) { var receipt = new Receipt(); var productQuantities = theCart.GetItems(); foreach (var pq in productQuantities) { var p = pq.Product; var quantity = pq.Quantity; var unitPrice = _catalog.GetUnitPrice(p); var price = quantity * unitPrice; receipt.AddProduct(p, quantity, unitPrice, price); } theCart.HandleOffers(receipt, _offers, _catalog); return(receipt); }