-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Showing
1 changed file
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class BankAccount: | ||
""" | ||
Represents a bank account with a balance and transaction history. | ||
""" | ||
def __init__(self, account_number, balance=0): | ||
""" | ||
Initializes a new bank account with the specified account number and balance. | ||
""" | ||
self.account_number = account_number | ||
self.balance = balance | ||
self.transaction_history = [] | ||
|
||
def deposit(self, amount): | ||
""" | ||
Deposits the specified amount into the bank account. | ||
""" | ||
self.balance += amount | ||
self.transaction_history.append(f"Deposit: ${amount:.2f}") | ||
|
||
def withdraw(self, amount): | ||
""" | ||
Withdraws the specified amount from the bank account, if sufficient funds are available. | ||
""" | ||
if self.balance >= amount: | ||
self.balance -= amount | ||
self.transaction_history.append(f"Withdraw: ${amount:.2f}") | ||
else: | ||
raise InsufficientFundsError(f"Insufficient funds to withdraw ${amount:.2f} from account {self.account_number}") | ||
|
||
class InsufficientFundsError(Exception): | ||
""" | ||
Raised when there are insufficient funds to complete a withdrawal. | ||
""" | ||
pass |