コード例 #1
0
        private static void ProcessSale(string filename, IEnumerable <string> productLines, ProductCatalog productCatalog)
        {
            Sale sale = null;

            try
            {
                sale = new Sale();
            }
            catch (PromotionCatalogFileException ex)
            {
                WriteLine("Error reading promotion catalog.");
                WriteLine($"{ex.Message}");
                WriteLine($"{ex.InnerException?.Message}");
                WriteLine("Please have a system administrator correct the error.");
                return;
            }

            WriteLine(new string('-', 80));
            WriteLine($"Sale from {filename}");
            WriteLine(new string('-', 80));

            foreach (
                var productLine in
                productLines.Where(line => !string.IsNullOrEmpty(line)).Select(line => line.Trim()))
            {
                var product = productCatalog.FindProduct(productLine);
                if (product == null)
                {
                    WriteLine($"Warning! '{productLine}' was not found in the product catalog, skipping.");
                    continue;
                }
                sale.MakeLineItem(product, quantity: 1);
            }

            var total = sale.GetTotal();

            foreach (var lineItem in sale.LineItems.OrderBy(i => i.ProductId))
            {
                Write(lineItem.GetLineItemString());
            }

            WriteLine(new string('-', 80));
            WriteLine("Your total is ".PadRight(30) + $"{total:c}");
            WriteLine(new string('-', 80));
        }