コード例 #1
0
        private static void CalculatePrice(string commandName, string[] arguments, ICommandInvoker commandInvoker)
        {
            IBasketService           basketService           = new BasketService(dbContext);
            GetProductsByNameCommand getProductByNameCommand = new GetProductsByNameCommand(basketService);

            getProductByNameCommand.ProductNames = arguments;
            commandInvoker.ExecuteCommand(getProductByNameCommand);
            IEnumerable <BasketItem> products = getProductByNameCommand.Result;

            if (products.Count() == 0)
            {
                Console.WriteLine("Given products are not available!");
                return;
            }

            CalculateTotalPriceCommand calculateTotalPriceCommand = AppCommandsByName[commandName] as CalculateTotalPriceCommand;

            calculateTotalPriceCommand.Items = products;
            commandInvoker.ExecuteCommand(calculateTotalPriceCommand);

            decimal basketItemsTotalPrice = calculateTotalPriceCommand.Result;

            Console.WriteLine($"Subtotal: {basketItemsTotalPrice:c}");

            IDateTimeUtil dateTimeUtil = new DateTimeUtil();
            ITimespanDiscountCalculator timespanDiscountCalculator = new TimespanDiscountCalculator(dateTimeUtil);
            IQuantityDiscountCalculator quantityDiscountCalculator = new QuantityDiscountCalculator();
            IDiscountService            discountService            = new DiscountService(dbContext, timespanDiscountCalculator, quantityDiscountCalculator);
            GetDiscountsCommand         getDiscountCommand         = new GetDiscountsCommand(discountService);

            getDiscountCommand.Items = products;
            commandInvoker.ExecuteCommand(getDiscountCommand);

            IEnumerable <DiscountResult> discounts = getDiscountCommand.Result;

            if (discounts.Count() > 0)
            {
                foreach (DiscountResult discount in discounts)
                {
                    Console.WriteLine($"{discount.ProductPluralName} {discount.DiscountPercentage:p0} off: -{discount.DiscountedPrice:c}");
                }
            }
            else
            {
                Console.WriteLine("(No offers available)");
            }

            decimal discountPrice = discounts.Sum(s => s.DiscountedPrice);
            decimal totalPrice    = basketItemsTotalPrice - discountPrice;

            Console.WriteLine($"Total price: {totalPrice:c}");
        }