Skip to content

Instantly share code, notes, and snippets.

@olostan
Created May 24, 2011 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olostan/988676 to your computer and use it in GitHub Desktop.
Save olostan/988676 to your computer and use it in GitHub Desktop.
POS terminal problem in 1 line of code
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
{
public class Product
{
public decimal PriceForOne { get; set; }
public int Pack { get; set; }
public decimal PriceForPack { get; set; }
public string Code { get; set; }
}
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[] // Catalog
{
new Product {Code = "A", PriceForOne = 1.25m, Pack = 3, PriceForPack = 3m},
new Product {Code = "B", PriceForOne = 4.25m},
new Product {Code = "C", PriceForOne = 1m, Pack = 6, PriceForPack = 5m},
new Product {Code = "D", PriceForOne = 0.75m},
}
on item.code equals catItem.Code
select
catItem.Pack > 1
? item.count/catItem.Pack*catItem.PriceForPack +
(item.count%catItem.Pack)*catItem.PriceForOne
: item.count*catItem.PriceForOne
).Sum();
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