#C12258. Library Book Management System
Library Book Management System
Library Book Management System
This problem requires you to implement a library management system that supports adding books, borrowing books, returning books, and listing books that are either available or borrowed. Each book has a title, an author, and a unique id. The borrowing and returning operations should handle errors appropriately: if a book does not exist or if the book is already borrowed (or not borrowed when returning), the program must output an error message. The operations are processed sequentially and the output is produced for list operations or when errors occur.
Operations:
- add <title> <author> <id>: Adds a new book to the library.
- borrow <id> <user>: Marks the book with the given id as borrowed by the given user. If the book does not exist or is already borrowed, output the corresponding error message.
- return <id>: Marks the book with the given id as returned. If the book is not borrowed, output the error message.
- list_available: Lists all available books (books not currently borrowed) by their titles, separated by a space.
- list_borrowed: Lists all borrowed books with their borrower in the format title:user, separated by a space.
Note: All input is read from stdin and all output is printed to stdout. If an error occurs in a non-list operation, simply output the error message.
For example, an operation sequence might be:
5 add 1984 Orwell 1 add Mockingbird Lee 2 list_available borrow 1 User1 list_available
The expected output would be:
1984 Mockingbird Mockingbird
inputFormat
The input begins with a single integer T representing the number of operations. The next T lines contain one operation per line in one of the following formats:
- add
- borrow
- return
- list_available
- list_borrowed
Notes:
- Titles and authors are strings without spaces.
- For 'add', is an integer.
- For 'borrow' and 'return', is an integer.
- Operations are processed sequentially.
outputFormat
For each operation:
- For 'list_available', output a single line with available book titles separated by a space. If none, output an empty line.
- For 'list_borrowed', output a single line with each borrowed book in the format title:user separated by a space. If none, output an empty line.
- For 'add', 'borrow', and 'return', output nothing unless an error occurs, in which case output the error message on a single line.## sample
5
add 1984 Orwell 1
add Mockingbird Lee 2
list_available
borrow 1 User1
list_available
1984 Mockingbird
Mockingbird
</p>