From c2c51fc5d6b7abd493bceafe98b0472cb5221419 Mon Sep 17 00:00:00 2001 From: nickzerjeski Date: Mon, 13 Apr 2026 10:44:32 +0200 Subject: [PATCH 1/4] feat(other): add basic BankAccount OOP class --- other/bank_account.py | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 other/bank_account.py diff --git a/other/bank_account.py b/other/bank_account.py new file mode 100644 index 000000000000..c3aa5a07b1ef --- /dev/null +++ b/other/bank_account.py @@ -0,0 +1,60 @@ +""" +Simple BankAccount class demonstrating core OOP concepts. +""" + + +class BankAccount: + """ + Basic bank account model with encapsulated account number and balance. + + >>> account = BankAccount("ACC-1001", initial_balance=100.0) + >>> account.balance + 100.0 + >>> account.deposit(50) + 150.0 + >>> account.withdraw(20) + 130.0 + >>> account.account_number + 'ACC-1001' + """ + + def __init__(self, account_number: str, initial_balance: float = 0.0) -> None: + if not account_number: + raise ValueError("account_number must be provided") + if initial_balance < 0: + raise ValueError("initial_balance cannot be negative") + + self.__account_number = account_number + self._balance = float(initial_balance) + + @property + def account_number(self) -> str: + """Read-only public accessor for the encapsulated account number.""" + return self.__account_number + + @property + def balance(self) -> float: + """Current account balance.""" + return self._balance + + def deposit(self, amount: float) -> float: + """Deposit a positive amount and return updated balance.""" + if amount <= 0: + raise ValueError("deposit amount must be positive") + self._balance += amount + return self._balance + + def withdraw(self, amount: float) -> float: + """Withdraw a positive amount and return updated balance.""" + if amount <= 0: + raise ValueError("withdraw amount must be positive") + if amount > self._balance: + raise ValueError("insufficient funds") + self._balance -= amount + return self._balance + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 1661ee00f5cab450583f1a9b9651f771d7145b44 Mon Sep 17 00:00:00 2001 From: Nick Zerjeski <57059725+nickzerjeski@users.noreply.github.com> Date: Tue, 14 Apr 2026 10:18:12 +0200 Subject: [PATCH 2/4] Update other/bank_account.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- other/bank_account.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/other/bank_account.py b/other/bank_account.py index c3aa5a07b1ef..6ada347d6240 100644 --- a/other/bank_account.py +++ b/other/bank_account.py @@ -1,5 +1,8 @@ """ Simple BankAccount class demonstrating core OOP concepts. + +Reference: +https://docs.python.org/3/tutorial/classes.html """ From 5e94a72b92687061ccfd9af9b378a4e02e2511a1 Mon Sep 17 00:00:00 2001 From: Nick Zerjeski <57059725+nickzerjeski@users.noreply.github.com> Date: Tue, 14 Apr 2026 10:18:20 +0200 Subject: [PATCH 3/4] Update other/bank_account.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- other/bank_account.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/other/bank_account.py b/other/bank_account.py index 6ada347d6240..f501ae07382f 100644 --- a/other/bank_account.py +++ b/other/bank_account.py @@ -19,6 +19,31 @@ class BankAccount: 130.0 >>> account.account_number 'ACC-1001' + + >>> BankAccount("", initial_balance=10.0) + Traceback (most recent call last): + ... + ValueError: account_number must be provided + + >>> BankAccount("ACC-1002", initial_balance=-1.0) + Traceback (most recent call last): + ... + ValueError: initial_balance cannot be negative + + >>> account.deposit(0) + Traceback (most recent call last): + ... + ValueError: deposit amount must be positive + + >>> account.withdraw(0) + Traceback (most recent call last): + ... + ValueError: withdraw amount must be positive + + >>> account.withdraw(1000) + Traceback (most recent call last): + ... + ValueError: insufficient funds """ def __init__(self, account_number: str, initial_balance: float = 0.0) -> None: From d28ec078bc353ad0f710ed9693291cf4f168a482 Mon Sep 17 00:00:00 2001 From: Nick Zerjeski <57059725+nickzerjeski@users.noreply.github.com> Date: Tue, 14 Apr 2026 10:18:31 +0200 Subject: [PATCH 4/4] Update other/bank_account.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- other/bank_account.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/other/bank_account.py b/other/bank_account.py index f501ae07382f..97137d21dc4a 100644 --- a/other/bank_account.py +++ b/other/bank_account.py @@ -2,14 +2,24 @@ Simple BankAccount class demonstrating core OOP concepts. Reference: -https://docs.python.org/3/tutorial/classes.html +Invariant-preserving bank account state model. + +This module implements a minimal transactional data model whose computational +value is the enforcement of account invariants during constant-time state +transitions: account identifiers must be present, balances may not start +negative, deposits must be positive, and withdrawals may not overdraw the +account. """ class BankAccount: """ - Basic bank account model with encapsulated account number and balance. + Stateful account abstraction with validated balance updates. + The class is intentionally small, but its purpose is not to serve as a + generic OOP tutorial. It provides a compact example of how to model a + mutable record while preserving correctness constraints across operations, + with each transaction executing in O(1) time. >>> account = BankAccount("ACC-1001", initial_balance=100.0) >>> account.balance 100.0