StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 1 · Dependency Injection

Layered Discount Pricing API

easy100%2 of 2 solved

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).

More in Dependency Injection

See the module
Request Tracking Service LifetimesThis problem covers choosing how long each service should live, so a counter shared by the whole application keeps counting while a value that belongs to one request starts fresh every time.33%3 of 9 solvedeasyOrder Service Constructor GraphThis problem covers letting the container build a whole chain of collaborating objects, so a service and everything beneath it arrives ready to use instead of being constructed by hand at the top.100%1 of 1 solvedeasyNotification Dispatch CenterThis problem covers sending one message through every notification channel the application has, counting deliveries across the whole application while keeping each request's own details separate, and reporting what each channel did.50%1 of 2 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.