Problem: Apply Flexible Discounts for Marketing Campaigns
Problem statement
You’re building a checkout system for an online store. The discount logic keeps changing: sometimes marketing runs a flat discount, other times a percentage discount, and for loyal customers, there’s a tiered loyalty discount.
Discount logic often evolves into lengthy chains of conditionals within checkout code. Each new rule means touching stable logic and risking regressions. You’ve decided to apply the Strategy Pattern so that each discount rule can live independently and be swapped in at runtime via configuration.
Goal
Implement both components of the pattern:
Strategies:
Create
FlatDiscount,PercentageDiscount, andLoyaltyDiscountclasses.Each must define the
.calculate(amount)method that returns the final price after applying its rule.
Context:
Implement
DiscountServicethat uses one discount strategy at a time.It must delegate computation to the active strategy’s
.calculate()method.Allow strategy changes through
.setStrategy().
Constraints
No
iforswitchlogic insideDiscountService.Each discount strategy must be self-contained—no shared helper functions.
Adding a new discount type should not require modifying existing ones.
Sample output
The examples below illustrate what the output should look like:
const subtotal = 100;const service = new DiscountService(new FlatDiscount());console.log('Flat:', service.apply(subtotal)); // e.g., 90/* Expected output:Flat: 90*/service.setStrategy(new PercentageDiscount());console.log('Percentage:', service.apply(subtotal)); // e.g., 80/* Expected output:Percentage: 80*/service.setStrategy(new LoyaltyDiscount());console.log('Loyalty:', service.apply(subtotal)); // e.g., 85/* Expected output:Loyalty: 85*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Apply Flexible Discounts for Marketing Campaigns
Problem statement
You’re building a checkout system for an online store. The discount logic keeps changing: sometimes marketing runs a flat discount, other times a percentage discount, and for loyal customers, there’s a tiered loyalty discount.
Discount logic often evolves into lengthy chains of conditionals within checkout code. Each new rule means touching stable logic and risking regressions. You’ve decided to apply the Strategy Pattern so that each discount rule can live independently and be swapped in at runtime via configuration.
Goal
Implement both components of the pattern:
Strategies:
Create
FlatDiscount,PercentageDiscount, andLoyaltyDiscountclasses.Each must define the
.calculate(amount)method that returns the final price after applying its rule.
Context:
Implement
DiscountServicethat uses one discount strategy at a time.It must delegate computation to the active strategy’s
.calculate()method.Allow strategy changes through
.setStrategy().
Constraints
No
iforswitchlogic insideDiscountService.Each discount strategy must be self-contained—no shared helper functions.
Adding a new discount type should not require modifying existing ones.
Sample output
The examples below illustrate what the output should look like:
const subtotal = 100;const service = new DiscountService(new FlatDiscount());console.log('Flat:', service.apply(subtotal)); // e.g., 90/* Expected output:Flat: 90*/service.setStrategy(new PercentageDiscount());console.log('Percentage:', service.apply(subtotal)); // e.g., 80/* Expected output:Percentage: 80*/service.setStrategy(new LoyaltyDiscount());console.log('Loyalty:', service.apply(subtotal)); // e.g., 85/* Expected output:Loyalty: 85*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.