feat(other): add basic BankAccount OOP class (v3)#14549
feat(other): add basic BankAccount OOP class (v3)#14549nickzerjeski wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new BankAccount class under other/ intended to demonstrate basic OOP concepts (encapsulation via properties, simple state updates) and includes inline doctests.
Changes:
- Introduces
BankAccountwith validated initialization, balance accessor,deposit(), andwithdraw(). - Adds doctest examples in the class docstring and a
__main__doctest runner.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,60 @@ | |||
| """ | |||
| Simple BankAccount class demonstrating core OOP concepts. | |||
There was a problem hiding this comment.
The module docstring doesn’t include any source/explanation URL. New submissions in this repo typically include at least one link in the header docstring/comment to help readers (see CONTRIBUTING.md:57 and e.g. other/graham_scan.py:2-6). Consider adding a relevant reference link for the concept being demonstrated.
| Simple BankAccount class demonstrating core OOP concepts. | |
| Simple BankAccount class demonstrating core OOP concepts. | |
| Reference: | |
| https://docs.python.org/3/tutorial/classes.html |
| >>> account.withdraw(20) | ||
| 130.0 | ||
| >>> account.account_number | ||
| 'ACC-1001' |
There was a problem hiding this comment.
The doctests currently cover only the happy path. This repo often includes doctests for erroneous inputs/exceptions as well (e.g., financial/interest.py:20-31). Consider adding doctests for the validation branches (empty account_number, negative initial_balance, non-positive deposit/withdraw, withdraw exceeding balance) to ensure those errors are exercised.
| 'ACC-1001' | |
| '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 |
| Simple BankAccount class demonstrating core OOP concepts. | ||
| """ | ||
|
|
||
|
|
||
| class BankAccount: | ||
| """ | ||
| Basic bank account model with encapsulated account number and balance. | ||
|
|
There was a problem hiding this comment.
This file reads more like an OOP tutorial/example than an algorithm or data-structure implementation, which may conflict with the project’s contribution guidelines about avoiding “how-to examples” and focusing on algorithms with unique computational value (CONTRIBUTING.md:41-62). If it’s intended to stay, consider clarifying in the module/class docstring what algorithmic concept is being implemented and why it belongs in this repository.
| Simple BankAccount class demonstrating core OOP concepts. | |
| """ | |
| class BankAccount: | |
| """ | |
| Basic bank account model with encapsulated account number and balance. | |
| 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: | |
| """ | |
| 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. |
Describe your change:
Fixes #14024
Checklist: