Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions Project/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import random
import validation
import database
from getpass import getpass


# import mimetypes
#
# from getpass import getpass

# user_db_path = 'data/user_record/'

# database = {}


def init():
print("\nWelcome to PythonBank")
have_account = int(input("Do you have account with us: (1) Yes (2) No \n"))
if have_account == 1:
login()
elif have_account == 2:
register()
else:
print("You have selected invalid option")
init()


# def current_balance(args):
# pass


def login():
print("********** Login **********")
account_number_from_user = int(input("What is your account number? \n"))
is_valid_account_number = validation.account_number_validation(account_number_from_user)

if is_valid_account_number:
password = input("What is your password \n")
user = database.authenticated_user(account_number_from_user, password)

if user:
bank_operation(user)

print('Invalid account or password')
login()

else:
print("Account Number Invalid: check that you have up to 10 digits and only integers")
init()


def register():
print("********** Register **********")
email = input("What is your email address? \n")
first_name = input("What is your first name? \n")
last_name = input("What is your last name? \n")
password = input("Create a password for yourself \n")

user_account_number = generation_account_number()

# balance = set_current_balance(user_details, balance)

is_user_created = database.create(user_account_number, first_name, last_name, email, password)
#
user_details = (first_name, last_name, email, password, 0)
database[user_account_number] = (first_name, last_name, email, password, 0)

if is_user_created:
print("Your account has been created")
print(" === ==== ====== ==== ===")
print(f'Your account number is {user_account_number}')
print("Make sure you keep it safe")
print(" === ==== ====== ==== ===")


login()

else:
print("Something went wrong, please try again \n")
init()
# change to login

def user_balance(user_account_number, balance, user_details=None):
user_details[4] = balance



def bank_operation(user_details):

# get_current_balance(, balance)
print(f'\nWelcome {user_details[0]} {user_details[1]}')
print("How can I help you today? \nAvailable Services:")
selected_option = int(
input('(1) Withdrawal \n(2) Deposit \n(3) Complaint \n(4) Logout \n(5) Exit \nPlease select an option: '))
if selected_option == 1:
withdrawal_operation(user_details)
elif selected_option == 2:
deposit_operation(user_details)
elif selected_option == 3:
complaint_operation(user_details)
elif selected_option == 4:
logout()
elif selected_option == 5:
exit()
else:
print("Invalid option selected")
bank_operation(user_details)
# return user


# def current_balance(args):
# pass


def withdrawal_operation(user_account_number, user_details, balance):
# get_current_balance(user_details, balance)
user_details[4] = int(user_details[4])
balance = user_details[4]
print("\nWithdrawal Menu")
print(f'You would like to make a withdrawal \nIs that correct?')
confirm_withdrawal = int(input('Press (1) for Yes (2) for No \n'))
if confirm_withdrawal == 1:
print(f'Your current balance is {balance}')
withdrawal_amount = int(input('How much would you like to withdraw?: \n'))
print(f'You have chosen to withdrawal ${withdrawal_amount}')
database.update(user_account_number, balance)

if balance < withdrawal_amount:
print(f'Sorry, the requested transaction cannot be completed \n'
f'The amount you are seeking to withdrawal exceeds your current balance of ${balance} \n'
f'Please try your transaction again')
withdrawal_operation(user_account_number, user_details, balance)
else:
balance = balance - withdrawal_amount
print(f'Your remaining balance is ${balance} \nPlease take your cash \nHave a nice day!')

init()

elif confirm_withdrawal == 2:
bank_operation(user_details)
else:
print('Invalid option selected, please try again')
withdrawal_operation(user_account_number, user_details, balance)


def deposit_operation(user_account_number, user_details, balance):
# get_current_balance(user_details, balance)
user_details[4] = int(user_details[4])
print("Deposit Menu")
print('You would like to make a deposit \nIs that correct?')
confirm_deposit = int(input('Press (1) for Yes (2) for No \n'))
if confirm_deposit == 1:
deposit_amount = int(input('How much would you like to deposit?:\n '))
print(f'You have chosen to deposit ${deposit_amount}')
user_details[4] = user_details[4] + deposit_amount
print(f'Your current balance is ${user_details[4]} \nHave a nice day!')
database.update(user_account_number, balance)
init(user_account_number, user_details, balance)
elif confirm_deposit == 2:
bank_operation(user_account_number, user_details, balance)
else:
print('Invalid option selected, please try again')
deposit_operation(user_account_number, user_details, balance)


def complaint_operation(user_details):
print("Complaint Menu")
print('You would like to make a complaint \nIs that correct?')
confirm_complaint = int(input('Press (1) for Yes (2) for No \n'))
if confirm_complaint == 1:
customer_complaint = str(input('What issue would you like to report?: '))
print(f'Thank you for contacting us {user_details[0]}nWe appreciate your feedback \nHave a nice day!')
init()
elif confirm_complaint == 2:
bank_operation(user_details)
else:
print('Invalid option selected, please try again')
complaint_operation(user_details)


def generation_account_number():
return random.randrange(1111111111, 9999999999)

# def set_current_balance(user_details, balance):
# user_details[4] = balance
# return balance

# def get_current_balance(balance):
# return balance



def logout():
login()




init()
# user_details = (first_name, last_name, email, password, 0)
161 changes: 161 additions & 0 deletions Project/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# create record
# update record
# read record
# delete record
# CRUD

# find user

import os
import validation

user_db_path = 'data/user_record/'


def create(user_account_number, first_name, last_name, email, password):
# create a file
# name of the file would be account_number.txt
# add the user details to the file
# return true
# if saving to file fails, then deleted created file

user_data = first_name + "," + last_name + "," + email + "," + password + "," + str(0)

if does_account_number_exist(user_account_number):
return False

if does_email_exist(email):
print("User already exists")
return False

completion_state = False

try:

f = open(user_db_path + str(user_account_number) + ".txt", "x")

except FileExistsError:

does_file_contain_data = read(user_db_path + str(user_account_number) + ".txt")
if not does_file_contain_data:
delete(user_account_number)

else:

f.write(str(user_data))
completion_state = True

finally:

f.close()
return completion_state


def read(user_account_number):
# find user with account number
# fetch content of the file
is_valid_account_number = validation.account_number_validation(user_account_number)

try:

if is_valid_account_number:
f = open(user_db_path + str(user_account_number) + ".txt", "r")
else:
f = open(user_db_path + user_account_number, "r")

except FileNotFoundError:

print("User not found")

except FileExistsError:

print("User doesn't exist")

except TypeError:

print("Invalid account number format")

else:

return f.readline()

return False


def update(user_account_number, balance):
if os.path.exists(user_db_path + str(user_account_number) + ".txt"):

try:
f = open(user_db_path + str(user_account_number) + ".txt", "w")
os.write(user_db_path + balance + ".txt, w")

# f = open(user_db_path + str(user_account_number) + ".txt", "r")
f.write(balance)

finally:
f.close()
is_update_successful = True

return True

# find user with account number
# fetch the content of the file
# update the content of the file
# save the file
# return true


def delete(user_account_number):
# find user with account number
# delete the user record (file)
# return true

is_delete_successful = False

if os.path.exists(user_db_path + str(user_account_number) + ".txt"):

try:

os.remove(user_db_path + str(user_account_number) + ".txt")
is_delete_successful = True

except FileNotFoundError:

print("User not found")

finally:

return is_delete_successful


def does_email_exist(email):
all_users = os.listdir(user_db_path)

for user in all_users:
user_list = str.split(read(user), ',')
if email in user_list:
return True

return False


def does_account_number_exist(account_number):
all_users = os.listdir(user_db_path)

for user in all_users:

if user == str(account_number) + ".txt":
return True

return False


def authenticated_user(account_number, password):
if does_account_number_exist(account_number):

user = str.split(read(account_number), ',')

if password == user[3]:
return user

return False
16 changes: 16 additions & 0 deletions Project/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def account_number_validation(account_number):

if account_number:

try:
int(account_number)

if len(str(account_number)) == 10:
return True

except ValueError:
return False
except TypeError:
return False

return False
Loading