public void HandleOffers(Receipt receipt, Dictionary <Product, Offer> offers, IShoppingCatalog catalog) { foreach (var p in _productLine.Keys) { var quantity = _productLine[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 (p.Name == "Rubber Gloves" && offer.OfferType == SpecialOfferType.ThreeForTwo && quantityAsInt > 2) { x = 3; var numberOfXs = quantityAsInt / x; var discountAmount = (quantity * unitPrice) - ((numberOfXs * 2 * unitPrice) + quantityAsInt % 3 * unitPrice); discount = new Discount(p, "3 for 2", discountAmount); } if (p.Name == "Stethoscope" && offer.OfferType == SpecialOfferType.ThreeForAmount && quantityAsInt >= 3) { var discountAmount = (quantity * unitPrice) - (((quantityAsInt / 3) * 999) + (quantityAsInt % 3 * unitPrice)); discount = new Discount(p, "3 pcs for 999", discountAmount); } if (discount != null) { receipt.AddDiscount(discount); } } } }
public Receipt ChecksOutGoods(ShoppingCart shoppingCart) { var receipt = new Receipt(); var orderLines = shoppingCart.GetOrderLines(); if (orderLines.Count() == 0) { throw new ArgumentNullException("Cart is empty!"); } else { foreach (OrderLine line in orderLines) { var p = line.Product; var quantity = line.Quantity; var unitPrice = _catalog.GetUnitPrice(p); var price = quantity * unitPrice; receipt.AddProduct(p, quantity, unitPrice, price); } shoppingCart.HandleOffers(receipt, _offers, _catalog); } return(receipt); }