-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-bank.py
313 lines (283 loc) · 10.3 KB
/
the-bank.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#[MINOR PROJECT]
# Import pickle to store data
import pickle
# Imported os to check if database file exists or not
import os
# Customer class for all customers
class Customer:
# Username of customer
Username = None
# Customer's password
Password = None
# Customer's balance initially 1000
Balance = 1000
# Customer's Transacton history
Transactions = ""
# Constructor to Initialize a customer with username and password
def __init__(self, Username, Password):
# Set Username as entered by user
self.Username = Username
# Set password as entered by user
self.Password = Password
# Method to check balance of customer
def CheckBalance(self):
# Return the balance
return self.Balance
# Method for matching customer's password
def LoginWithPassword(self, Password):
# Check if entered passeord matches with origional password
if self.Password == Password:
# return true if matches
return True
# Otherwise
else:
# return false
return False
# Method to update customer's balance after transaction
def UpdateBalance(self, Balance, Action):
# Check if transaction is debit
if Action == "debit":
# then debit amount
self.Balance -= Balance
# Otherwise if transaction is credit
elif Action == "credit":
# then credit amount
self.Balance += Balance
else: pass
# method to check transaction history
def CheckTransactions(self):
# return the history
return self.Transactions
# Method to add transaction record after transaction
def AddRecord(self, Record):
# add given transaction to the record
self.Transactions += Record
# List of all customers
AllCustomers = []
# Object of current logged customer
ActiveCustomer = None
# Function to load data from database
def Load():
# Get global variables
global ActiveCustomer
global AllCustomers
# Check if database exists or not
if os.path.exists("Database"):
# if exists then read the database
with open("Database","rb") as db:
# read and load all customer's data into AllCustomers List
AllCustomers = pickle.load(db)
# Check if any user is logged in or not
if ActiveCustomer != None:
# If user is logged in then refresh user's data
ActiveCustomer = FindCustomer(ActiveCustomer.Username)[1]
# Function to store data into databse
def Dump():
# Open database file
with open("Database","wb") as db:
# Put the data from AllCustomers list to the database
pickle.dump(AllCustomers, db)
# Function to find out a customer by username
def FindCustomer(Username):
# check if list of customers is empty or not
if AllCustomers == []:
# if empty then return false as no user exists
return [False]
# otherwise loop through all customers
for Customer in AllCustomers:
# and check is username of customer matches with the record or not
if Customer.Username == Username:
# if username is matched then return TRUE and the customer object
return [True, Customer]
# otherwise return FALSE as no customer is found
return [False]
# Function to create new account
def CreateAccount():
# print welcome messege
print("\n[SIGNUP]\nCreate new Username and Password\n")
# get username from user
Username = str(input("Username: "))
# load fresh data
Load()
# Check if user is already existing or not
if FindCustomer(Username)[0] == True:
# If username is already taken then show messege
print("Username is already taken. Try again")
return False
# otherwise
else:
# get passeord from user
Password = str(input("Password: "))
# create new customer object
NewCustomer = Customer(Username, Password)
# add new customer to the list of all customers
AllCustomers.append(NewCustomer)
# store updated data to the database
Dump()
# show success messege
print("Account created successfully.")
# Function to Log a user in
def Login():
# Show welcome messege
print("\n[LOGIN]\n")
# get username input
Username = str(input("Username: "))
# get password input
Password = str(input("Password: "))
# Load fresh data
Load()
# Find user by entered username
Data = FindCustomer(Username)
# check if user exists and also if password matches or not
if Data[0] == True and Data[1].LoginWithPassword(Password) == True:
global ActiveCustomer
# if password matches the set the active user as logged in
ActiveCustomer = Data[1]
# show sucess messege
print("Logged In successfully.")
# otherwise
else:
# show incorrect messege
print("Either username or password was incorrect, try again")
# Fuction to transfer Fund
def FundTransfer():
# show welcome messege
print("\n[FUND TRANSFER]\nWhom you want to send money:")
# get username from user
Username = input("Username:")
# load fresh data
Load()
# check is user exists or not
if FindCustomer(Username)[0] == False:
# If not then show messege
print("No such user found. Try again.")
return 0
# otherwise check if user is not entering his own username
if Username == ActiveCustomer.Username:
# then show messege
print("You can't send money to yourself.")
return 0
# otherwise get amount input from user
Amount = int(input("Enter the amount: Rs."))
# load new data
Load()
# check if amount is not less than 0
if Amount < 1:
print("Please Enter a valid amount.")
return 0
# check if user have sufficent balance to transfer
if Amount > ActiveCustomer.Balance:
print("You don't have enough money to send.")
return 0
# get other customer's object
SecondCustomer = FindCustomer(Username)[1]
# Update balance of other customer
SecondCustomer.UpdateBalance(Amount, "credit")
# Update balance of current customer
ActiveCustomer.UpdateBalance(Amount, "debit")
# Add transaction record to the history of current user
ActiveCustomer.AddRecord("DEBIT " + str(Amount) + " TO USER " + Username + "\n")
# Add transaction record to the history of other user
SecondCustomer.AddRecord("CREDIT " + str(Amount) + " BY USER " + ActiveCustomer.Username + "\n")
# store updated data into database
Dump()
print("Transaction Successful.")
# Function to delete a customer account
def DeleteAccount():
# load fresh data
Load()
# get global variables
global AllCustomers
global ActiveCustomer
# find the customer to be deleted
for index, user in enumerate(AllCustomers):
if user.Username == ActiveCustomer.Username:
# remove that customer form the record
AllCustomers.pop(index)
# logout the user as it has been deleted
ActiveCustomer = None
# store updated data
Dump()
# Function for help
def Help():
with open("help.txt") as h:
print(h.read())
# show welcome messege
print("\n-----[ MUNISH'S BANK ]-----\n")
# show help text on start
Help()
# main loop
while True:
# get action input from user
UserInput = input(":> ").lower()
# check if user wants to quit the close the program
if UserInput in ["exit","close","quit","x"]:
break
# check if user needs help then show the help text from file
elif UserInput in ["help","h"]:
Help()
# if user wants to create new account then..
elif UserInput in ["signup","create account","new account","s"]:
# check if there is no active user
if ActiveCustomer == None:
# then call the CreateAccount function
CreateAccount()
# otherwise show messege
else:
print("Please Logout before this action.")
# check if user wants to login
elif UserInput in ["login","l"]:
# check if there is no active user
if ActiveCustomer == None:
# then call login
Login()
# otherwise show messege
else:
print("You are already logged in.")
elif UserInput in ["logout","o"]:
# check if user is logged in or not
if ActiveCustomer == None:
# if not then show messege
print("You are not logged in.")
# otherwise clear ActiveCustomer
else:
ActiveCustomer = None
print("[LOGOUT]\nLogged out successfully.")
# check if user wants to check balance
elif UserInput in ["balance","b"]:
# check if there is no active user
if ActiveCustomer == None:
# if not then show messege
print("You are not logged in.")
else:
Load()
print("Balance: Rs.",ActiveCustomer.CheckBalance())
# check if user wants to transfer fund
elif UserInput in ["transfer","t"]:
# check if there is no active user
if ActiveCustomer == None:
# if not then show messege
print("You are not logged in.")
else:
FundTransfer()
# check if user want to see transaction history
elif UserInput in ["transactions","tx"]:
# check if there is no active user
if ActiveCustomer == None:
# if not then show messege
print("You are not logged in.")
else:
Load()
print(ActiveCustomer.CheckTransactions())
# check is user wants to delete the account
elif UserInput in ["delete account","d","delete"]:
# check if there is no active user
if ActiveCustomer == None:
# if not then show messege
print("You are not logged in.")
else:
DeleteAccount()
print("Your account has been deleted.")
# Otherwise show error messege
else:
print("Unable to understand, try again.")