Example #1
0
        public CalculateState TakeSuitableProducts(
            IEnumerable <Price> pricing,
            CalculateState calculateState)
        {
            var     leftProducts = new LinkedList <TProduct>();
            decimal totalPrice   = calculateState.LeftProducts.Sum(product => {
                var simplePrice = pricing.FirstOrDefault(price =>
                                                         (price is SimplePrice) &&
                                                         price.Product.Equals(product)) as SimplePrice;

                if (simplePrice == null)
                {
                    leftProducts.AddLast(product);
                    return(0m);
                }
                return(simplePrice.Price);
            });

            return(new CalculateState(calculateState.Money + totalPrice, leftProducts));
        }
Example #2
0
        public CalculateState TakeSuitableProducts(
            IEnumerable <Price> pricing,
            CalculateState calculateState)
        {
            var productGroups = calculateState.LeftProducts.GroupBy(x => x)
                                .ToDictionary(x => x.Key, x => x.Count());
            var pricingGroupedAroundProduct = pricing.GroupBy(x => x.Product);


            var calculatingResult = productGroups.AsParallel().Select(product => {
                var productPricings = GetPricesToCheckForProduct(product.Key, pricingGroupedAroundProduct);
                return(TakeProduct(product.Key, product.Value, productPricings));
            }).Aggregate(
                (leftProducts: Enumerable.Empty <TProduct>(), moneyReceived: 0m),
                (state, current) => (
                    state.leftProducts.Concat(Enumerable.Repeat(current.product, current.countProductsLeft)),
                    state.moneyReceived + current.moneyReceived
                    )
                );

            return(new CalculateState(calculateState.Money + calculatingResult.moneyReceived,
                                      calculatingResult.leftProducts));
        }