Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
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

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

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

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
61 changes: 61 additions & 0 deletions billing/tests/test_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest

from billing.accounts import BankAccount


class TestBankAccount(unittest.TestCase):
def setUp(self):
self.account = BankAccount("John Doe", 100.0)

def test_init(self):
self.assertEqual(self.account.owner, "John Doe")
self.assertEqual(self.account.balance, 100.0)
with self.assertRaises(ValueError):
BankAccount("Jane Doe", -50.0)

def test_deposit(self):
new_balance = self.account.deposit(50.0)
self.assertEqual(new_balance, 150.0)
self.assertEqual(self.account.balance, 150.0)
with self.assertRaises(ValueError):
self.account.deposit(-10.0)
with self.assertRaises(ValueError):
self.account.deposit(0)

def test_withdraw(self):
new_balance = self.account.withdraw(50.0)
self.assertEqual(new_balance, 50.0)
self.assertEqual(self.account.balance, 50.0)
with self.assertRaises(ValueError):
self.account.withdraw(-10.0)
with self.assertRaises(ValueError):
self.account.withdraw(0)
with self.assertRaises(ValueError):
self.account.withdraw(1000.0)

def test_transfer(self):
other_account = BankAccount("Jane Doe", 50.0)
balances = self.account.transfer(other_account, 30.0)
self.assertEqual(balances, (70.0, 80.0))
self.assertEqual(self.account.balance, 70.0)
self.assertEqual(other_account.balance, 80.0)

with self.assertRaises(ValueError):
self.account.transfer(other_account, -10.0)
with self.assertRaises(ValueError):
self.account.transfer(other_account, 0)
with self.assertRaises(ValueError):
self.account.transfer(other_account, 1000.0)
with self.assertRaises(ValueError):
self.account.transfer("not an account", 10.0)

def test_get_balance(self):
self.assertEqual(self.account.get_balance(), 100.0)
self.account.deposit(50.0)
self.assertEqual(self.account.get_balance(), 150.0)
self.account.withdraw(30.0)
self.assertEqual(self.account.get_balance(), 120.0)


if __name__ == "__main__":
unittest.main()
Loading