Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, sqlite3, datetime
- # VARIABLES #
- conn = sqlite3.connect('bankaccounts.db')
- c = conn.cursor()
- data = []
- transfer_data = []
- loggedin = False
- ############
- def login():
- print('\nPyBank | Login')
- print('==============')
- username = input('Username: ')
- password = input('Password: ')
- select = c.execute("SELECT * FROM accounts WHERE username=? LIMIT 1", [username])
- for x in select.fetchall():
- for y in x:
- data.append(y)
- if(len(data) == 0):
- print('Account not found, please try again.')
- login();
- else:
- if(data[1] == username and data[2] == password):
- print('You have successfully logged in.')
- global loggedin
- loggedin = True
- home();
- else:
- print('Invalid credentials, please try again.')
- login();
- def register():
- print('\nPyBank | Registration')
- print('=====================')
- username = input('Username: ')
- password = input('Password: ')
- if(len(username) > 0 and len(password) > 0):
- count = c.execute('SELECT * FROM accounts WHERE username=?', [username]) #Checks if the account already exists.
- if(len(count.fetchall()) == 0):
- result = c.execute('INSERT INTO accounts (username, password, balance) VALUES (?, ?, 5000)', (username, password)) #If the account doesn't exist, it creates it.
- conn.commit()
- print('Account successfully created.')
- login();
- else:
- print('Account found with that username, returning to main menu.')
- startmenu();
- else:
- print('Invalid input, please try again.')
- register();
- def home():
- if(loggedin == False):
- print('Nice try.')
- startmenu();
- else:
- print('\nPyBank | Homescreen')
- print('===================')
- print('Welcome {}'.format(data[1]))
- print('Current balance: ${}'.format(data[3]))
- print('What would you wish to do?')
- action = input('1 - Withdraw\n2 - Deposit\n3 - Transfer\n4 - Account Settings\n> ')
- if(action == '1'):
- withdraw();
- elif(action == '2'):
- deposit();
- elif(action == '3'):
- transfer();
- elif(action == '4'):
- settings();
- else:
- print('Invalid input, please try again.')
- home();
- def withdraw():
- print('\nHow much would you wish to withdraw?')
- withdraw = input('> ')
- if(is_number(withdraw)):
- amount = float(withdraw)
- if((data[3]-amount) >= 0.0):
- data[3] -= amount
- c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]]) #If the account doesn't exist, it creates it.
- conn.commit()
- print('You have successfully withdrawn ${} from your bank account'.format(amount))
- home();
- else:
- print('Insufficient funds, please try again.')
- home();
- else:
- print('Invalid input, please try again')
- home();
- def deposit():
- print('\nHow much would you wish to deposit?')
- deposit = input('> ')
- if(is_number(deposit)):
- amount = float(deposit)
- data[3] += amount
- c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]]) #If the account doesn't exist, it creates it.
- conn.commit()
- print('You have successfully deposited ${} to your bank account'.format(amount))
- home();
- else:
- print('Invalid input, please try again')
- home();
- def settings():
- pass
- def transfer():
- user_to = input('Username you wish to transfer money to: ')
- amount = input('Amount: ')
- amount = float(amount)
- if(len(user_to) > 0 and len(str(amount)) > 0):
- count = c.execute('SELECT * FROM accounts WHERE username=?', [user_to]) #Checks if the account already exists.
- if(len(count.fetchall()) == 0):
- if(is_number(amount)):
- if((data[3]-amount) >= 0.0):
- data[3] -= amount
- c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [data[3], data[1]])
- select = c.execute("SELECT * FROM accounts WHERE username=? LIMIT 1", [user_to])
- for x in select.fetchall():
- for y in x:
- transfer_data.append(y)
- transfer_data[3] += amount
- c.execute('UPDATE accounts SET balance = ? WHERE username = ?', [transfer_data[3], user_to])
- print('You have transferred {} to {}'.format(amount, user_to))
- conn.commit()
- else:
- print('Invalid input')
- home();
- else:
- print('No account found.')
- home();
- def startmenu():
- print('\nPyBank version 2')
- print('================')
- print('1 - Login')
- print('2 - Register')
- user_choice = input('> ')
- if(user_choice == '1'):
- login();
- elif(user_choice == '2'):
- register();
- else:
- print('Invalid input, login or register only.')
- startmenu();
- def is_number(s):
- try:
- float(s)
- return True
- except ValueError:
- pass
- try:
- import unicodedata
- unicodedata.numeric(s)
- return True
- except (TypeError, ValueError):
- pass
- 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.
- startmenu();
- # TO DO
- # 1) Register system [x]
- # 2) Cipher system
- # 3) Login system [x]
- # 4) Home screen [x]
- # 5) Withdraw money [x]
- # 6) Transfer money to another account
- # 7) Deposit money [x]
- # 8) Possibly a system to send emails.
- # 9) Make into a GUI
- # 10) Change password
- # 11) Account deletion
Add Comment
Please, Sign In to add comment