Loading...
Loading...
A stationery shop adds products, restocks them and sells them, and prints a report on request. Selling more than the stock is refused.
Write a class Product (fields name, price, stock) and a class Shop with a Product[] field, a count, a sales total and methods Product find(String name), void add(String name, long price, int qty) and long sell(Product p, int qty). Adding a name that already exists only adds to its stock and keeps the old price.
add NAME price qty, sell NAME qty or report.ADDED NAME or RESTOCKED NAME newStock for add; BILL amount, ONLY s LEFT or NO PRODUCT NAME for sell; for report, one line NAME price stock per product in the order they were first added, then SALES total.Example
Input
6 add pen 10 50 add book 60 5 sell pen 3 sell book 6 add pen 99 10 report
Output
ADDED pen ADDED book BILL 30 ONLY 5 LEFT RESTOCKED pen 57 pen 10 57 book 60 5 SALES 30
Six books cannot be sold from five; the restock keeps the pen price at 10.
Try the problem first. Open one hint at a time.
find loops over the first count slots and compares names with equals, not ==.
Set shop.products = new Product[q] after new Shop(): there are never more products than commands.
sell lowers the stock and adds the bill to sales in one place.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor