Skip to main content
edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
added 59 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
  1. If the purchase order contains a membership, it has to be activated in the customer account immediately.
  2. 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"));
    }
}
  1. If the purchase order contains a membership, it has to be activated in the customer account immediately.
  2. If the purchase order contains Comprehensive First Aid Training video then Basic First Aid training video is added to the purchase order

Using the strategy design pattern, i define the first rule as follows

I dont 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 i call an item in an order. 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 
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"));
    }
}
  1. If the purchase order contains a membership, it has to be activated in the customer account immediately.
  2. If the purchase order contains "Comprehensive First Aid training video," then "Basic First Aid training video" is added to the purchase order

Using the strategy design pattern, I define the first rule as follows:

I don'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 I call an item in an order. 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 
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"));
    }
}
added 619 characters in body
Source Link

Added code for the second rule:

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"));
    }
}

Added code for the second rule:

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"));
    }
}
updated definition of line item with an example
Source Link
Loading
edited title and formatting
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Loading
added 5 characters in body
Source Link
Loading
Post Undeleted by Francis Ade
Post Deleted by Francis Ade
Source Link
Loading