-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.py
More file actions
68 lines (54 loc) · 1.96 KB
/
Account.py
File metadata and controls
68 lines (54 loc) · 1.96 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
from hashlib import sha256
from module.Transaction import Transaction
from typing import List
def apply_sha256(input):
try:
digest = sha256()
digest.update(input.encode('utf-8'))
hash_bytes = digest.digest()
hex_string = ''.join(f'{b:02x}' for b in hash_bytes)
return hex_string
except Exception as e:
print(e)
raise RuntimeError(e)
class Account:
def __init__(self, account_number: str, name: str, balance: int, previous_hash: str):
self.account_number = account_number
self.name = name
self.balance = balance
self.previous_hash = previous_hash
self.hash: str = self.compute_hash()
self.transactions: List = []
self.assets = {
"stocks": {},
"real_estate": {}
} # 보유 자산
def deposit(self, amount: int) -> int:
self.balance += amount
# print(f"{self.name} Deposited {amount}. Current balance is: {self.balance}")
return self.balance
def withdraw(self, amount: int) -> int:
if self.balance > amount:
self.balance -= amount
# print(f"{self.name} Withdrew {amount} $. Current balance is: {self.balance}")
return amount
else:
# print("You don't have enough funds to withdraw.")
return 0
def compute_hash(self) -> str:
calculatedHash = apply_sha256(
str(self.account_number) + self.name + self.previous_hash
)
return calculatedHash
def getAccountNumber(self) -> str:
return self.account_number
def getAccountOwnerName(self) -> str:
return self.name
def getAccountBalance(self) -> int:
return self.balance
def getLatestTransaction(self) -> Transaction:
return self.transactions[-1]
def setPreviousHash(self, previous_hash: str):
self.previous_hash = previous_hash
def setHash(self, hash: str):
self.hash = hash