Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions billing/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class BankAccount:
def __init__(self, owner, balance=0.0):
self.owner = owner
if balance < 0:
raise ValueError("Initial balance cannot be negative")
self.balance = balance

Check warning on line 6 in billing/accounts.py

View check run for this annotation

Codecov Notifications / codecov/patch

billing/accounts.py#L1-L6

Added lines #L1 - L6 were not covered by tests

def deposit(self, amount):
if amount <= 0:
raise ValueError("Deposit amount must be positive")
self.balance += amount
return self.balance

Check warning on line 12 in billing/accounts.py

View check run for this annotation

Codecov Notifications / codecov/patch

billing/accounts.py#L8-L12

Added lines #L8 - L12 were not covered by tests

def withdraw(self, amount):
if amount <= 0:
raise ValueError("Withdrawal amount must be positive")
if amount > self.balance:
raise ValueError("Insufficient funds")
self.balance -= amount
return self.balance

Check warning on line 20 in billing/accounts.py

View check run for this annotation

Codecov Notifications / codecov/patch

billing/accounts.py#L14-L20

Added lines #L14 - L20 were not covered by tests

def transfer(self, other_account, amount):
if not isinstance(other_account, BankAccount):
raise ValueError("Recipient must be a BankAccount instance")
self.withdraw(amount)
other_account.deposit(amount)
return self.balance, other_account.balance

Check warning on line 27 in billing/accounts.py

View check run for this annotation

Codecov Notifications / codecov/patch

billing/accounts.py#L22-L27

Added lines #L22 - L27 were not covered by tests
Comment on lines +22 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transfer method should handle the case where the transfer fails due to insufficient funds or other reasons. Currently, if self.withdraw(amount) raises an exception, the other_account.deposit(amount) will not be executed, but the exception will not be caught or handled.

Suggested change
def transfer(self, other_account, amount):
if not isinstance(other_account, BankAccount):
raise ValueError("Recipient must be a BankAccount instance")
self.withdraw(amount)
other_account.deposit(amount)
return self.balance, other_account.balance


def get_balance(self):
return self.balance

Check warning on line 30 in billing/accounts.py

View check run for this annotation

Codecov Notifications / codecov/patch

billing/accounts.py#L29-L30

Added lines #L29 - L30 were not covered by tests
Loading