-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
140 lines (124 loc) · 4.5 KB
/
user.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
from csv import writer
class User:
def __init__(self, name, acc_no, password, balance=0):
self.name = name
self.account_no = acc_no
self.password = password
self.balance = balance
def get_name(self):
return self.name
def get_account_no(self):
return self.account_no
def get_balance(self):
return self.balance
@staticmethod
def my_update(key, temp):
temp['name'] = key['name']
temp['account_no'] = key['account_no']
temp['password'] = key['password']
temp['balance'] = key['balance']
@staticmethod
def is_valid(acc_no, password):
jsonfile = open("user.json", "r+")
mydata = json.load(jsonfile)
jsonfile.close()
for key in mydata:
if key["account_no"] == acc_no and key['password'] == password:
user = User(key['name'], key['account_no'], key['password'], key['balance'])
return user
@staticmethod
def is_valid_acc(acc_no):
jsonfile = open("user.json", "r+")
mydata = json.load(jsonfile)
jsonfile.close()
for key in mydata:
if key["account_no"] == acc_no :
return True
else:
return False
def deposit(self, amount):
jsonfile = open("user.json", "r+")
mydata = json.load(jsonfile)
jsonfile.close()
new_data = []
for key in mydata:
if key['account_no'] == self.account_no:
temp = {}
key['balance'] += amount
User.my_update(key, temp)
new_data.append(temp)
with open('transaction.csv', 'a') as f_object:
writer_object = writer(f_object)
writer_object.writerow({self.account_no, "You deposit "+str(amount)})
f_object.close()
else:
temp = {}
User.my_update(key, temp)
new_data.append(temp)
with open("user.json", "w") as f:
f.write(json.dumps(new_data, indent=4))
def withdraw(self, amount):
jsonfile = open("user.json", "r+")
mydata = json.load(jsonfile)
jsonfile.close()
new_data = []
for key in mydata:
if key['account_no'] == self.account_no:
temp = {}
key['balance'] -= amount
User.my_update(key, temp)
new_data.append(temp)
with open('transaction.csv', 'a') as f_object:
writer_object = writer(f_object)
writer_object.writerow({self.account_no, "You withdraw "+str(amount)})
f_object.close()
else:
temp = {}
User.my_update(key, temp)
new_data.append(temp)
with open("user.json", "w") as f:
f.write(json.dumps(new_data, indent=4))
def transfer(self, to_acc_no, amount):
jsonfile = open("user.json", "r+")
mydata = json.load(jsonfile)
jsonfile.close()
new_data = []
for key in mydata:
if key['account_no'] == self.account_no:
temp = {}
key['balance'] -= amount
User.my_update(key, temp)
new_data.append(temp)
elif key['account_no'] == to_acc_no:
temp = {}
key['balance'] += amount
User.my_update(key, temp)
new_data.append(temp)
with open('transaction.csv', 'a') as f_object:
writer_object = writer(f_object)
writer_object.writerow({self.account_no, "You transfer " + str(amount) + " to " + to_acc_no})
f_object.close()
else:
temp = {}
User.my_update(key, temp)
new_data.append(temp)
with open("user.json", "w") as f:
f.write(json.dumps(new_data, indent=4))
@staticmethod
def unique(acc_no):
with open("user.json", 'r+') as j:
mydata = json.load(j)
for key in mydata:
if key["account_no"] == acc_no:
return True
return False
def show_profile(self):
with open("user.json", 'r+') as j:
mydata = json.load(j)
for key in mydata:
if key["account_no"] == self.account_no:
print(key)
def update_user(self, user):
user.add_user()
self.del_user()