#C14082. Simulate a Message Queue
Simulate a Message Queue
Simulate a Message Queue
This problem simulates a simple inter-service message queue system. In this system, several services (namely user, product, and order services) produce logs and events. When a user is added, an event is generated to send a welcome email. When an order is created, an event is generated to process the payment.
The order of processing events follows the First-In-First-Out (FIFO) principle, i.e. $$FIFO$$. Your task is to simulate this system by reading a series of commands from stdin
and outputting the corresponding log messages to stdout
.
The following commands are supported:
- ADD_USER user_id user_name: Adds a user. Prints "User user_id added" and enqueues an event to send a welcome email.
- ADD_PRODUCT product_id product_name: Adds a product. Prints "Product product_id added".
- CREATE_ORDER order_id user_id product_id: Creates an order. Prints "Order order_id created for user user_id" and enqueues an event to process payment.
- PROCESS: Processes the earliest event in the queue. For a user creation event, prints "Sending email to user user_id: Welcome user_name!"; for an order creation event, prints "Processing payment for order order_id by user user_id".
Note: If the PROCESS command is issued and the event queue is empty, nothing should be printed for that command.
inputFormat
The input begins with an integer T (T ≥ 1) indicating the number of commands. Each of the next T lines contains one of the following commands:
ADD_USER user_id user_name
ADD_PRODUCT product_id product_name
CREATE_ORDER order_id user_id product_id
PROCESS
All data items (user_id, product_id, order_id) are strings without spaces, and user_name or product_name is a single word.
outputFormat
For each command, output the corresponding message to stdout
in the order the commands are processed. The messages are:
- For
ADD_USER
: "User user_id added". - For
ADD_PRODUCT
: "Product product_id added". - For
CREATE_ORDER
: "Order order_id created for user user_id". - For
PROCESS
: If processing a user creation event, "Sending email to user user_id: Welcome user_name!"; if processing an order event, "Processing payment for order order_id by user user_id". If the queue is empty, nothing is printed.
5
ADD_USER 1 Alice
ADD_PRODUCT 101 Laptop
CREATE_ORDER 1001 1 101
PROCESS
PROCESS
User 1 added
Product 101 added
Order 1001 created for user 1
Sending email to user 1: Welcome Alice!
Processing payment for order 1001 by user 1
</p>