From d131d47a234f3fb055b2973c8b8c4edae2b38445 Mon Sep 17 00:00:00 2001 From: lcarter505 <77297044+lcarter505@users.noreply.github.com> Date: Sat, 17 Apr 2021 20:54:30 -0400 Subject: [PATCH 1/2] Add files via upload --- mock_atm_improved.py | 131 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 mock_atm_improved.py diff --git a/mock_atm_improved.py b/mock_atm_improved.py new file mode 100644 index 0000000..3ae5146 --- /dev/null +++ b/mock_atm_improved.py @@ -0,0 +1,131 @@ +import datetime +import random + +beginning_balance = 200 + +database = {} + +def generated_account_number(): + return random.randrange(1111111111,9999999999) + +account_number = generated_account_number() + + +def login(): + print("********** Login **********") + account_number_from_user = int(input("What is your account number? \n")) + password = input("What is your password \n") + + for account_number,user_details in database.items(): + if(account_number == account_number_from_user): + if(user_details[3] == password): + bank_operation(user_details) + break + else: + print('Invalid username or password') + 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") + + account_number = generated_account_number() + database[account_number] = [first_name, last_name, email, password] + + print("Your account has been created") + print(" === ==== ====== ==== ===") + print(f'Your account number is {account_number}') + print("Make sure you keep it safe") + print(" === ==== ====== ==== ===") + + login() + + +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 bank_operation(user): + print(f'\nHello {user[0]} {user[1]}') + print("How can I help you today? \nAvailable Services:") + selected_option = int(input('(1) Withdrawl \n(2) Deposit \n(3) Complaint \n(4) Logout \n(5) Exit \nPlease select an option: ')) + if selected_option == 1: + withdrawal_operation(user) + elif selected_option == 2: + deposit_operation(user) + elif selected_option == 3: + complaint_operation(user) + elif selected_option == 4: + logout() + elif selected_option == 5: + exit() + else: + print("Invalid option selected") + bank_operation(user) + return user + + +def withdrawal_operation(user): + print("\nWithdrawal Menu") + print(f'You would like to make a withdrawl \nIs that correct?') + confirm_withdrawl = int(input('Press (1) for Yes (2) for No \n')) + if confirm_withdrawl == 1: + withdrawl_amount = int(input('How much would you like to withdraw?: \n')) + print(f'You have chosen to withdrawl ${withdrawl_amount} \nPlease take your cash \nHave a nice day!') + init() + elif confirm_withdrawl == 2: + bank_operation(user) + else: + print('Invalid option selected, please try again') + withdrawal_operation(user) + +def deposit_operation(user): + 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 withdraw?:\n ')) + print(f'You have chosen to deposit ${deposit_amount}') + current_balance = deposit_amount + beginning_balance + print(f'Your current balance is ${current_balance} \nHave a nice day!') + init() + elif confirm_deposit == 2: + bank_operation(user) + else: + print('Invalid option selected, please try again') + deposit_operation(user) + +def complaint_operation(user): + 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 \nWe appriciate your feedback \nHave a nice day!') + init() + elif confirm_complaint == 2: + bank_operation(user) + else: + print('Invalid option selected, please try again') + complaint_operation(user) + +def logout(): + login() + +def exit(): + init() + + +init() + From c7b19c57d19f502ab0bb719972939c5d0e668db5 Mon Sep 17 00:00:00 2001 From: lcarter505 <77297044+lcarter505@users.noreply.github.com> Date: Sun, 9 May 2021 21:20:17 -0400 Subject: [PATCH 2/2] Add files via upload --- Project/auth.py | 200 ++++++++++++++++++++++++++++++++++++++++++ Project/database.py | 161 ++++++++++++++++++++++++++++++++++ Project/validation.py | 16 ++++ 3 files changed, 377 insertions(+) create mode 100644 Project/auth.py create mode 100644 Project/database.py create mode 100644 Project/validation.py diff --git a/Project/auth.py b/Project/auth.py new file mode 100644 index 0000000..b03a8ff --- /dev/null +++ b/Project/auth.py @@ -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) diff --git a/Project/database.py b/Project/database.py new file mode 100644 index 0000000..1952daf --- /dev/null +++ b/Project/database.py @@ -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 diff --git a/Project/validation.py b/Project/validation.py new file mode 100644 index 0000000..c6e27a8 --- /dev/null +++ b/Project/validation.py @@ -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