-
Notifications
You must be signed in to change notification settings - Fork 2
/
snbank.py
164 lines (155 loc) · 5.54 KB
/
snbank.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
import json
import os
import re
from random import randint
from datetime import datetime
username = ''
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
def staff():
print("*************************************")
print("=<< 1. Create new bank account >>=")
print("=<< 2. Check Account Details >>=")
print("=<< 3. Logout >>=")
print("*************************************")
choice = input(f'Select your choice number from the above menu :')
if choice == "1":
create()
elif choice == "2":
account()
elif choice == "3":
file = open('user_session.txt', 'r+')
file.truncate()
file.close()
welcome()
else:
print('Wrong input')
staff()
def create():
name_correct = False
while name_correct == False:
name = str(input(f"Enter customer's name"))
if len(name) > 4:
if not name.isalpha():
print('Only alphabets are allowed')
name_correct = False
else:
name_correct = True
else:
print('Name should be longer than four characters')
name_correct = False
balance_correct = False
while balance_correct == False:
try:
balance = float(input('Enter opening balance'))
balance_correct == True
break
except ValueError:
print('Only numbers allowed')
balance_correct == False
acc_type_correct = False
while acc_type_correct == False:
acc_type = input(f"Enter account type(Savings or Current)")
if acc_type.upper() == "SAVINGS":
acc_type_correct = True
elif acc_type.upper() == "CURRENT":
acc_type_correct = True
else:
print('Enter account type(Savings or Current)')
acc_type_correct = False
email_correct = False
while email_correct == False:
email = input('Enter email')
regex = '^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$'
if re.search(regex, email):
email_correct = True
else:
email_correct = False
number = random_with_N_digits(10)
customer_file_data = []
customer_banking_data = {
username: [
{
'Account name': name.title(),
'Opening Balance': balance,
'Account Type': acc_type.title(),
'Account email': email,
'Account Number': number,
}
],
}
print(f"An account with account number {number} has been opened for {name}")
if os.stat('customer.txt').st_size == 0:
customer_file_data.append(customer_banking_data)
with open('customer.txt', 'w') as obj:
json.dump(customer_file_data, obj)
else:
with open('customer.txt') as obj:
data = json.load(obj)
data.append(customer_banking_data)
with open('customer.txt', 'w') as obj:
json.dump(data, obj)
staff()
def account():
try:
collect_number = int(input(f"\nEnter customer's account number: "))
with open('customer.txt') as file_obj:
data = json.load(file_obj)
found_flag = False
for user_data in data:
for user_data_key in user_data.keys():
for user_details in user_data[user_data_key]:
if collect_number in user_details.values():
found_flag = True
print('\nAccount Found ! See details below:')
print(user_details)
staff()
if found_flag == False:
print('\nAccount Not Found! You can register a new one if you wish.')
staff()
except ValueError:
print('Only integers are allowed')
staff()
def exit():
file = open('customer.txt', 'r+')
file.truncate()
file.close()
print("Thank you for using our banking system!")
def login():
print(f"Enter Details")
username = input("Please enter your username")
password = input(f'Please enter your password')
with open('staff.txt') as json_file:
data = json.load(json_file)
while (username != (data['Staff 1']['Username']) or password != (data['Staff 1']['Password'])) and (username != (data['Staff 2']['Username']) or password != (data['Staff 2']['Password'])):
print('Username or password not found')
login()
else:
print(f'Welcome {username}')
login = datetime.now()
login = login.strftime("%d:%m:%Y %H:%M:%S")
session_data = {
'Present User': username,
'Login Time': login,
}
with open('user_session.txt', 'w') as file_object:
json.dump(session_data, file_object)
staff()
def welcome():
print("=====================================")
print(" ----Welcome to SNBank---- ")
print("*************************************")
print("=<< 1. Staff Login >>=")
print("=<< 2. Close App >>=")
print("*************************************")
choice = input(f'Select your choice number from the above menu :')
if choice == "1":
login()
elif choice == "2":
exit()
else:
print('Wrong input')
welcome()
welcome()