Skip to content

Commit 0cc717b

Browse files
committed
All tests pass for combined birthday rulse with loyalty rules
1 parent 4591d94 commit 0cc717b

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculate_CalculateDiscountPercentage.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,33 @@ public void Returns10PctForCustomersWhoAreVeterans(int customerAge)
6262
[InlineData(15, .15)]
6363
public void ReturnsCorrectLoyaltyDiscountForLongtimeCustomers(int yearsAsCustomer, decimal expectedDiscount)
6464
{
65-
var customer = CreateCustomer(DEFAULT_AGE,
65+
var customer = CreateCustomer(DEFAULT_AGE,
6666
DateTime.Today.AddYears(-yearsAsCustomer).AddDays(-1));
6767

6868
var result = _calculator.CalculateDiscountPercentage(customer);
6969

7070
result.Should().Be(expectedDiscount);
7171
}
7272

73+
[Theory]
74+
[InlineData(1, .15)]
75+
[InlineData(2, .18)]
76+
[InlineData(5, .20)]
77+
[InlineData(10, .22)]
78+
[InlineData(15, .25)]
79+
public void ReturnsCorrectLoyaltyDiscountForLongtimeCustomersOnTheirBirthday(int yearsAsCustomer, decimal expectedDiscount)
80+
{
81+
var customer = new Customer
82+
{
83+
DateOfBirth = DateTime.Today.AddYears(-DEFAULT_AGE),
84+
DateOfFirstPurchase = DateTime.Today.AddYears(-yearsAsCustomer).AddDays(-1)
85+
};
86+
87+
var result = _calculator.CalculateDiscountPercentage(customer);
88+
89+
result.Should().Be(expectedDiscount);
90+
}
91+
7392
[Theory]
7493
[InlineData(1)]
7594
[InlineData(2)]

DesignPatternsInCSharp/RulesEngine/Discounts/DiscountCalculator.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,56 @@ public class DiscountCalculator
66
{
77
public decimal CalculateDiscountPercentage(Customer customer)
88
{
9-
if(!customer.DateOfFirstPurchase.HasValue)
9+
bool isBirthday = customer.DateOfBirth.HasValue && customer.DateOfBirth.Value.Month == DateTime.Today.Month && customer.DateOfBirth.Value.Day == DateTime.Today.Day;
10+
if (!customer.DateOfFirstPurchase.HasValue)
1011
{
1112
return .15m;
1213
}
1314
else
1415
{
1516
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-15))
1617
{
18+
if (isBirthday) return .25m;
1719
return .15m;
1820
}
1921
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-10))
2022
{
23+
if (isBirthday) return .22m;
2124
return .12m;
2225
}
23-
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-5) ||
24-
(customer.DateOfBirth.HasValue && customer.DateOfBirth.Value.Month == DateTime.Today.Month && customer.DateOfBirth.Value.Day == DateTime.Today.Day))
26+
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-5))
2527
{
28+
if (isBirthday) return .20m;
2629
return .10m;
2730
}
2831
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-2) &&
2932
!customer.IsVeteran)
3033
{
34+
if (isBirthday) return .18m;
3135
return .08m;
3236
}
3337
if (customer.DateOfFirstPurchase.Value < DateTime.Now.AddYears(-1) &&
3438
!customer.IsVeteran)
3539
{
40+
if (isBirthday) return .15m;
3641
return .05m;
3742
}
3843
}
3944

4045
if (customer.IsVeteran)
4146
{
47+
if (isBirthday) return .20m;
4248
return .10m;
4349
}
4450

4551
if(customer.DateOfBirth < DateTime.Now.AddYears(-65))
4652
{
53+
if (isBirthday) return .15m;
4754
return .05m;
4855
}
4956

57+
if (isBirthday) return .10m;
58+
5059
return 0;
5160
}
5261
}

0 commit comments

Comments
 (0)