private void CalculateItemPrice(OrderItem item) { item.Price = item.Ingredients.Sum(i => i.Price * i.Ammount); Func <Ingredients, decimal> calculateSaleIngredient = ingredient => { var price = item.Ingredients .Where(i => i.Ingredient.Id != (short)ingredient) .Sum(i => i.Price * i.Ammount); return(price + item.Ingredients .Where(i => i.Ingredient.Id == (short)ingredient) .Sum(i => i.Price * (i.Ammount - i.Ammount / IngredientSaleAmmount))); }; // The sales are calculated according to their priority. // If one sale applies to one item, no other sales can be applied. if (item.HasIngredient(Ingredients.Alface) && !item.HasIngredient(Ingredients.Bacon)) { item.Price *= 0.9m; item.Sale = Sales.Light; } else if (item.CountIngredient(Ingredients.Hamburguer) >= IngredientSaleAmmount) { item.Price = calculateSaleIngredient(Ingredients.Hamburguer); item.Sale = Sales.MuitaCarne; } else if (item.CountIngredient(Ingredients.Queijo) >= IngredientSaleAmmount) { item.Price = calculateSaleIngredient(Ingredients.Queijo); item.Sale = Sales.MuitoQueijo; } }