#C13381. Bank Account Operations
Bank Account Operations
Bank Account Operations
You are required to implement a bank account manager that supports depositing money, withdrawing money, and querying the balance. The bank account is instantiated with an initial balance. However, if the initial balance is negative, a NegativeAmountError
exception must be raised.
The operations are defined as follows:
- deposit amount: Add a positive amount to the account balance. If the deposit amount is not positive, raise
NegativeAmountError
. - withdraw amount: Remove a positive amount from the account balance. If the withdraw amount is not positive or if the amount exceeds the current balance, raise
NegativeAmountError
orInsufficientFunds
respectively. - balance: Output the current balance of the account.
In mathematical notation, the following conditions must hold:
\( initial\_balance \ge 0 \)
\( deposit\_amount > 0 \)
\( withdraw\_amount > 0 \) and \( withdraw\_amount \leq current\_balance \)
If any operation fails, your program must output the corresponding exception message (NegativeAmountError
or InsufficientFunds
) and terminate immediately.
inputFormat
The input is read from stdin and has the following format:
- A line with a floating point number representing the initial balance.
- A line with an integer n indicating the number of operations.
- n lines, each containing one of the following commands:
deposit x
— deposit an amount x (a float).withdraw x
— withdraw an amount x (a float).balance
— print the current balance.
outputFormat
The output should be written to stdout. For each balance
command, output the current balance (as a float) on a new line. If any operation fails because of an exception, output the exception's message (NegativeAmountError
or InsufficientFunds
) and terminate the program immediately, without processing further commands.
100.0
5
deposit 50.0
withdraw 25.0
balance
withdraw 100.0
balance
125.0
25.0
</p>