forked from reingart/exercism
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aayush Badoni
authored and
Aayush Badoni
committed
Apr 22, 2024
1 parent
7495b50
commit 606eb65
Showing
29 changed files
with
1,210 additions
and
566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,79 @@ | ||
class BankAccount: | ||
def __init__(self): | ||
# Initialize balance to zero and account status to closed | ||
"""Initialize a BankAccount object. | ||
The balance is set to zero and the account status is closed by default. | ||
""" | ||
self._balance = 0 | ||
self._opened = False | ||
|
||
def _raise_error_if_not_open(self): | ||
# Check if account is not open, raise ValueError if closed | ||
"""Raise ValueError if the account is closed.""" | ||
if not self._opened: | ||
raise ValueError("account not open") | ||
|
||
def _raise_error_if_amount_less_than_0(self, amount): | ||
# Check if amount is less than 0, raise ValueError if negative | ||
"""Raise ValueError if the amount is less than zero.""" | ||
if amount < 0: | ||
raise ValueError("amount must be greater than 0") | ||
|
||
def get_balance(self): | ||
# Check if account is open, return balance if open | ||
"""Get the current balance of the account. | ||
Returns: | ||
int: The current balance of the account. | ||
Raises: | ||
ValueError: If the account is closed. | ||
""" | ||
self._raise_error_if_not_open() | ||
return self._balance | ||
|
||
def open(self): | ||
# Check if account is already open, raise ValueError if already open | ||
"""Open the bank account. | ||
Raises: | ||
ValueError: If the account is already open. | ||
""" | ||
if self._opened: | ||
raise ValueError("account already open") | ||
# Set account status to open | ||
self._opened = True | ||
|
||
def deposit(self, amount): | ||
# Check if account is open and amount is valid | ||
"""Deposit funds into the bank account. | ||
Args: | ||
amount (int): The amount to deposit. | ||
Raises: | ||
ValueError: If the account is closed or the amount is negative. | ||
""" | ||
self._raise_error_if_not_open() | ||
self._raise_error_if_amount_less_than_0(amount) | ||
# Add deposit amount to balance | ||
self._balance += amount | ||
|
||
def withdraw(self, amount): | ||
# Check if account is open, amount is valid, and balance is sufficient | ||
"""Withdraw funds from the bank account. | ||
Args: | ||
amount (int): The amount to withdraw. | ||
Raises: | ||
ValueError: If the account is closed, the amount is negative, | ||
or the withdrawal amount exceeds the balance. | ||
""" | ||
self._raise_error_if_not_open() | ||
self._raise_error_if_amount_less_than_0(amount) | ||
if amount > self._balance: | ||
raise ValueError("amount must be less than balance") | ||
# Deduct withdrawal amount from balance | ||
self._balance -= amount | ||
|
||
def close(self): | ||
# Check if account is open, close the account, reset balance to zero | ||
"""Close the bank account and reset the balance to zero. | ||
Raises: | ||
ValueError: If the account is already closed. | ||
""" | ||
self._raise_error_if_not_open() | ||
self._opened = False | ||
self._balance = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.