...

/

Solution: Apply Flexible Discounts for Marketing Campaigns

Solution: Apply Flexible Discounts for Marketing Campaigns

Implement self-contained discount strategies and delegate discount calculation through a configurable DiscountService context.

We'll cover the following...

Solution explanation

  • Lines 2–22: We define three discount strategies: FlatDiscountPercentageDiscount, and LoyaltyDiscount.

    • Each strategy encapsulates a distinct pricing rule through a .calculate() method.

    • FlatDiscount applies a fixed numeric deduction.

    • PercentageDiscount multiplies the subtotal to simulate a percentage-based reduction.

    • LoyaltyDiscount simulates a loyalty-tier reduction, representing customer-specific logic.

  • Lines 25–38: ...