#C14236. Library Management System
Library Management System
Library Management System
You are required to implement a thread-safe Library Management System that supports four types of operations:
1. ADD: Add a new book to the system. Each book has a unique ID assigned sequentially starting from 1. The details of a book include its title, author, genre, and publication year. The add operation should return the assigned book ID.
2. REMOVE: Remove an existing book by its unique ID. This operation should return True
if the removal is successful and False
if no such book exists.
3. SEARCH: Search for books by a query string. The operation can search by either the book's title or the author's name (case-insensitive). The default search field is title
if not specified. The search operation should return the count of matching books.
4. UPDATE: Update the genre and publication year of a book identified by its ID. If the update is successful, print all the details of the updated book in the following format: id title author genre year
. If no such book exists, output None
.
The operations are processed sequentially. Although multi-threaded access is suggested via a lock (i.e. (\texttt{lock})), for the purpose of this problem, a correct sequential implementation is sufficient.
inputFormat
The first line of input contains an integer (Q) representing the number of operations. Each of the next (Q) lines contains an operation in one of the following formats:
ADD title;author;genre;year – Add a new book. Example: ADD Python Programming;John Doe;Programming;2020
.
REMOVE id – Remove a book by its unique ID. Example: REMOVE 1
.
SEARCH query[;search_by] – Search for books where the specified field contains the given query (case-insensitive). The search_by
field is optional and defaults to title
if not provided. Example: SEARCH Python
or SEARCH Jane;author
.
UPDATE id;genre;year – Update the genre and publication year of the book with the given ID. Example: UPDATE 1;Software Development;2021
.
outputFormat
For each operation, output a single line with the result:
- For ADD, output the assigned book ID.
- For REMOVE, output True
if the book was removed, False
otherwise.
- For SEARCH, output the count of matching books.
- For UPDATE, if the update is successful, output the updated book details in the format: id title author genre year
(fields separated by a space); if the book does not exist, output None
.## sample
4
ADD Python Programming;John Doe;Programming;2020
ADD Advanced Python;Jane Doe;Programming;2019
SEARCH Python
REMOVE 1
1
2
2
True
</p>