public Receipt Checkout() { log.Info("Checkout Started"); IDictionary <int, IProduct> products = _storeRepository.GetProducts(); ISalePrice salePrice = new SalePrice(_storeRepository); IPromotionalPrice promotionalPrice = new PromotionalPrice(_storeRepository); Receipt receipt = new Receipt(); foreach (KeyValuePair <int, int> cartItem in CartItems) { log.Info($"Processing product: #{_cartItemDescription[cartItem.Key].PLU}: {_cartItemDescription[cartItem.Key].Description}"); if (!products.ContainsKey(cartItem.Key)) { log.Warn($"Product not found: #{_cartItemDescription[cartItem.Key].PLU}: {_cartItemDescription[cartItem.Key].Description}"); receipt.IgnoredItems.Add(_cartItemDescription[cartItem.Key]); continue; } IProduct currentProduct = products[cartItem.Key]; decimal bestPrice = cartItem.Value * products[cartItem.Key].Price; ReceiptLineItem receiptLineItem = new ReceiptLineItem(currentProduct.PLU, currentProduct.Name, cartItem.Value, currentProduct.Price); receipt.AddLineItem(receiptLineItem); decimal price = salePrice.Apply(cartItem, products[cartItem.Key].Price); if (price < bestPrice) { bestPrice = price; receiptLineItem.SetDiscountLine($"Sale @ {salePrice.GetSalePrice(currentProduct.PLU)} ea ", bestPrice); } price = promotionalPrice.Apply(cartItem, products[cartItem.Key].Price); if (price < bestPrice) { bestPrice = price; receiptLineItem.SetDiscountLine($"Promotion {promotionalPrice.GetAppliedPromotion(currentProduct.PLU)}", bestPrice); } receipt.GrandTotal += bestPrice; } log.Info("Checkout Completed"); return(receipt); }
public void AddLineItem(ReceiptLineItem receiptLineItem) { LineItems.Add(receiptLineItem); }