#K77007. Bookstore Inventory Management

    ID: 34768 Type: Default 1000ms 256MiB

Bookstore Inventory Management

Bookstore Inventory Management

You are managing the inventory for a bookstore. The system handles two types of commands issued as input lines:

  • Inventory Update: U book_id quantity — This increases the available stock of the book with the given book_id by the specified quantity.
  • Purchase Request: P book_id quantity — This attempts to sell the requested quantity of the book with the given book_id.

For each purchase request, the program should output one of the following responses (where book_id is printed as provided):

  • book_id successful — if the book exists in stock and there is sufficient quantity to complete the purchase.
  • book_id insufficient stock — if the book exists, but the available quantity is less than the requested amount.
  • book_id out of stock — if the book does not exist in the inventory.
  • </p>

    You must process the input from standard input (stdin) and write the results to standard output (stdout). The commands are given one per line. The order of operations must be preserved, and inventory updates will affect subsequent purchase requests.

    For example, if the input is as follows:

    U 101 3
    U 202 5
    P 101 2
    P 202 6
    P 303 1
    P 202 3
    

    The expected output is:

    101 successful
    202 insufficient stock
    303 out of stock
    202 successful
    

    Note: The formula for updating the inventory after a purchase can be expressed using the equation in \( \text{new_inventory} = \text{current_inventory} - \text{purchase_quantity} \) when the purchase is successful.

    inputFormat

    The input is read from standard input (stdin) and consists of several lines. Each line is in one of the following formats:

    • U book_id quantity for an inventory update
    • P book_id quantity for a purchase request

    There is no specific number of lines provided; your program should process all lines until the end of input.

    outputFormat

    For each purchase request encountered in the input, output a single line on standard output (stdout) indicating the result in one of the following formats:

    • book_id successful
    • book_id insufficient stock
    • book_id out of stock

    The output order should follow the order in which purchase requests appear in the input.

    ## sample
    U 101 3
    U 202 5
    P 101 2
    P 202 6
    P 303 1
    P 202 3
    
    101 successful
    

    202 insufficient stock 303 out of stock 202 successful

    </p>