Merchant and Payment Command Processor
Part 1: Basic Command Processing
Problem Description
You are given a list of commands to simulate a basic merchant and payment system. Each command is a string containing keywords and parameters separated by spaces. You must maintain a mapping of merchants and their balances, as well as a record of payments.
Each command can be one of the following:
INIT merchant_id balance
: Initialize a merchant with a specific balance.CREATE payment_id merchant_id amount
: Create a payment for a merchant. When a payment is successfully processed, the amount is added to the merchant’s balance.
Payment states and transitions are implied: once a payment is processed successfully, its amount is added to the merchant’s balance.
Input
A list of strings, each representing a command.
Output
Final state of each merchant's balance after all commands have been executed.
Example
Input:
[
"INIT m1 1000",
"CREATE p1 m1 200",
"CREATE p2 m1 300"
]
Output:
{
"m1": 1500
}
Part 2: Refund Feature
Problem Description
Building upon Part 1, support an additional command:
REFUND payment_id
: Refund the given payment. This subtracts the payment amount from the associated merchant's balance.
Only successfully created payments can be refunded.
Example
Input:
[
"INIT m1 1000",
"CREATE p1 m1 200",
"REFUND p1"
]
Output:
{
"m1": 1000
}
Part 3: Additional Command Variants
Problem Description
Add more commands or handle additional payment states if applicable. Commands so far include:
INIT
CREATE
REFUND
Command semantics remain the same. Processing logic should ensure correct balance updates.
Part 4: Timestamp and Refund Limit
Problem Description
Each command now includes a timestamp as the first element (an integer). When initializing a merchant, a refund_limit
is also included (in the same time unit as timestamps).
New format for INIT
:
timestamp INIT merchant_id balance refund_limit
The REFUND
command should only succeed if it occurs within refund_limit
time after the payment was created.
Example
Input:
[
"1 INIT m1 1000 10",
"2 CREATE p1 m1 200",
"5 REFUND p1",
"15 REFUND p1"
]
Output:
{
"m1": 1000
}
Explanation:
- Refund at timestamp 5 is allowed (within refund limit).
- Refund at timestamp 15 is denied (outside refund window).
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.
