Guest User

PyBank non-flask | http://www.gta-sarp.com/forums/showthread

a guest
Sep 4th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import random, sqlite3, datetime
  2.  
  3. # VARIABLES #
  4. conn = sqlite3.connect('bankaccounts.db')
  5. c = conn.cursor()
  6. data = []
  7. transfer_data = []
  8. loggedin = False
  9. ############
  10.  
  11. def login():
  12.     print('\nPyBank | Login')
  13.     print('==============')
  14.     username = input('Username: ')
  15.     password = input('Password: ')
  16.     select = c.execute("SELECT * FROM accounts WHERE username=? LIMIT 1", [username])
  17.     for x in select.fetchall():
  18.         for y in x:
  19.             data.append(y)
  20.     if(len(data) == 0):
  21.         print('Account not found, please try again.')
  22.         login();
  23.     else:
  24.         if(data[1] == username and data[2] == password):
  25.             print('You have successfully logged in.')
  26.             global loggedin
  27.             loggedin = True
  28.             home();
  29.         else:
  30.             print('Invalid credentials, please try again.')
  31.             login();
  32.  
  33. def register():
  34.     print('\nPyBank | Registration')
  35.     print('=====================')
  36.     username = input('Username: ')
  37.     password = input('Password: ')
  38.     if(len(username) > 0 and len(password) > 0):
  39.         count = c.execute('SELECT * FROM accounts WHERE username=?', [username]) #Checks if the account already exists.
  40.         if(len(count.fetchall()) == 0):
  41.             result = c.execute('INSERT INTO accounts (username, password, balance) VALUES (?, ?, 5000)', (username, password)) #If the account doesn't exist, it creates it.
  42.             conn.commit()
  43.             print('Account successfully created.')
  44.             login();
  45.         else:
  46.             print('Account found with that username, returning to main menu.')
  47.             startmenu();
  48.     else:
  49.         print('Invalid input, please try again.')
  50.         register();
  51.  
  52. def home():
  53.     if(loggedin == False):
  54.         print('Nice try.')
  55.         startmenu();
  56.     else:
  57.         print('\nPyBank | Homescreen')
  58.         print('===================')
  59.         print('Welcome {}'.format(data[1]))
  60.         print('Current balance: ${}'.format(data[3]))
  61.        
  62.         print('What would you wish to do?')
  63.         action = input('1 - Withdraw\n2 - Deposit\n3 - Transfer\n4 - Account Settings\n> ')
  64.         if(action == '1'):
  65.             withdraw();
  66.  
  67.         elif(action == '2'):
  68.             deposit();
  69.  
  70.         elif(action == '3'):
  71.             transfer();
  72.  
  73.         elif(action == '4'):
  74.             settings();
  75.  
  76.         else:
  77.             print('Invalid input, please try again.')
  78.             home();
  79.  
  80. def withdraw():
  81.     print('\nHow much would you wish to withdraw?')
  82.     withdraw = input('> ')
  83.     if(is_number(withdraw)):
  84.         amount = float(withdraw)
  85.         if((data[3]-amount) >= 0.0):
  86.             data[3] -= amount
  87.             c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]]) #If the account doesn't exist, it creates it.
  88.             conn.commit()
  89.             print('You have successfully withdrawn ${} from your bank account'.format(amount))
  90.             home();
  91.         else:
  92.             print('Insufficient funds, please try again.')
  93.             home();
  94.     else:
  95.         print('Invalid input, please try again')
  96.         home();
  97.  
  98. def deposit():
  99.     print('\nHow much would you wish to deposit?')
  100.     deposit = input('> ')
  101.     if(is_number(deposit)):
  102.         amount = float(deposit)
  103.         data[3] += amount
  104.         c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]]) #If the account doesn't exist, it creates it.
  105.         conn.commit()
  106.         print('You have successfully deposited ${} to your bank account'.format(amount))
  107.         home();
  108.     else:
  109.         print('Invalid input, please try again')
  110.         home();
  111.  
  112. def settings():
  113.     pass
  114.  
  115. def transfer():
  116.     user_to = input('Username you wish to transfer money to: ')
  117.     amount = input('Amount: ')
  118.     amount = float(amount)
  119.     if(len(user_to) > 0 and len(str(amount)) > 0):
  120.         count = c.execute('SELECT * FROM accounts WHERE username=?', [user_to]) #Checks if the account already exists.
  121.     if(len(count.fetchall()) == 0):
  122.         if(is_number(amount)):
  123.                 if((data[3]-amount) >= 0.0):
  124.                     data[3] -= amount
  125.                     c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]])
  126.  
  127.                     select = c.execute("SELECT * FROM accounts WHERE username=? LIMIT 1", [user_to])
  128.                     for x in select.fetchall():
  129.                         for y in x:
  130.                             transfer_data.append(y)
  131.  
  132.                     transfer_data[3] += amount
  133.                     c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [transfer_data[3], user_to])
  134.                     print('You have transferred {} to {}'.format(amount, user_to))
  135.                     conn.commit()
  136.         else:
  137.             print('Invalid input')
  138.             home();
  139.     else:
  140.         print('No account found.')
  141.         home();
  142.  
  143. def startmenu():
  144.     print('\nPyBank version 2')
  145.     print('================')
  146.     print('1 - Login')
  147.     print('2 - Register')
  148.  
  149.     user_choice = input('> ')
  150.     if(user_choice == '1'):
  151.         login();
  152.  
  153.     elif(user_choice == '2'):
  154.         register();
  155.  
  156.     else:
  157.         print('Invalid input, login or register only.')
  158.         startmenu();
  159.  
  160. def is_number(s):
  161.     try:
  162.         float(s)
  163.         return True
  164.     except ValueError:
  165.         pass
  166.  
  167.     try:
  168.         import unicodedata
  169.         unicodedata.numeric(s)
  170.         return True
  171.     except (TypeError, ValueError):
  172.         pass
  173.  
  174. c.execute('CREATE TABLE IF NOT EXISTS accounts(id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT, balance REAL)') #Creates a table in our database if it doesn't exist.
  175. startmenu();
  176.  
  177. # TO DO
  178. # 1) Register system [x]
  179. # 2) Cipher system
  180. # 3) Login system [x]
  181. # 4) Home screen [x]
  182. # 5) Withdraw money [x]
  183. # 6) Transfer money to another account
  184. # 7) Deposit money [x]
  185. # 8) Possibly a system to send emails.
  186. # 9) Make into a GUI
  187. # 10) Change password
  188. # 11) Account deletion
Add Comment
Please, Sign In to add comment