Loading...
Loading...
A store processes commands on its stock. ADD p q adds q units of product p. SELL p q removes q units if there are enough, and prints OK; otherwise it changes nothing and prints NOT ENOUGH. STOCK p prints the units of p (0 if the store has none). A product whose stock reaches 0 is removed from the store.
SELL and every STOCK command; ADD prints nothing.Example
Input
7 ADD pen 10 ADD marker 4 SELL pen 3 STOCK pen SELL marker 5 SELL marker 4 STOCK marker
Output
OK 7 NOT ENOUGH OK 0
Selling 5 markers fails (only 4); selling 4 empties the product, so STOCK prints 0.
Try the problem first. Open one hint at a time.
Read the command word with sc.next(), then read the other values that this command needs.
stock.merge(p, q, (a, b) -> a + b) handles ADD, and getOrDefault(p, 0) handles STOCK and the check for SELL.
After a sale, if the new stock is 0, remove the product (or return null from a merge function).
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor