Skip to content

Commit 98b79fc

Browse files
committed
Update rules pattern examples
1 parent 7b0e99b commit 98b79fc

File tree

12 files changed

+38
-39
lines changed

12 files changed

+38
-39
lines changed

src/AdditionalPatterns/Rules/RulesLibrary/Common/Customer.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
public class Customer
44
{
5-
public DateTime? DateOfFirstPurchase { get; set; }
6-
7-
public DateTime DateOfBirth { get; set; }
8-
9-
public bool IsVeteran { get; set; }
10-
}
5+
public Customer(DateTime dateOfBirth, bool isVeteran, DateTime? dateOfFirstPurchase = null)
6+
{
7+
DateOfBirth = dateOfBirth;
8+
IsVeteran = isVeteran;
9+
DateOfFirstPurchase = dateOfFirstPurchase;
10+
}
11+
public DateTime DateOfBirth { get; }
12+
public DateTime? DateOfFirstPurchase { get; }
13+
public bool IsVeteran { get; }
14+
}

src/AdditionalPatterns/Rules/RulesLibrary/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public override void Execute()
1313
MotivatingExecutor.Execute();
1414
RulesExecutor.Execute();
1515
}
16-
}
16+
}

src/AdditionalPatterns/Rules/RulesLibrary/MotivatingExample/DiscountCalculator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ public class DiscountCalculator
1111
{
1212
public decimal Calculate(Customer customer)
1313
{
14-
decimal discount = 0;
14+
var discount = 0m;
1515
var currentDate = DateTime.Now;
1616

1717
if (customer.DateOfBirth < currentDate.AddYears(-65))
1818
{
19-
// Senior discount of 5%
19+
// Senior discount of 5%.
2020
discount = 0.05m;
2121
}
2222

2323
if (customer.DateOfFirstPurchase.HasValue)
2424
{
2525
if (customer.DateOfFirstPurchase.Value < currentDate.AddYears(-1))
2626
{
27-
// After 1 year, loyal customers get 10% discount
27+
// After 1 year, loyal customers get 10% discount.
2828
discount = Math.Max(discount, 0.10m);
2929
if (customer.DateOfFirstPurchase.Value < currentDate.AddYears(-5))
3030
{
31-
// After 5 years, loyal customers get 12% discount
31+
// After 5 years, loyal customers get 12% discount.
3232
discount = Math.Max(discount, 0.12m);
3333
if (customer.DateOfFirstPurchase.Value < currentDate.AddYears(-10))
3434
{
35-
// After 10 years, loyal customers get 20% discount
35+
// After 10 years, loyal customers get 20% discount.
3636
discount = Math.Max(discount, 0.20m);
3737
}
3838
}
@@ -41,20 +41,20 @@ public decimal Calculate(Customer customer)
4141
customer.DateOfBirth.Day == DateTime.Today.Day)
4242
{
4343
// Loyal customers can get an additional 10% discount
44-
// if they purchase a product on their birthday
44+
// if they purchase a product on their birthday.
4545
discount += 0.10m;
4646
}
4747
}
4848
}
4949
else
5050
{
51-
// First time purchase discount of 15%
51+
// First time purchase discount of 15%.
5252
discount = Math.Max(discount, 0.15m);
5353
}
5454

5555
if (customer.IsVeteran)
5656
{
57-
// Veterans get 10% discount
57+
// Veterans get 10% discount.
5858
discount = Math.Max(discount, 0.10m);
5959
}
6060

src/AdditionalPatterns/Rules/RulesLibrary/MotivatingExample/MotivatingExecutor.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ public static void Execute()
99
{
1010
ConsoleExtension.WriteSeparator("Motivating example");
1111

12-
var customer = new Customer()
13-
{
14-
DateOfBirth = DateTime.Now.AddYears(-70),
15-
DateOfFirstPurchase = DateTime.Today.AddYears(-6),
16-
IsVeteran = false,
17-
};
12+
var customer = new Customer(
13+
dateOfBirth: DateTime.Now.AddYears(-70),
14+
dateOfFirstPurchase: DateTime.Today.AddYears(-6),
15+
isVeteran: false);
1816

1917
var discountCalculator = new DiscountCalculator();
20-
2118
var discountPercentage = discountCalculator.Calculate(customer);
2219

23-
Console.WriteLine($"Customer can get a {discountPercentage:P2} discount on any product.");
20+
Console.WriteLine($"The customer can get a {discountPercentage:P2} discount on any product.");
2421
}
25-
}
22+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/DiscountCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public decimal Calculate(Customer customer)
3939

4040
return discount;
4141
}
42-
}
42+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/Rules/BirthdayDiscountRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ public decimal CalculateDiscount(Customer customer)
1515

1616
return 0;
1717
}
18-
}
18+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/Rules/Common/IDiscountRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace RulesLibrary.RulesExample.Rules.Common;
55
public interface IDiscountRule
66
{
77
decimal CalculateDiscount(Customer customer);
8-
}
8+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/Rules/FirstPurchaseRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public decimal CalculateDiscount(Customer customer)
1414

1515
return 0;
1616
}
17-
}
17+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/Rules/LoyalCustomerRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public decimal CalculateDiscount(Customer customer)
2727

2828
return 0;
2929
}
30-
}
30+
}

src/AdditionalPatterns/Rules/RulesLibrary/RulesExample/Rules/SeniorRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public decimal CalculateDiscount(Customer customer)
1414

1515
return 0;
1616
}
17-
}
17+
}

0 commit comments

Comments
 (0)