Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static decimal GetPrice(IEnumerable<Products> products, IDictionary<Products, decimal> prices)
- {
- var grouping = products.ToLookup(l => l);
- var ab = grouping[Products.A]
- .TupleZip(grouping[Products.B])
- .Count() * prices.GroupPrice(Products.A, Products.B) * 0.9m;
- var de = grouping[Products.D]
- .TupleZip(grouping[Products.E])
- .Count() * prices.GroupPrice(Products.A, Products.B) * 0.95m;
- var efg = grouping[Products.E]
- .TupleZip(grouping[Products.F])
- .TupleZip(grouping[Products.G])
- .Count() * prices.GroupPrice(Products.A, Products.B) * 0.95m;
- var aAndklm = products
- .Where(p => p == Products.C || p == Products.L || p == Products.M)
- .Take(grouping[Products.A].Count())
- .Aggregate(0m, (acc, p) => acc + prices[p] * 0.95m);
- decimal onCount;
- switch (products.Except(List(Products.A, Products.B)).Count())
- {
- case var c when c < 3:
- onCount = 1m;
- break;
- case 3:
- onCount = 0.95m;
- break;
- case 4:
- onCount = 0.90m;
- break;
- case var c when c >= 5:
- onCount = 0.95m;
- break;
- default:
- onCount = 1m;
- break;
- }
- var initalSum = products.Sum(p => prices[p]);
- var result = (initalSum - ab - de - efg - aAndklm) * onCount;
- return result;
- }
- static IEnumerable<(T, T)> TupleZip<T>(this IEnumerable<T> c1, IEnumerable<T> c2)
- {
- return c1.Zip(c2, (a, b) => (a, b));
- }
- static IEnumerable<(T, T, T)> TupleZip<T>(this IEnumerable<(T, T)> c1, IEnumerable<T> c2)
- {
- return c1.Zip(c2, (a, b) => (a.Item1, a.Item2, b));
- }
- static decimal GroupPrice(this IDictionary<Products, decimal> dict, params Products[] products)
- {
- return products.Sum(p => dict[p]);
- }
- static IEnumerable<T> List<T>(params T[] items) => items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement