Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Last active December 13, 2015 17:48
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 hyrmn/4950442 to your computer and use it in GitHub Desktop.
Save hyrmn/4950442 to your computer and use it in GitHub Desktop.
Things I wouldn't do
public class Customer
{
public string Name { get; set; }
public int LifetimeValue { get; set; }
}
class Program
{
public const int NoDiscountThreshold = 3;
public const int LowDiscountThreshold = 6;
public const int BigDiscountThreshold = 15;
static void Main(string[] args)
{
var binner = new Dictionary<int, Action<Customer>>();
var cheapCustomers = new List<Customer>();
var regularCustomers = new List<Customer>();
var vipCustomers = new List<Customer>();
var customers = new List<Customer>
{
new Customer {Name="Bob", LifetimeValue=3},
new Customer {Name="Jimmy", LifetimeValue=3},
new Customer {Name="Sue", LifetimeValue=3},
new Customer {Name="Mary", LifetimeValue=6},
new Customer {Name="Sarah", LifetimeValue=28},
new Customer {Name="Bill", LifetimeValue=41},
};
binner.Add(NoDiscountThreshold, cheapCustomers.Add);
binner.Add(LowDiscountThreshold, regularCustomers.Add);
binner.Add(BigDiscountThreshold, vipCustomers.Add);
customers.ForEach(c => binner[GetDiscountBin(c.LifetimeValue)](c));
}
private static int GetDiscountBin(int lifetimeValue)
{
if (lifetimeValue >= BigDiscountThreshold) return BigDiscountThreshold;
if (lifetimeValue >= LowDiscountThreshold) return LowDiscountThreshold;
return NoDiscountThreshold;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment