-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankAtm.py
248 lines (211 loc) · 8.25 KB
/
BankAtm.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from tkinter import messagebox
import string
import logging
logging.basicConfig(filename = "errors.txt",level=logging.ERROR)
class ManageDbm(object):
"""Base DataBase Manager,Controls The Opening And Closing Of The
Bank's DataBase.Its __INIT__ Method Read(r) Or Write(w) Mode Of
Opening A File As Its Argument And Then Opens The DataBase Using
That Mode
"""
def __init__(self,arg):
self.arg = arg
self.dict = open("database.txt",arg)
"""This Destructor Closes The Opened File InOrder To Facilitate
Of Details Written On It So As Not To Lose Important Data
"""
def __del__(self):
self.dict.close()
class BankAcct(object):
"""THE __INIT__ METHOD OF THE BACNKACCT CLASS RECEIVES AN ATM PIN AS
ARGUMENT,THEN SEARCHES THROUGH THE DATABASE TO CHECK IF THE PIN EXISTS
AND THEN INITILIZES BANK DETAILS
"""
account_name = list()
def __init__(self,pin):
self.__pin = pin
self.__acct = ManageDbm("r")
self.__dbm = self.__acct.dict.read().replace("\n","")
self.DataBase = eval(self.__dbm)
self.atmCash = self.DataBase[100000][2]
"""SEARCHES FOR SPECIFIED ACCT. PIN FROM THE DATABASE.
IF FOUND,INITIALISES THE CLASS'S ACCT. NO,NAME AND ACCT. BALANCE.
IF NOT FOUND IN THE DATABASE,RETURNS AN ERROR MESSAGE TO THE USER
"""
name = BankAcct.account_name[-1]
for key in self.DataBase:
if self.__pin in self.DataBase[key] and name in self.DataBase[key]:
self.AcctNo = key
self.__name = self.DataBase[key][0]
self.__bal = int(self.DataBase[key][2])
break
def CheckPin(self,name):
""" Verifies If User's Pin Exists"""
for key in self.DataBase:
if (self.__pin in self.DataBase[key] and name in self.DataBase[key]):
return True
else: return False
@staticmethod
def CheckName(name):
"""Verifies If User's log-in name exists"""
database = ManageDbm("r")
dbm = database.dict.read().replace("\n","")
DataBase = eval(dbm)
database.dict.close()
del database
for key in DataBase:
if name in DataBase[key]:
BankAcct.account_name.append(name)
return True
else: return False
@staticmethod
def ResetPin(name,new):
"""Verifies If User's log-in name exists,If It Exists After Verification
Updates The Customer's Pin With The Newly Randomly Selected Pin.
"""
database = ManageDbm("r")
db = database.dict.read().replace("\n","")
del database
DataBase = eval(db)
for key in DataBase:
if name in DataBase[key]:
DataBase[key][1] = new
break
else: return False
db = "{\n"
for key in DataBase:
db += str(key)+": "+str(DataBase[key])+",\n"
db += "}"
database = ManageDbm("w")
try:
database.dict.write(db)
del database
return True
except BaseException as e:
logging.error(e)
del database
return False
"""ACCESS FUNCTIONS FOR VIEWING THE USER'S ACCOUNT BALANCE,ACCOUNT NAME
AND THE ACCOUNT NUMBER.
"""
def GetBalance(self):
return self.__bal
def GetName(self):
return self.__name
def GetAccountNo(self):
return self.AcctNo
def Deposit(self,amnt):
del self.__acct
self.__bal += amnt
return self._Processing()
def WithDraw(self,amnt):
if self.__bal-amnt<0:
messagebox.showwarning("ERROR","Insufficient Funds !!")
return False
if self.atmCash - amnt<0:
msg = "Insufficient Cash In ATM !!\nContact Operator To Deposit More Cash."
messagebox.showerror("ERROR",msg)
return False
del self.__acct
self.atmCash -= amnt
self.DataBase[100000][2] = self.atmCash
self.__bal -= amnt
return self._Processing()
def DelAcct(self,name):
for key in self.DataBase:
if (self.__pin in self.DataBase[key] and name in self.DataBase[key]):
del self.DataBase[key]
self.db = "{\n"
for item in self.DataBase:
self.db += str(item)+": "+str(self.DataBase[item])+",\n"
self.db += "}"
try:
self.__acct = ManageDbm("w")
self.__acct.dict.write(self.db)
except BaseException as e:
logging.error(e)
messagebox.showerror("ERROR","An Error Occurred,Check Errors.txt To View It")
return False
return True
else: return False
def _Processing(self):
"""_PROCESSING CONDUCTS THE UPDATING OF THE DATABASE WITH THE NEW
ACCOUNT FIGURES
"""
self.DataBase[self.AcctNo][2] = self.__bal
self.db = "{\n"
for key in self.DataBase:
self.db += str(key)+": "+str(self.DataBase[key])+",\n"
self.db += "}"
try:
self.__acct = ManageDbm("w")
self.__acct.dict.write(self.db)
except BaseException as e:
logging.error(e)
messagebox.showerror("ERROR","An Error Occurred,Check Errors.txt To View It")
return False
else:
self.__acct.dict.close()
return (True,self.__bal)
def Transfer(self,AcctNo,amnt):
if self.__bal-amnt<0:
#end transaction if money in the acct is less than
#money about to be transferred.
messagebox.showerror("ERROR","Insufficient Funds !!")
return False
"""SERACHES FOR THE RECIPIENT'S ACCT. NUMBER IN THE DATABASE,
IF FOUND, CONTINUE WITH THE TRANSACTION AND IF NOT FOUND,RETURNS
AN ERROR MESSAGE NOTIFYING USER OF INVALID ACCT. NUMBER
"""
for key in self.DataBase:
if AcctNo == key:
otherAcctNo = key
otherName = self.DataBase[key][0]
otherBal = self.DataBase[key][2]
break
else:
messagebox.showerror("INVALID CREDENTIAL","Recipients's Account Number Is Invalid !!")
return False
del self.__acct
self.__bal -= amnt
otherBal += amnt
self.DataBase[otherAcctNo][2] = otherBal
self.DataBase[self.AcctNo][2] = self.__bal
self.db = "{\n"
for key in self.DataBase:
self.db += str(key)+": "+str(self.DataBase[key])+",\n"
self.db += "}"
try:
self.__acct = ManageDbm("w")
self.__acct.dict.write(self.db)
except BaseException as e:
logging.error(e)
messagebox.showerror("ERROR","An Error Occurred,Check Errors.txt To View It")
return False
else:
self.__acct.dict.close()
return (True,self.__bal)
def ChangePin(self,new,name):
del self.__acct
self.__new = new
for key in self.DataBase:
if (self.__pin in self.DataBase[key] and name in self.DataBase[key]):
self.AcctNo = key
break
self.DataBase[self.AcctNo][1] = self.__new
self.db = "{\n"
for key in self.DataBase:
self.db += str(key)+": "+str(self.DataBase[key])+",\n"
self.db += "}"
try:
self.__acct = ManageDbm("w")
self.__acct.dict.write(self.db)
except BaseException as e:
logging.error(e)
messagebox.showerror("ERROR","An Error Occurred,Check Errors.txt To View It")
return False
else:
self.__acct.dict.close()
return True
def __del__(self):
del self.__acct