Some jobs can be done in several ways, like pricing a cart with different discounts. The Strategy pattern puts each way in its own class, all built on one abstract class. The code that uses them can swap one for another, and it never checks which kind it holds.
Here you build a shop's checkout. Its discount is the strategy, and it can change while the cart fills up.
The abstract class Discount has one method, apply(total, items). It returns the price to pay for a cart. The cart's sum in cents is total. The number of items in it is items. An abstract class lists methods without a body. Each class built on it must write them.
Write four classes built on it:
NoDiscount()paystotal.PercentOff(percent)takespercentpercent off. The discount is rounded down, so the price is rounded up.AmountOff(amount)takesamountcents off. The price never goes below 0.BulkDiscount(minItems, percent)pays like aPercentOffofpercentwhen the cart has at leastminItemsitems. A smaller cart paystotal.
Then write Checkout:
Checkout()starts with an empty cart and aNoDiscount.add(price)puts one item ofpricecents in the cart.setDiscount(discount)usesdiscountfrom now on.total()returns the price to pay. It asks the current discount'sapply.clear()empties the cart and keeps the discount.
The tests make each discount themselves and pass it in, as in setDiscount(new PercentOff(10)). So Checkout must call apply through the abstract class, whatever kind it holds.
Only total() writes output: one line with the price.
Example 1:
Input: Checkout() 1. add(800) 2. add(450) 3. total() 4. setDiscount(new PercentOff(10)) 5. total() 6. add(199) 7. total() 8. setDiscount(new NoDiscount()) 9. total() Output: 1250 1125 1305 1449 Explanation: The cart starts with no discount, so call 3 returns 800 + 450 = 1250. Call 4 swaps in 10 percent off. 10 percent of 1250 is 125, so call 5 returns 1125. Then the cart holds 1449 cents. 10 percent of that is 144.9, rounded down to 144. So call 7 returns 1449 - 144 = 1305. Call 8 swaps back to no discount, and call 9 returns the full 1449.
Example 2:
Input: Checkout() 1. setDiscount(new BulkDiscount(3, 20)) 2. add(400) 3. add(300) 4. total() 5. add(300) 6. total() Output: 700 800 Explanation: The bulk discount takes 20 percent off a cart of 3 items or more. At call 4 the cart holds 2 items, so it pays the full 700. At call 6 it holds 3 items. 20 percent of 1000 is 200, so it pays 800.
Example 3:
Input: Checkout() 1. setDiscount(new AmountOff(500)) 2. add(300) 3. add(150) 4. total() 5. add(100) 6. total() 7. clear() 8. add(900) 9. total() Output: 0 50 400 Explanation: At call 4 the cart holds 450, less than the 500 off. The price never goes below 0. At call 6 it holds 550, so it pays 50. Call 7 empties the cart but keeps the discount. So call 9 pays 900 - 500 = 400.
Constraints:
priceis a whole number from 1 to 1,000.percentis a whole number from 0 to 100.amountis a whole number from 0 to 1,000,000.minItemsis a whole number from 1 to 100,000.- The cart's sum never goes over 20,000,000 cents.
- At most 100,000 calls follow the constructor.
Note:
Each call should run in O(1) time, so keep the cart's sum and count, not its list of prices. The sum is at most 20,000,000. So total * percent is at most 2,000,000,000, which fits in a 32-bit integer.
Free plan: 30 tutor messages. PRO is unlimited.
