Ejemplo n.º 1
0
        private void LogTotals(BasketResult result)
        {
            var message = new StringBuilder();

            message.AppendLine(".GetTotals()");
            message.AppendLine($"\tBase Amount: {result.BaseAmount.ToString("c")}");
            message.AppendLine($"\tTotal Discount: {result.DiscountAmount.ToString("c")}");
            message.AppendLine($"\tTotal Amount: {result.TotalAmount.ToString("c")}");
            message.AppendLine($"\tItems:");

            foreach (var item in result.BasketItemResults)
            {
                message.AppendLine($"\t\tProductId: {item.ProductId}; Quantity: {item.Quantity}; Base Amount: {item.BaseAmount.ToString("c")}; TotalAmount: {item.TotalAmount.ToString("c")};");

                if (item.DiscountResult.AppliedDiscounts.Any())
                {
                    foreach (var discount in item.DiscountResult.AppliedDiscounts)
                    {
                        message.AppendLine($"\t\t\tApplied Discount: {discount.Key}; Number of times applied: {discount.Value}; Total Discount Amount: {item.DiscountResult.TotalDiscount.ToString("c")}");
                    }
                }
            }

            _logger.LogInformation(message.ToString());
        }
        /// <summary>
        /// Calculate the Basket totals.
        /// </summary>
        /// <param name="basketItems">The collection of <see cref="BasketItem"/>.</param>
        /// <returns></returns>
        public BasketResult Calculate(IEnumerable <BasketItem> basketItems)
        {
            var result = new BasketResult();

            foreach (var bItem in basketItems)
            {
                var discountResult = _discountCalculator.Calculate(basketItems, bItem.ProductId);
                var baseAmount     = bItem.UnitPrice * bItem.Quantity;
                var itemResult     = new BasketItemResult
                {
                    ProductId      = bItem.ProductId,
                    BaseAmount     = baseAmount,
                    Quantity       = bItem.Quantity,
                    UnitPrice      = bItem.UnitPrice,
                    TotalAmount    = baseAmount - discountResult.TotalDiscount,
                    DiscountResult = discountResult
                };

                result.BasketItemResults.Add(itemResult);
            }

            return(result);
        }