#K34147. Implement a Book Issue Context Manager
Implement a Book Issue Context Manager
Implement a Book Issue Context Manager
You are required to implement two classes: Book
and IssueBook
. A Book
object has a title and an issued flag which is initially set to false
. The class IssueBook
acts as a context manager (or its equivalent in your language) that sets the book's issued flag to true
when entering the context, and resets it to false
when exiting the context, no matter if an exception occurred or not.
In particular, your implementation should satisfy the following conditions:
- When a
Book
is created, its issued flag should befalse
. - When using the context manager on a book, inside the context the issued flag must be
true
. - Upon exiting the context (even if an exception is thrown inside the context), the issued flag must be reset to
false
. - In the case of managing multiple books, only the book associated with the current context should have its issued flag set to
true
inside the context, while the other remains unchanged.
Note: If your language supports automatic resource management (such as RAII, try-with-resources, or context managers), make sure to use it appropriately.
For any formulas required, use the LaTeX format. (There is no mathematical formula needed in this problem.)
inputFormat
The input is read from standard input (stdin). The first line contains an integer T
representing the number of test scenarios. Each test scenario starts with a command type (one of INIT
, CONTEXT
, MULTIPLE
, or EXCEPTION
), followed by the required number of lines as described below:
- INIT: Followed by one line containing the book title. You must create a
Book
object and output its issued status. - CONTEXT: Followed by one line containing the book title. Create a
Book
object, then simulate using theIssueBook
context manager by printing the issued status inside the context and then printing the status after the context is exited. - MULTIPLE: Followed by two lines, each containing a book title. Create two
Book
objects. Then, use the context manager only on the first book. Print the issued status of both books inside the context, then again print the issued statuses after the context. - EXCEPTION: Followed by one line containing the book title. Create a
Book
, then simulate entering the context and immediately throw an exception (simulate this in your code). Catch the exception and then print the issued status after the context has exited. Also print the issued status while inside the context before the exception is thrown.
All outputs should be printed to standard output (stdout), one result per line.
Example Input:
4 INIT The Great Gatsby CONTEXT The Great Gatsby MULTIPLE 1984 To Kill a Mockingbird EXCEPTION The Catcher in the Rye
outputFormat
For each test scenario, output the issued status of the book(s) as described below, each on a new line:
- INIT: Output one line:
False
. - CONTEXT: Output two lines. The first line should be
True
(inside the context) and the second lineFalse
(after exiting the context). - MULTIPLE: Output four lines. The first two lines are the statuses inside the context: the first book should be
True
and the secondFalse
. The next two lines are the statuses after the context: both should beFalse
. - EXCEPTION: Output two lines. The first line is
True
(inside the context before the exception) and the second line isFalse
(after exiting the context, even after an exception).
4
INIT
The Great Gatsby
CONTEXT
The Great Gatsby
MULTIPLE
1984
To Kill a Mockingbird
EXCEPTION
The Catcher in the Rye
False
True
False
True
False
False
False
True
False
</p>