Skip to content

Remove old version and udpate to use reflection to load rules #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions DesignPatternsInCSharp/RulesEngine/Before/Customer.cs

This file was deleted.

47 changes: 0 additions & 47 deletions DesignPatternsInCSharp/RulesEngine/Before/DiscountCalculator.cs

This file was deleted.

This file was deleted.

103 changes: 84 additions & 19 deletions DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,127 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace DesignPatternsInCSharp.RulesEngine.Discounts
{
public class DiscountCalculator
// View History
// https://github.githistory.xyz/ardalis/DesignPatternsInCSharp/blob/master/DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculator.cs
public interface IDiscountRule
{
public decimal CalculateDiscountPercentage(Customer customer)
decimal CalculateDiscount(Customer customer, decimal currentDiscount);
}

public class FirstTimeCustomerRule : IDiscountRule
{
public decimal CalculateDiscount(Customer customer, decimal currentDiscount)
{
bool isBirthday = customer.DateOfBirth.HasValue && customer.DateOfBirth.Value.Month == DateTime.Today.Month && customer.DateOfBirth.Value.Day == DateTime.Today.Day;
if (!customer.DateOfFirstPurchase.HasValue)
{
return .15m;
}
else
return 0;
}
}

public class LoyalCustomerRule : IDiscountRule
{
public decimal CalculateDiscount(Customer customer, decimal currentDiscount)
{
if (customer.DateOfFirstPurchase.HasValue)
{
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-15))
{
if (isBirthday) return .25m;
return .15m;
}
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-10))
{
if (isBirthday) return .22m;
return .12m;
}
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-5))
{
if (isBirthday) return .20m;
return .10m;
}
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-2) &&
!customer.IsVeteran)
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-2))
{
if (isBirthday) return .18m;
return .08m;
}
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-1) &&
!customer.IsVeteran)
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-1))
{
if (isBirthday) return .15m;
return .05m;
}
}
return 0;
}
}

public class VeteranRule : IDiscountRule
{
public decimal CalculateDiscount(Customer customer, decimal currentDiscount)
{
if (customer.IsVeteran)
{
if (isBirthday) return .20m;
return .10m;
}

if(customer.DateOfBirth < DateTime.Now.AddYears(-65))
return 0;
}
}
public class SeniorRule : IDiscountRule
{
public decimal CalculateDiscount(Customer customer, decimal currentDiscount)
{
if (customer.DateOfBirth < DateTime.Now.AddYears(-65))
{
if (isBirthday) return .15m;
return .05m;
}
return 0;
}
}

if (isBirthday) return .10m;
public class BirthdayRule : IDiscountRule
{
public decimal CalculateDiscount(Customer customer, decimal currentDiscount)
{
bool isBirthday = customer.DateOfBirth.HasValue && customer.DateOfBirth.Value.Month == DateTime.Today.Month && customer.DateOfBirth.Value.Day == DateTime.Today.Day;

return 0;
if (isBirthday) return currentDiscount + 0.10m;
return currentDiscount;
}
}

public class DiscountRuleEngine
{
List<IDiscountRule> _rules = new List<IDiscountRule>();

public DiscountRuleEngine(IEnumerable<IDiscountRule> rules)
{
_rules.AddRange(rules);
}

public decimal CalculateDiscountPercentage(Customer customer)
{
decimal discount = 0m;
foreach(var rule in _rules)
{
discount = Math.Max(discount, rule.CalculateDiscount(customer, discount));
}
return discount;
}
}

public class DiscountCalculator
{
public decimal CalculateDiscountPercentage(Customer customer)
{
var ruleType = typeof(IDiscountRule);
IEnumerable<IDiscountRule> rules = this.GetType().Assembly.GetTypes()
.Where(p => ruleType.IsAssignableFrom(p) && !p.IsInterface)
.Select(r => Activator.CreateInstance(r) as IDiscountRule);

var engine = new DiscountRuleEngine(rules);

return engine.CalculateDiscountPercentage(customer);
}
}
}