Last active
December 13, 2015 17:48
-
-
Save hyrmn/4950442 to your computer and use it in GitHub Desktop.
Things I wouldn't do
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
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