-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathbank_account.py
More file actions
98 lines (79 loc) · 2.97 KB
/
bank_account.py
File metadata and controls
98 lines (79 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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.
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'
>>> 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()