Created
May 24, 2011 13:18
-
-
Save olostan/988676 to your computer and use it in GitHub Desktop.
POS terminal problem in 1 line of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Practices.Unity; | |
namespace ConsoleApplication1 | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Func<String[], decimal> POSTerminal = inp => | |
( | |
from item in | |
from code in inp | |
group code by code | |
into g | |
select new {code = g.Key, count = g.Count()} | |
join catItem in | |
new dynamic[]// Catalog | |
{ | |
new {Code = "A", PriceForOne = 1.25m, Pack = 3, PriceForPack = 3m}, | |
new {Code = "B", PriceForOne = 4.25m}, | |
new {Code = "C", PriceForOne = 1m, Pack = 6, PriceForPack = 5m}, | |
new {Code = "D", PriceForOne = 0.75m}, | |
} | |
on item.code.ToString() equals catItem.Code.ToString() | |
select | |
//catItem.Pack > 1 | |
( ((Type) catItem.GetType()).GetProperties().FirstOrDefault(p => p.Name == "Pack")!=null | |
? item.count/catItem.Pack*catItem.PriceForPack + | |
(item.count%catItem.Pack)*catItem.PriceForOne | |
: item.count*catItem.PriceForOne) | |
).Sum(x => (decimal)x); | |
Console.WriteLine("13.25=" + POSTerminal(new[] { "A", "B", "C", "D", "A", "B", "A" })); | |
Console.WriteLine("6=" + POSTerminal(new[] { "C", "C", "C", "C", "C", "C", "C" })); | |
Console.WriteLine("7.25=" + POSTerminal(new[] { "A", "B", "C", "D" })); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment