Advertisement
dc5553

Python ATM for Students

May 10th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import sqlite3
  5.  
  6. '''
  7. Information:
  8.    Create a sqlite3 database named ATM.DB in the same folder as this file
  9.    The create tables statements for the sqlite3 tables to make this program run are:
  10.    CREATE TABLE accounts (user_id integer,account_number text PRIMARY KEY, balance decimal, interest_rate decimal, FOREIGN KEY(user_id) REFERENCES customers(user_id));
  11.    CREATE TABLE customers (user_id integer PRIMARY KEY,first_name text, last_name text, pin text);
  12. '''
  13.  
  14.  
  15. def account_checker(cur, account_check):
  16.     # Checks to see if the account is in the database
  17.     cur.execute('select * from accounts where account_number ="{0}"'.format(account_check))
  18.     # Returns either TRue of False if the account is in the database
  19.     if len(list(cur)) < 1:
  20.         return False
  21.     else:
  22.         return True
  23.  
  24.  
  25. def pin_checker(cur, account_check, pin_check):
  26.     # Checks to see if the PIN matches the account number
  27.     cur.execute('''SELECT customers.pin, accounts.account_number
  28.                   FROM customers
  29.                   INNER JOIN accounts
  30.                   ON customers.user_id=accounts.user_id
  31.                   WHERE customers.pin = '{0}'
  32.                   AND accounts.account_number = "{1}"'''.format(pin_check, account_check))
  33.     # Returns either True or False if the PIN matches the account number
  34.     if len(list(cur)) < 1:
  35.         return False
  36.     else:
  37.         return True
  38.  
  39.  
  40. def get_customer_name_by_acct(cur, valid_customer):
  41.     # Retrieves the customers name given the account number
  42.     cur.execute('''SELECT customers.first_name,customers.last_name
  43.                   FROM customers
  44.                   INNER JOIN accounts
  45.                   ON customers.user_id=accounts.user_id
  46.                   WHERE accounts.account_number = "{0}"'''.format(valid_customer))
  47.     names = cur.fetchone()
  48.     first_name, last_name = names[0], names[1]
  49.     return first_name, last_name
  50.  
  51.  
  52. def check_customer(cur):
  53.     # Runs the login dialog and returns the validated account number
  54.     try_number_acct = 0
  55.     try_number_pin = 0
  56.     while try_number_acct < 3 and try_number_pin < 3:
  57.         account_check = input('[?] Enter you account number:\t')
  58.         account_is_present = account_checker(cur, account_check)
  59.         if account_is_present:
  60.             while try_number_pin < 3:
  61.                 pin_check = input('[?] Enter your pin number:\t')
  62.                 pin_is_correct = pin_checker(cur, account_check, pin_check)
  63.                 if pin_is_correct:
  64.                     print('[-] You have been authenticated')
  65.                     return account_check
  66.                 else:
  67.                     try_number_pin += 1
  68.                     print('[!] Invalid PIN!!')
  69.         else:
  70.             try_number_acct += 1
  71.             print('[!] Invalid account number!!')
  72.            
  73.     sys.exit('[!] Maximum Attempts Reached Exiting')
  74.  
  75.  
  76. def withdraw_funds(cur, account_number, amount):
  77.     cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
  78.     current_balance = cur.fetchone()[0]
  79.     new_balance = float(current_balance) - float(amount)
  80.     cur.execute('UPDATE accounts SET balance = {0} WHERE account_number = "{1}"'.format(new_balance, account_number))
  81.  
  82.  
  83. def deposit_funds(cur, account_number, amount):
  84.     cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
  85.     current_balance = cur.fetchone()[0]
  86.     new_balance = float(current_balance) + float(amount)
  87.     cur.execute('UPDATE accounts SET balance = {0} WHERE account_number = "{1}"'.format(new_balance, account_number))
  88.  
  89.  
  90. def report_balance(cur, account_number):
  91.     cur.execute('SELECT balance FROM accounts WHERE account_number = "{0}"'.format(account_number))
  92.     return cur.fetchone()[0]
  93.  
  94.  
  95. def report_interest_rate(cur, account_number):
  96.     cur.execute('SELECT interest_rate FROM accounts WHERE account_number = "{0}"'.format(account_number))
  97.     return cur.fetchone()[0]
  98.  
  99.  
  100. def main():
  101.     # Connect to the sqlite3 database
  102.     conn = sqlite3.connect('ATM.DB')
  103.     # Create the cursor (used to execute SQL commands
  104.     cur = conn.cursor()
  105.     # Authenticate our customer
  106.     valid_account_number = check_customer(cur)
  107.     # Get the customers name for the greeting
  108.     customer_name = get_customer_name_by_acct(cur, valid_account_number)
  109.  
  110.     print('\n[-] Welcome to the Bank of Python {0} {1}\n'.format(customer_name[0], customer_name[1]))
  111.  
  112.     while True:
  113.         choice = input('''
  114.        [?] What is your choice?\n
  115.        [1] Deposit\n
  116.        [2] Withdraw\n
  117.        [3] Display Balance\n
  118.        [4] Display Interest Rate\n
  119.        [5] Exit\n
  120.        Enter choice>>\t''')
  121.  
  122.         choice = int(choice)
  123.  
  124.         if choice == 1:
  125.             amount = float(input('[?] How much would you like to deposit?\t$'))
  126.             deposit_funds(cur, valid_account_number, amount)
  127.             conn.commit()
  128.             print('\n[-] Your new balance is:',
  129.                   '$' + str(report_balance(cur, valid_account_number)))
  130.  
  131.         elif choice == 2:
  132.             amount = float(input('[?] How much would you like to withdraw?\t$'))
  133.             withdraw_funds(cur, valid_account_number, amount)
  134.             conn.commit()
  135.             print('\n[-] Your new balance is:',
  136.                   '$' + str(report_balance(cur, valid_account_number)))
  137.  
  138.         elif choice == 3:
  139.             print('\n[-] Your balance is:',
  140.                   '$' + str(report_balance(cur, valid_account_number)))
  141.  
  142.         elif choice == 4:
  143.             print('\n[-] Your interest rate is:',
  144.                   str(report_interest_rate(cur, valid_account_number)) + '%')
  145.  
  146.         elif choice == 5:
  147.             sys.exit('\n[-] Good Bye!')
  148.  
  149.         else:
  150.             sys.exit('\n[!] Exiting Program')
  151.  
  152.  
  153. if __name__ == '__main__':
  154.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement