Skip to content

Commit

Permalink
Create accounts.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent de250ea commit 8affc59
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions banking/accounts.py
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

0 comments on commit 8affc59

Please sign in to comment.