Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import sqlite3
- '''
- Information:
- Create a sqlite3 database named ATM.DB in the same folder as this file
- The create tables statements for the sqlite3 tables to make this program run are:
- CREATE TABLE accounts (user_id integer,account_number text PRIMARY KEY, balance decimal, interest_rate decimal, FOREIGN KEY(user_id) REFERENCES customers(user_id));
- CREATE TABLE customers (user_id integer PRIMARY KEY,first_name text, last_name text, pin text);
- '''
- def account_checker(cur, account_check):
- # Checks to see if the account is in the database
- cur.execute('select * from accounts where account_number ="{0}"'.format(account_check))
- # Returns either TRue of False if the account is in the database
- if len(list(cur)) < 1:
- return False
- else:
- return True
- def pin_checker(cur, account_check, pin_check):
- # Checks to see if the PIN matches the account number
- cur.execute('''SELECT customers.pin, accounts.account_number
- FROM customers
- INNER JOIN accounts
- ON customers.user_id=accounts.user_id
- WHERE customers.pin = '{0}'
- AND accounts.account_number = "{1}"'''.format(pin_check, account_check))
- # Returns either True or False if the PIN matches the account number
- if len(list(cur)) < 1:
- return False
- else:
- return True
- def get_customer_name_by_acct(cur, valid_customer):
- # Retrieves the customers name given the account number
- cur.execute('''SELECT customers.first_name,customers.last_name
- FROM customers
- INNER JOIN accounts
- ON customers.user_id=accounts.user_id
- WHERE accounts.account_number = "{0}"'''.format(valid_customer))
- names = cur.fetchone()
- first_name, last_name = names[0], names[1]
- return first_name, last_name
- def check_customer(cur):
- # Runs the login dialog and returns the validated account number
- try_number_acct = 0
- try_number_pin = 0
- while try_number_acct < 3 and try_number_pin < 3:
- account_check = input('[?] Enter you account number:\t')
- account_is_present = account_checker(cur, account_check)
- if account_is_present:
- while try_number_pin < 3:
- pin_check = input('[?] Enter your pin number:\t')
- pin_is_correct = pin_checker(cur, account_check, pin_check)
- if pin_is_correct:
- print('[-] You have been authenticated')
- return account_check
- else:
- try_number_pin += 1
- print('[!] Invalid PIN!!')
- else:
- try_number_acct += 1
- print('[!] Invalid account number!!')
- sys.exit('[!] Maximum Attempts Reached Exiting')
- def withdraw_funds(cur, account_number, amount):
- cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
- current_balance = cur.fetchone()[0]
- new_balance = float(current_balance) - float(amount)
- cur.execute('UPDATE accounts SET balance = {0} WHERE account_number = "{1}"'.format(new_balance, account_number))
- def deposit_funds(cur, account_number, amount):
- cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
- current_balance = cur.fetchone()[0]
- new_balance = float(current_balance) + float(amount)
- cur.execute('UPDATE accounts SET balance = {0} WHERE account_number = "{1}"'.format(new_balance, account_number))
- def report_balance(cur, account_number):
- cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
- return cur.fetchone()[0]
- def report_interest_rate(cur, account_number):
- cur.execute('SELECT interest_rate FROM accounts WHERE account_number = "{0}"'.format(account_number))
- return cur.fetchone()[0]
- def main():
- # Connect to the sqlite3 database
- conn = sqlite3.connect('ATM.DB')
- # Create the cursor (used to execute SQL commands
- cur = conn.cursor()
- # Authenticate our customer
- valid_account_number = check_customer(cur)
- # Get the customers name for the greeting
- customer_name = get_customer_name_by_acct(cur, valid_account_number)
- print('\n[-] Welcome to the Bank of Python {0} {1}\n'.format(customer_name[0], customer_name[1]))
- while True:
- choice = input('''
- [?] What is your choice?\n
- [1] Deposit\n
- [2] Withdraw\n
- [3] Display Balance\n
- [4] Display Interest Rate\n
- [5] Exit\n
- Enter choice>>\t''')
- choice = int(choice)
- if choice == 1:
- amount = float(input('[?] How much would you like to deposit?\t$'))
- deposit_funds(cur, valid_account_number, amount)
- conn.commit()
- print('\n[-] Your new balance is:',
- '$' + str(report_balance(cur, valid_account_number)))
- elif choice == 2:
- amount = float(input('[?] How much would you like to withdraw?\t$'))
- withdraw_funds(cur, valid_account_number, amount)
- conn.commit()
- print('\n[-] Your new balance is:',
- '$' + str(report_balance(cur, valid_account_number)))
- elif choice == 3:
- print('\n[-] Your balance is:',
- '$' + str(report_balance(cur, valid_account_number)))
- elif choice == 4:
- print('\n[-] Your interest rate is:',
- str(report_interest_rate(cur, valid_account_number)) + '%')
- elif choice == 5:
- sys.exit('\n[-] Good Bye!')
- else:
- sys.exit('\n[!] Exiting Program')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement