Total a Cart with Typed Discounts

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

A cart has items, each with a sku, a priceCents and a quantity, and an optional discount. Export a Discount union told apart by kind: percent (with a percent), fixed (with amountCents), or buyXGetY (with a sku, buy and free: in every group of buy + free units of that sku, free of them cost nothing). Export Cart and total(cart): the items' total in cents less the discount, rounded to the nearest cent and never below zero. Type it so a discount must have exactly its own kind's fields.

Your code must pass

  • Adds up items with no discount
  • Applies percent and fixed discounts
  • Applies buy X get Y free
  • Has no type errors
  • Discount kinds are exactly the three
  • Rejects a discount with the wrong fields
  • Rejects an unknown kind of discount