-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank_account.py
76 lines (63 loc) · 2.43 KB
/
bank_account.py
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
class BankAccount:
"""
A class representing a bank account.
"""
def __init__(
self, account_number: int, account_holder: str, balance: float = 0
):
self.account_number = account_number
self.balance = balance
self.account_holder = account_holder
def deposit(self, amount: float) -> None:
"""
Deposits a specified amount into the account.
:param amount: The amount to be deposited. Must be positive.
:raises ValueError: If the amount is negative.
"""
if amount < 0:
raise ValueError("Amount must be positive.")
self.balance += amount
def withdraw(self, amount: float) -> None:
"""
Withdraws a specified amount from the account.
:param amount: The amount to be withdrawn.
Must be positive and less than or equal to the
current balance.
:raises ValueError: If the amount is negative or exceeds the
current balance.
"""
if amount < 0 or amount > self.balance:
raise ValueError("Insufficient funds or amount must be positive.")
self.balance -= amount
def get_balance(self) -> float:
"""
Returns the current balance of the account.
:return: The current balance.
"""
return self.balance
def transfer(self, amount: float, target_account: "BankAccount"):
"""
Transfers a specified amount to another bank account.
:param amount: The amount to be transferred.
Must be positive and less than or
equal to the current balance.
:param target_account: The target bank account to which the
amount will be transferred.
:raises ValueError: If the amount is negative or
exceeds the current balance.
"""
if amount < 0 or amount > self.balance:
raise ValueError("Insufficient funds or amount must be positive.")
self.balance -= amount
target_account.balance += amount
def __str__(self) -> str:
"""
Returns a string representation of the bank account,
including the account number, holder, and balance.
:return: A string representing the bank account.
"""
return (
f"Account Number: {self.account_number}\n"
f"Account Holder: {self.account_holder}\n"
f"Balance: {self.balance}"
)