#C12094. Library Catalog Management
Library Catalog Management
Library Catalog Management
You are required to implement a library catalog system with the following operations:
- add: Add a new book with a unique id, title, and author.
- remove: Remove a book by its id.
- update: Update the title or author of an existing book identified by its id. The field to update is given as either title or author.
- query: Query the catalog for a book by its id. If found, print the book details in the format
id title author
; otherwise, print "Book not found".
Operations will be given via standard input. You must read the commands, execute them accordingly, and output the result of every query command to standard output.
The answer should correctly handle edge cases, e.g. querying a non-existent book or updating/removing a book that is not in the catalog.
There are no explicit constraints, so you may assume that the number of operations is moderate.
For any formulas, if needed, use LaTeX format. (For example: $\text{result} = id \; title \; author$)
inputFormat
The first line contains a single integer n
which denotes the number of operations.
Each of the following n
lines contains an operation command in one of the following formats:
add id title author
— Add a new book with the givenid
(an integer),title
, andauthor
(both strings without spaces).remove id
— Remove the book with the givenid
.update id field new_value
— Update thefield
(eithertitle
orauthor
) of the book with the givenid
tonew_value
.query id
— Query for the book with the givenid
. For each query, output a line with the book details if found, or "Book not found" otherwise.
outputFormat
For each query
operation, output a single line.
- If the book exists, print:
id title author
. - If the book does not exist, print:
Book not found
.
4
add 1 TestTitle TestAuthor
query 1
remove 1
query 1
1 TestTitle TestAuthor
Book not found
</p>