Library Management
Model a small lending library: stock copies of a title, lend them out one-per-reader, take them back, and answer how many are on the shelf.
Requirements
- A title is identified by its
isbn;addBookmay be called several times for the same ISBN and the copies accumulate. - A user may hold at most one copy of a given ISBN at a time — a second
borrowof the same ISBN by the same user fails while they still hold it (borrowing a different ISBN is fine). borrowsucceeds only when a copy is available and the user isn't already holding that ISBN; it then decrements availability by one.returnBooksucceeds only when the user currently holds a copy of that ISBN; it then increments availability by one.availablereports the copies currently on the shelf and returns 0 for an unknown ISBN — querying never creates a title.
API to implement
Implement the class Library. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| addBook <isbn> <copies> | — | Add copies copies of the title isbn to the shelf. May be called repeatedly for the same ISBN — the copies accumulate. |
| borrow <user> <isbn> | boolean | user borrows one copy of isbn. Returns true (and decrements availability) only if a copy is free and this user isn't already holding a copy of that ISBN; otherwise false. |
| returnBook <user> <isbn> | boolean | user returns a copy of isbn. Returns true (and increments availability) only if this user currently holds a copy of that ISBN; otherwise false. |
| available <isbn> | int | Copies of isbn currently on the shelf. Returns 0 for an ISBN that was never added. |
Input format
The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
6
addBook 111 2
available 111
borrow alice 111
available 111
returnBook alice 111
available 1112
true
1
true
26
addBook 222 3
borrow bob 222
borrow bob 222
available 222
returnBook bob 222
returnBook bob 222true
false
2
true
false