- If the purchase order contains a membership, it has to be activated in the customer account immediately.
- If the purchase order contains Comprehensive"Comprehensive First Aid Trainingtraining video," then Basic"Basic First Aid training videovideo" is added to the purchase order
Using the strategy design pattern, iI define the first rule as follows:
I dontdon't feel comfortable using C# is
and as
above. Is there a better way for me to program these rules. I was thinking of Visitor pattern but given the 2nd rule. I will need to operate on the order and the item as well.
A line item is what iI call an item in an order. For For example, an order can look like this.:
Order No: 3344656
Total: 48.50
Customer Id: 4567890
Line items:
• Video "Comprehensive First Aid Training"
• Book "The Girl on the train"
• Book Club Membership
Order No: 3344656
Total: 48.50
Customer Id: 4567890
Line items:
• Video "Comprehensive First Aid Training"
• Book "The Girl on the train"
• Book Club Membership
public class ComprehensiveVideoRule : IPurchaseOrderRule
{
public void Execute(PurchaseOrder order)
{
bool addNewVideo = false;
foreach(var item in order.Items)
{
if(item is Video)
{
var checkItem = item as Video;
if(checkItem.Title.Contains("Comprehensive"))
addNewVideo = true;
}
}
if(addNewVideo) order.Items.Add(new Video("Basic First Aid"));
}
}
public class ComprehensiveVideoRule : IPurchaseOrderRule
{
public void Execute(PurchaseOrder order)
{
bool addNewVideo = false;
foreach(var item in order.Items)
{
if(item is Video)
{
var checkItem = item as Video;
if(checkItem.Title.Contains("Comprehensive"))
addNewVideo = true;
}
}
if(addNewVideo) order.Items.Add(new Video("Basic First Aid"));
}
}