#C14031. Bank Account Simulator
Bank Account Simulator
Bank Account Simulator
This problem requires you to simulate a simple bank account management system. You need to implement a system that supports a series of operations on bank accounts. The system should support the following operations:
- CREATE name initial_balance: Create a new bank account with the given owner name and an initial balance (a non-negative real number). The system assigns a unique account ID starting from 1.
- DEPOSIT account_id amount: Deposit a positive amount into the account specified by its ID.
- WITHDRAW account_id amount: Withdraw a positive amount from the specified account, if the account has sufficient funds. Otherwise, report an error.
- TRANSFER from_account_id to_account_id amount: Transfer a positive amount from one account to another if possible. Both account IDs must be valid and the source account must have sufficient funds.
- GET_BALANCE account_id: Output the current balance of the given account.
If any operation (except GET_BALANCE) fails due to an invalid account id, negative or zero amounts (where not allowed), or insufficient funds, your program must output error
on its own line immediately. All successful operations (except GET_BALANCE) produce no output.
Notes:
- For any error during an operation, output
error
. - All amounts are represented as floats. You may assume that the input will be such that no rounding issues occur, but it is recommended to use standard floating point arithmetic.
- You must process the operations in the order they are provided from standard input.
For details, note that a deposit is only allowed if the amount is strictly positive. Similarly, withdrawals and transfers require the amount to be strictly positive. In mathematical terms, if x is the amount then it must satisfy \( x > 0 \).
inputFormat
Input is read from standard input. The first line contains an integer \( N \) representing the number of operations. The following \( N \) lines each contain one operation. Each operation is in one of the following formats:
CREATE name initial_balance DEPOSIT account_id amount WITHDRAW account_id amount TRANSFER from_account_id to_account_id amount GET_BALANCE account_id
All tokens are separated by spaces. For example, to create an account for Alice with an initial balance of 100.0, the command is:
CREATE Alice 100.0
outputFormat
Output is written to standard output. For each GET_BALANCE
operation, output the current balance of the account on its own line. If an operation fails (for example, due to invalid account id, insufficient funds, or non-positive transaction amounts), output a line with the text error
immediately when that operation is processed. No output is produced for successful operations other than GET_BALANCE
.
6
CREATE Alice 100.0
GET_BALANCE 1
DEPOSIT 1 50.0
GET_BALANCE 1
WITHDRAW 1 70.0
GET_BALANCE 1
100.0
150.0
80.0
</p>