An online shop needs a cart. Build a ShoppingCart class that tracks what is in the cart and what it costs.
new ShoppingCart(catalog) takes an object that maps each SKU to its price in cents. A SKU is a product code. For example: { "MUG": 1200, "TEE": 2500 }. All money in this problem is in whole cents, so prices never have fractions of a cent.
Input: cart = new ShoppingCart({ MUG: 1200, PEN: 150 })
cart.addItem("MUG", 2); cart.addItem("PEN", 3)
cart.total()
Output: 2850
Input: cart.addCode("SAVE10", { kind: "percent", value: 10 })
cart.applyCode("SAVE10")
cart.total()
Output: 256510% of 2850 is 285
It comes in 3 parts, each building on the last: Items and totals, Discount codes, Per-customer limits.