|
|
|
import dataclasses |
|
import pytest |
|
|
|
|
|
@dataclasses.dataclass |
|
class BankAccount: |
|
"""Represents a simple bank account""" |
|
account_number: int |
|
account_holder: str |
|
balance: float = 0.0 |
|
|
|
def deposit(self, amount: float) -> None: |
|
"""Deposit money into the account""" |
|
self.balance += amount |
|
|
|
def withdraw(self, amount: float) -> None: |
|
"""Withdraw money from the account""" |
|
if amount > self.balance: |
|
raise ValueError("Insufficient balance") |
|
self.balance -= amount |
|
|
|
def get_balance(self) -> float: |
|
"""Get the current balance of the account""" |
|
return self.balance |
|
|
|
|
|
def create_account(account_number: int, account_holder: str) -> BankAccount: |
|
"""Create a new BankAccount instance""" |
|
return BankAccount(account_number, account_holder) |
|
|
|
|
|
def perform_transaction(account: BankAccount, amount: float, is_deposit: bool) -> None: |
|
"""Perform a transaction on the account""" |
|
if is_deposit: |
|
account.deposit(amount) |
|
else: |
|
account.withdraw(amount) |
|
|
|
|
|
def test_bank_account(): |
|
"""Test the BankAccount class""" |
|
account = create_account(12345, "John Doe") |
|
assert account.get_balance() == 0.0 |
|
perform_transaction(account, 100.0, True) |
|
assert account.get_balance() == 100.0 |
|
perform_transaction(account, 50.0, False) |
|
assert account.get_balance() == 50.0 |
|
|
|
|
|
pytest.main([__file__]) |
|
|
|
|
|
account = create_account(12345, "John Doe") |
|
|
|
|
|
perform_transaction(account, 100.0, True) |
|
perform_transaction(account, 50.0, False) |
|
|
|
|
|
print("Final balance:", account.get_balance()) |