#K81727. Library Book Management System
Library Book Management System
Library Book Management System
You are required to implement a simple library management system which supports three operations:
- BORROW: A user borrows a book. If the book is available (i.e. its stock is greater than 0), it will decrease the stock by 1 and return
SUCCESS
; otherwise, it returnsOUT OF STOCK
. - RETURN: A user returns a book. This operation increases the stock by 1 for the book and returns
RETURNED
. - STOCK: Check the available stock for a given book, printing the current stock as an integer.
Internally, the system starts with zero stock for every book. A book's stock is only increased when a RETURN
command is executed. You may assume that any RETURN
command is valid. The operations follow the rules below:
For a book with ID B, let its current stock be \( S_B \). Then:
- For
BORROW
: If \( S_B > 0 \) then update \( S_B \gets S_B - 1 \) and printSUCCESS
; otherwise, printOUT OF STOCK
. - For
RETURN
: Update \( S_B \gets S_B + 1 \) and printRETURNED
. - For
STOCK
: Output the integer value \( S_B \).
inputFormat
The input is read from standard input and consists of multiple lines. The first line contains a single integer \( N \) representing the number of commands. Each of the following \( N \) lines contains one command. Each command is one of the following formats:
STOCK <book_id>
BORROW <user_id> <book_id>
RETURN <user_id> <book_id>
outputFormat
The output should be printed to standard output. For each command, output the result on a new line. The possible outputs are:
- An integer (for the
STOCK
command). - The strings
SUCCESS
,OUT OF STOCK
, orRETURNED
forBORROW
andRETURN
commands.
6
STOCK B001
BORROW U001 B001
STOCK B001
BORROW U002 B001
RETURN U001 B001
STOCK B001
0
OUT OF STOCK
0
OUT OF STOCK
RETURNED
1
</p>