Skip to content

Commit d4ca380

Browse files
committed
Added loyalty discount
1 parent da924f4 commit d4ca380

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculate_CalculateDiscountPercentage.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ public void Returns10PctForCustomersWhoAreVeterans(int customerAge)
5454
result.Should().Be(.10m);
5555
}
5656

57+
[Theory]
58+
[InlineData(1, .05)]
59+
[InlineData(2, .08)]
60+
[InlineData(5, .10)]
61+
[InlineData(10, .12)]
62+
[InlineData(15, .15)]
63+
public void ReturnsCorrectLoyaltyDiscountForLongtimeCustomers(int yearsAsCustomer, decimal expectedDiscount)
64+
{
65+
var customer = CreateCustomer(DEFAULT_AGE,
66+
DateTime.Today.AddYears(-yearsAsCustomer).AddDays(-1));
67+
68+
var result = _calculator.CalculateDiscountPercentage(customer);
69+
70+
result.Should().Be(expectedDiscount);
71+
}
72+
73+
5774
private Customer CreateCustomer(int age = DEFAULT_AGE, DateTime? firstPurchaseDate = null)
5875
{
5976
return new Customer

DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculator.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,31 @@ public decimal CalculateDiscountPercentage(Customer customer)
1010
{
1111
return .15m;
1212
}
13-
14-
if(customer.IsVeteran)
13+
else
14+
{
15+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-15))
16+
{
17+
return .15m;
18+
}
19+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-10))
20+
{
21+
return .12m;
22+
}
23+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-5))
24+
{
25+
return .10m;
26+
}
27+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-2))
28+
{
29+
return .08m;
30+
}
31+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-1))
32+
{
33+
return .05m;
34+
}
35+
}
36+
37+
if (customer.IsVeteran)
1538
{
1639
return .10m;
1740
}

0 commit comments

Comments
 (0)