-
Notifications
You must be signed in to change notification settings - Fork 1
/
CLI_BankingApp.py
214 lines (186 loc) · 8.46 KB
/
CLI_BankingApp.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
import json, os
class BankApp:
def __init__(self):
self.users=[]
self.prompt()
with open('usersdata.json', 'r') as json_file:
self.users = json.load(json_file)
def prompt(self):
print("Welcome to Virtual Gardens Group Bank")
print("Press 1: Create Account\nPress 2: Transaction\nPress 3: To Quit\n")
try:
welcome_prompt = int(input(">> "))
except ValueError:
print("\nPlease choose 1, 2 or 3\n")
self.prompt()
if (welcome_prompt == 1):
self.create_user()
elif (welcome_prompt == 2):
self.transaction()
elif (welcome_prompt == 3):
quit()
else:
print("\nPlease choose 1, 2 or 3\n")
self.prompt()
def create_user(self):
#Get current directory
currentDirectory = os.getcwd()
#store file name in a variable
file_name = r'\\usersdata.json'
#full path of json file
total_path = currentDirectory + file_name
#check if json file exists
if os.path.isfile(total_path) and os.access(total_path, os.R_OK):
with open('usersdata.json', 'r') as json_file:
self.users = json.load(json_file)
email = input("Enter email: ").lower()
if ("@" in email) and ("." in email):
#check if user exists
if email in ([sub['email'] for sub in self.users]):
print("User already exists ")
return self.create_user()
else:
try:
"""
pin variable is retrieved as a string object to enable the len() function to
check number of characters. Function int(pin) enables ValueError to check for
values that are not integers but does not change the datatype of pin.
"""
pin = str(input("Create 4-digit pin: "))
int(pin)
except ValueError:
print("Use only numbers")
return self.create_user()
#check for length of pin
if len(pin) != 4:
print("Pin must be 4 digits only.")
return self.create_user()
else:
pass
user = {"email": email, "pin": pin, "balance": 0.0}
print(self.users)
self.users.append(user)
with open('usersdata.json', 'w') as userdetail:
json.dump(self.users, userdetail, indent=4)
# get new user balance
bal = user["balance"]
print(f"Account created successfully! Your account balance is: ₦{bal}")
self.prompt()
else:
print("Email is not valid, Please try again")
self.create_user()
else:
pass
def transaction(self):
#opens file for reading
with open('usersdata.json', 'r') as json_file:
data = json.load(json_file)
print("Please login\n")
# ask the user to enter email
login_email = input("Enter email: ")
login_pin = input("Enter pin: ")
for i in data:
if login_email == i["email"]:
if login_pin == i["pin"]:
print("\nLogin successful!\nPlease proceed to select transaction\n")
transaction_prompt = int(input("Press 1: Check Balance \nPress 2: Deposit \nPress 3: Withdraw \nPress 4: Transfer \n>> " ))
# display actions to perform
if transaction_prompt == 1:
self.check_balance(login_email)
elif transaction_prompt == 2:
return self.deposit(login_email)
elif transaction_prompt == 3:
return self.withdraw(login_email)
elif transaction_prompt == 4:
return self.transfer(login_email)
else:
print("\nInvalid entry. Please try again\n")
self.transaction()
else:
print("\nEmail or pin is incorrect. Try again.")
self.prompt()
def check_balance(self, login_email):
#opens file for reading
with open('usersdata.json', 'r') as json_file:
data = json.load(json_file)
for i in data:
if login_email == i["email"]:
account_balance = i["balance"]
print(f"Your balance is ₦{account_balance}\n")
return self.transaction()
else:
pass
def deposit(self, login_email):
#opens file for reading and writing
with open('usersdata.json', 'r') as json_file:
data = json.load(json_file)
#get deposit amount
deposit_amount = float(input("Enter amount to deposit: "))
for i in data:
if login_email == i["email"]:
i["balance"] += deposit_amount
new_balance = i["balance"]
with open('usersdata.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
print(f"\nTransaction successful! \nYour new account balance is ₦{new_balance}")
print("\nThanks for banking with us!\n")
return self.transaction()
def withdraw(self, login_email):
#opens file for reading and writing
with open('usersdata.json', 'r') as json_file:
data = json.load(json_file)
#get amount for withdrawal
withdraw_amount = float(input("Enter amount for withdrawal: "))
for i in data:
if login_email == i["email"]:
balance = i["balance"]
if withdraw_amount > balance or balance <= 1000.0:
print("Insufficient funds\n Please deposit required amount\n")
return self.deposit(login_email)
else:
i["balance"] -= withdraw_amount
new_balance = i["balance"]
with open('usersdata.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
print(f"\nYou have successfully withdrawn ₦{withdraw_amount} \nYour balance is ₦{new_balance}\n")
print("\nThanks for banking with us!\n")
self.prompt()
def transfer(self, login_email):
#opens file for reading and writing
with open('usersdata.json', 'r') as json_file:
data = json.load(json_file)
beneficiary = input("Enter receipient's email: ")
# loop through list of dictionaries to check if beneficiary mail exists in the system
for i in data:
if login_email == i["email"]:
#store that particular item into this variable for future use
sender = i
break
#so that it is assigned default value before use
receiver = {}
for i in data:
#store the beneficiary email after finding it
# into this variable for future use
if beneficiary == i["email"]:
receiver = i
break
if not receiver:
print("Beneficiary not found. Please try again.\n")
self.transfer(login_email)
else:
transfer_amount = float(input("Enter amount to transfer: "))
if transfer_amount > sender["balance"] or sender["balance"] <= 1000.0:
print("Insufficient funds\n Please deposit\n")
self.deposit(login_email)
else:
sender["balance"] -= transfer_amount
sender_balance = sender["balance"]
receiver["balance"] += transfer_amount
#receiver_balance = receiver["balance"]
print("Transfer Successful!\nThank you for banking with us\n")
print(f"Your account balance is: ₦{sender_balance}\n")
with open('usersdata.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
self.prompt()
#creating an object of class
s = BankApp()