File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
DesignPatternsInCSharp/RulesEngine/Discounts Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -62,14 +62,33 @@ public void Returns10PctForCustomersWhoAreVeterans(int customerAge)
62
62
[ InlineData ( 15 , .15 ) ]
63
63
public void ReturnsCorrectLoyaltyDiscountForLongtimeCustomers ( int yearsAsCustomer , decimal expectedDiscount )
64
64
{
65
- var customer = CreateCustomer ( DEFAULT_AGE ,
65
+ var customer = CreateCustomer ( DEFAULT_AGE ,
66
66
DateTime . Today . AddYears ( - yearsAsCustomer ) . AddDays ( - 1 ) ) ;
67
67
68
68
var result = _calculator . CalculateDiscountPercentage ( customer ) ;
69
69
70
70
result . Should ( ) . Be ( expectedDiscount ) ;
71
71
}
72
72
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
+
73
92
[ Theory ]
74
93
[ InlineData ( 1 ) ]
75
94
[ InlineData ( 2 ) ]
Original file line number Diff line number Diff line change @@ -6,47 +6,56 @@ public class DiscountCalculator
6
6
{
7
7
public decimal CalculateDiscountPercentage ( Customer customer )
8
8
{
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 )
10
11
{
11
12
return .15m ;
12
13
}
13
14
else
14
15
{
15
16
if ( customer . DateOfFirstPurchase . Value < DateTime . Now . AddYears ( - 15 ) )
16
17
{
18
+ if ( isBirthday ) return .25m ;
17
19
return .15m ;
18
20
}
19
21
if ( customer . DateOfFirstPurchase . Value < DateTime . Now . AddYears ( - 10 ) )
20
22
{
23
+ if ( isBirthday ) return .22m ;
21
24
return .12m ;
22
25
}
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 ) )
25
27
{
28
+ if ( isBirthday ) return .20m ;
26
29
return .10m ;
27
30
}
28
31
if ( customer . DateOfFirstPurchase . Value < DateTime . Now . AddYears ( - 2 ) &&
29
32
! customer . IsVeteran )
30
33
{
34
+ if ( isBirthday ) return .18m ;
31
35
return .08m ;
32
36
}
33
37
if ( customer . DateOfFirstPurchase . Value < DateTime . Now . AddYears ( - 1 ) &&
34
38
! customer . IsVeteran )
35
39
{
40
+ if ( isBirthday ) return .15m ;
36
41
return .05m ;
37
42
}
38
43
}
39
44
40
45
if ( customer . IsVeteran )
41
46
{
47
+ if ( isBirthday ) return .20m ;
42
48
return .10m ;
43
49
}
44
50
45
51
if ( customer . DateOfBirth < DateTime . Now . AddYears ( - 65 ) )
46
52
{
53
+ if ( isBirthday ) return .15m ;
47
54
return .05m ;
48
55
}
49
56
57
+ if ( isBirthday ) return .10m ;
58
+
50
59
return 0 ;
51
60
}
52
61
}
You can’t perform that action at this time.
0 commit comments