#C13484. ATM Simulation
ATM Simulation
ATM Simulation
This problem requires you to simulate a basic ATM (Automated Teller Machine) system. You need to implement functionalities such as deposit, withdraw, and check balance. The ATM starts with a balance of 0. Each operation should output a confirmation or error message. For an operation:
- deposit X: Increase the balance by X. If X is negative, print an error message.
- withdraw X: Decrease the balance by X provided that there are sufficient funds and X is non-negative; otherwise, output an appropriate error message.
- check: Print the current balance.
The associated messages must follow the format below:
- For a valid deposit:
Deposit of X successful. New balance: Y
- For a valid withdrawal:
Withdrawal of X successful. New balance: Y
- For a balance check:
Current balance: Y
- If an operation is invalid (e.g. negative amount or insufficient funds), print a corresponding error message:
- Deposit Error:
Invalid amount. Deposit amount must be non-negative.
- Withdrawal Error:
Invalid amount. Withdrawal amount must be non-negative.
orInsufficient funds for the withdrawal or invalid amount.
Note: You are also expected to log each transaction internally (although the log is not visible in the output). The transaction log format is not required for the output.
In your solution, any mathematical formulas should be written in LaTeX format if needed.
inputFormat
The first line contains an integer n which indicates the number of operations. The next n lines each contain an operation.
Each operation is one of the following:
deposit X
: Deposit an integer amount X into the ATM.withdraw X
: Withdraw an integer amount X from the ATM.check
: Check the current balance.
outputFormat
For each operation, output a line with the result message.
- If a deposit is successful, print:
Deposit of X successful. New balance: Y
. - If a withdrawal is successful, print:
Withdrawal of X successful. New balance: Y
. - If a balance check is performed, print:
Current balance: Y
. - If an operation is invalid, print the corresponding error message:
- For deposit with a negative amount:
Invalid amount. Deposit amount must be non-negative.
- For withdrawal with a negative amount:
Invalid amount. Withdrawal amount must be non-negative.
- For withdrawal with insufficient funds:
Insufficient funds for the withdrawal or invalid amount.
3
deposit 100
withdraw 50
check
Deposit of 100 successful. New balance: 100
Withdrawal of 50 successful. New balance: 50
Current balance: 50
</p>