-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.py
More file actions
85 lines (75 loc) · 3.11 KB
/
transaction.py
File metadata and controls
85 lines (75 loc) · 3.11 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
from customer import Customer
from datetime import datetime
class Transaction:
def __init__(self,senderAccountNumber,receiverAccountNumber,amount):
self.sender = senderAccountNumber
self.receiver = receiverAccountNumber
self.amount = amount
Transaction.updateBalance(self.sender,self.amount,"W")
Transaction.updateBalance(self.receiver,self.amount,"D")
Transaction.updateTransactionLog(self.sender,self.receiver,amount)
print("Transaction Successful")
@classmethod
def updateTransactionLog(self,senderAccNo,ReceiverAccNo,amount):
with open('data/transaction_logs.txt','r') as f:
lines = f.readlines()
now = datetime.now()
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
lines.append(f"{date_time_str} {senderAccNo} {ReceiverAccNo} {amount} \n")
f.close()
with open('data/transaction_logs.txt','w') as f:
f.writelines(lines)
@classmethod
def getTransactionLog(self,account):
with open('data/transaction_logs.txt','r') as f:
lines = f.readlines()
logs = list()
for i in lines:
if f"{account}" in i:
logs.append(i)
else:
pass
if logs == []:
print("Logs Not Found: Incorrect Account Number!")
else:
return logs
@classmethod
def updateBalance(self,CustomerID,amount,type : str):
amount = float(amount)
if type.upper() == "D":
with open("data/accounts.txt",'r') as file:
lines = file.readlines()
updated = list()
for i in lines:
if f"{CustomerID}" in i:
acc, bal, ty, cid = i.split(' ')
newbal = float(bal) + amount
updated.append(f"{acc} {newbal} {ty} {cid}")
else:
updated.append(i)
file.close()
with open("data/accounts.txt",'w') as file:
file.writelines(updated)
file.close()
return "Successful Deposit..."
if type.upper() == "W":
with open("data/accounts.txt",'r') as file:
lines = file.readlines()
updated = list()
for i in lines:
if f"{CustomerID}" in i:
acc, bal, ty, cid = i.split(' ')
if float(bal)<amount:
raise Exception("Insufficient Funds! Balance too low.")
else:
newbal = float(bal) - amount
updated.append(f"{acc} {newbal} {ty} {cid}")
else:
updated.append(i)
file.close()
with open("data/accounts.txt",'w') as file:
file.writelines(updated)
file.close()
return "Successful Withdrawal..."
else:
raise Exception("Invalid Type of Transaction.")