Problem statement
Register several discount rules behind one interface, and let a pricing service apply all of them, in registration order, to a base price.
Requirements
The rules, the PricingController and the response classes are already provided. Your task has two parts: register the services in DiscountPricingApi/Program.cs, and finish Quote in DiscountPricingApi/Services/PricingService.cs.
You can register several classes for one interface. A constructor that asks for IEnumerable<IDiscountRule> then receives all of them, in registration order. PricingService already does this and keeps them in its rules field.
Each rule has a Name, and an Apply method that takes a price and returns the discounted price:
- SeasonalDiscountRule (Seasonal): 10% off, price x 0.90
- MembershipDiscountRule (Membership): 15 off, price - 15
- ClearanceDiscountRule (Clearance): 20% off, price x 0.80
Program.cs
- Register SeasonalDiscountRule, MembershipDiscountRule and ClearanceDiscountRule as IDiscountRule with AddScoped, in that order
- Register PricingService as IPricingService with AddScoped
PricingService.Quote
The bad-price check and the return are already written.
- Go through rules in order, and give each rule the price that the previous rule returned (the first rule gets basePrice). Keep the result in price
- If price becomes negative, set it to 0 before the next rule runs
- After each rule, add a QuoteStep to steps, with Rule set to the rule's Name and Price set to the new price
- Quote uses only the rules in the rules field and never creates rule objects itself
PricingController (provided)
- GET /quote?basePrice=100 answers 200 with basePrice, finalPrice and steps. A missing, 0 or negative basePrice answers 400
Sample Requests & Responses
Decimal results keep the decimal places from the multiplications, so 60 is printed as 60.0000.
GET /quote?basePrice=100
Response (200 OK):
{
"basePrice": 100,
"finalPrice": 60.0000,
"steps": [
{ "rule": "Seasonal", "price": 90.00 },
{ "rule": "Membership", "price": 75.00 },
{ "rule": "Clearance", "price": 60.0000 }
]
}
GET /quote?basePrice=10
Membership turns 9.00 into -6.00, which becomes 0 before Clearance runs.
Response (200 OK):
{
"basePrice": 10,
"finalPrice": 0.00,
"steps": [
{ "rule": "Seasonal", "price": 9.00 },
{ "rule": "Membership", "price": 0 },
{ "rule": "Clearance", "price": 0.00 }
]
}
GET /quote?basePrice=0
Response (400 Bad Request).
GET /quote
Response (400 Bad Request).
