This project puts the whole module together. The shop wants a restock report: every product the supplier has fewer than 10 of, with its stock and its price, sorted by name. No single answer has all of that. /prices has the names and prices, and /stock has the counts, but only if you send the X-Api-Key header.
Joining two answers: both lists use the same id for a product, and that is the only thing that links them. Turn the stock list into a Map<Integer, Integer> that maps each id to its count. Then stock.get(product.get("id")) gives any product's stock in one step, with no searching.
Sorting maps: Java can't compare two maps on its own. Give sort a Comparator: Comparator.comparing takes a method that gets one item and returns the value to sort by. For example, to sort a cart by price:
static Integer byPrice(Map<String, Object> item) {
return (Integer) item.get("price");
}
cart.sort(Comparator.comparing(Lesson::byPrice));
Pass the method with :: and no parentheses: sort calls it on every item for you. Your program must print exactly these lines:
Lamp: 8 left, price 14
Notebook: 0 left, price 3
Plant: 0 left, price 6
Your task: SUPPLIER and API_KEY are given, and the starting code already reads /prices into prices. Read /stock with the key, build the map stock from id to count, then print every product with fewer than 10 left, sorted by name: its name with a colon, its stock, left, price and its price.
Assignment
Follow the Coding Tutorial to build the restock report: the stock request, a map of stock by id, the printing loop and a sort by name.
Hint
Build stock before the loop, starting from new HashMap<>(). In the loop, compare stock.get(product.get("id")) with 10 using <. Sort prices with Comparator.comparing(Lesson::byName) just before the loop.
Free plan: 30 tutor messages. PRO is unlimited.
