#K94022. Library Management System
Library Management System
Library Management System
You are required to implement a library management system that supports the following operations:
- ADD book: Add a new book to the library catalog. The command is given as
ADD id title quantity
, whereid
is an integer representing the unique book identifier,title
is the title of the book (words are connected with underscores), andquantity
is the number of available copies. - BORROW book: Borrow a book if it is available. The command is given as
BORROW id
. If the book exists and there is at least one copy available, reduce its available quantity by one and increase its borrow count by one. Otherwise, ignore the command. - RETURN book: Return a borrowed book by increasing its available quantity by one. The command is given as
RETURN id
. It is guaranteed that this operation will only be invoked on books that exist in the system. - REPORT: Generate a report of all books that have been borrowed at least once. For each such book, output its id, title, and total number of times it was borrowed. The report must be sorted in non-increasing order by the total borrow count. If two books have the same borrow count, they are sorted in lexicographical order of their titles.
Note: All operations are processed in the order they are received. There will be exactly one REPORT command per test case, which will be the last command.
The sorting criteria can be mathematically described as follows. For each book i, let \( b_i \) denote the total borrow count and \( t_i \) denote the title. The books are sorted such that for any two books i and j, book i comes before book j if and only if \[ b_i > b_j \quad \text{or} \quad \Big(b_i = b_j \text{ and } t_i < t_j\Big). \]
inputFormat
The input begins with an integer n
representing the number of commands. Each of the following n
lines contains a command. Commands can be one of the following:
ADD id title quantity
BORROW id
RETURN id
REPORT
It is guaranteed that the last command is REPORT
, and titles are provided as a single token using underscores (_) instead of spaces.
outputFormat
Upon encountering the REPORT
command, output the details of each borrowed book (i.e., books with a borrow count greater than 0) on a separate line. Each line should contain three space-separated elements: id
, title
, and borrow_count
. If no book has been borrowed, output nothing.
10
ADD 1 The_Great_Gatsby 3
ADD 2 1984 2
ADD 3 To_Kill_a_Mockingbird 4
ADD 4 The_Catcher_in_the_Rye 1
BORROW 1
BORROW 1
BORROW 2
RETURN 1
BORROW 4
REPORT
1 The_Great_Gatsby 2
2 1984 1
4 The_Catcher_in_the_Rye 1
</p>