-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
feat(other): add basic BankAccount OOP class (v3) #14549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickzerjeski
wants to merge
5
commits into
TheAlgorithms:master
Choose a base branch
from
nickzerjeski:renew-bank-account-14024-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2c51fc
feat(other): add basic BankAccount OOP class
nickzerjeski 1661ee0
Update other/bank_account.py
nickzerjeski 5e94a72
Update other/bank_account.py
nickzerjeski d28ec07
Update other/bank_account.py
nickzerjeski bc7f00c
Merge branch 'master' into renew-bank-account-14024-v2
nickzerjeski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """ | ||
| Simple BankAccount class demonstrating core OOP concepts. | ||
|
|
||
| Reference: | ||
| 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. | ||
|
|
||
nickzerjeski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| >>> account.deposit(50) | ||
| 150.0 | ||
| >>> account.withdraw(20) | ||
| 130.0 | ||
| >>> account.account_number | ||
| 'ACC-1001' | ||
nickzerjeski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| >>> 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: | ||
| 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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.