#C13905. Bank Account Simulation
Bank Account Simulation
Bank Account Simulation
This problem requires you to simulate a simple banking system. You need to design a BankAccount class that supports the following operations:
- Deposit: Adds an amount to the account if the amount is positive. On success, it should output:
Deposited {amount}. New balance is {balance}.
. If the deposit amount is not positive, output:Deposit amount must be positive.
- Withdraw: Deducts an amount from the account if the amount is positive and sufficient funds are available. On success, it should output:
Withdrew {amount}. New balance is {balance}.
. If the withdrawal amount exceeds the current balance, output:Insufficient funds.
. If the withdrawal amount is not positive, output:Withdrawal amount must be positive.
- Balance: Reports the current balance as an integer.
The balance update follows the formula: \(\text{balance}_{new} = \text{balance}_{old} \pm \text{amount}\).
You will receive a series of commands, and your program must process each command accordingly.
inputFormat
The input begins with a line containing an integer representing the initial balance of the account.
The second line contains an integer n representing the number of operations.
Each of the following n lines contains a command. A command is one of the following:
deposit X
— Deposit an amount X into the account.withdraw X
— Withdraw an amount X from the account.balance
— Query the current balance of the account.
Note: X is an integer.
outputFormat
For each command in the input, output the result on a separate line:
- If the command is
deposit
orwithdraw
, output the corresponding message. - If the command is
balance
, output the current balance as an integer.
100
3
balance
withdraw 50
balance
100
Withdrew 50. New balance is 50.
50
</p>