#C14698. Library Management System Simulation
Library Management System Simulation
Library Management System Simulation
You are required to simulate a simple library management system. The system supports adding books, displaying all books, borrowing books, returning books, searching for books, and deleting books based on a series of commands. Each book has an identifier, title, author, publication year, genre, and available quantity.
The commands are given in the following format:
ADD <book_id> <title> <author> <publication_year> <genre> <quantity>
: Add a new book. If a book with the given ID already exists, outputError
.DISPLAY
: Display all the books in the order they were added. For each book, printtitle author quantity
on a separate line. If there are no books, output nothing.BORROW <book_id>
: Borrow a book by its ID. If the book does not exist or is unavailable (quantity is 0), outputError
. Otherwise, decrease the quantity by 1.RETURN <book_id>
: Return a book by its ID. If the book does not exist, outputError
. Otherwise, increase the quantity by 1.SEARCH <keyword>
: Search for books whose title, author, or genre contains the given keyword (case insensitive) and display each matching book on a new line with details in the format:title author publication_year genre quantity
. If no matches are found, output nothing.DELETE <book_id>
: Delete a book by its ID. If the book does not exist, outputError
.
All error messages should exactly be Error
(case sensitive). Only commands that require output (DISPLAY and SEARCH, or errors) should print output.
The system reads the number of commands from standard input and processes each command in order, writing output to standard output.
The operations must be executed according to their order of appearance.
Additionally, any mathematical expressions or important notes should be formatted in LaTeX. For example, when updating the quantity, you can note that \(quantity = quantity - 1\) when a book is borrowed.
inputFormat
The input starts with an integer n
indicating the number of commands. The next n
lines each contain a command in one of the following formats:
ADD <book_id> <title> <author> <publication_year> <genre> <quantity>
DISPLAY
BORROW <book_id>
RETURN <book_id>
SEARCH <keyword>
DELETE <book_id>
All fields are space-separated and you can assume that none of the string fields contain spaces.
outputFormat
Output the results as follows:
- For
DISPLAY
, print each book's details (title author quantity
) on a separate line in the order they were added to the system. - For
SEARCH
, print each matching book's details (title author publication_year genre quantity
) on a separate line in the order they were added. - For commands
ADD
,BORROW
,RETURN
, andDELETE
, if the operation fails (for instance, due to duplicate IDs or non-existent books or unavailable quantity), printError
on a separate line. Successful operations produce no output.
All outputs should be printed to standard output.
## sample7
ADD 1 Book1 Author1 2021 Genre1 3
ADD 2 Book2 Author2 2022 Genre2 2
DISPLAY
BORROW 1
DISPLAY
SEARCH Book1
DELETE 2
Book1 Author1 3
Book2 Author2 2
Book1 Author1 2
Book1 Author1 2021 Genre1 2
</p>