def getusers(user): "display other users w/ bankaccout nos." cursor = get_Cursor() print() # acc is the account no. of the current user acc = user.accno query = ( "select accno, firstname, lastname from users where accno <> %s and passwd <> 'administrator123'" % (acc) ) cursor.execute(query) # users table users = cursor.fetchall() if users == []: print("No users") else: print("{:<12} {:<15} {:<15}".format("Account no.", "Firstname", "Lastname")) for row in users: print( "{:<12} {:<15} {:<15}".format( row[0], row[1], row[2], ) ) print()
def deposit(user): "Deposit money to current users acc." db = get_DB() cursor = get_Cursor() money = int(input("Amount to be deposited:")) acc = user.accno # acc is the account no. of the current user balance = int(user.balance[0]) balance += money # in users table query = "update users set balance = %s where accno=%s" % (balance, acc) cursor.execute(query) # in transaction table cursor.execute( "insert into transactionhistory(user2accno, amount) values(%s,%s)", (acc, money)) db.commit() # new balance object cursor.execute("select balance from users where accno = %s", (acc, )) balance = cursor.fetchone() user.balance = balance print("Deposited Rs.", money, "in account-", acc)
def transactionHistory(user): """ Display transaction history of current user """ # get all transactions involving current user (recent to oldest) cursor = get_Cursor() cursor.execute( "SELECT * FROM transactionhistory WHERE user1accno = {0} OR user2accno = {0} ORDER BY time_of_transaction DESC".format( user.accno, ) ) transaction_hist = [] for transaction in cursor.fetchall(): transaction_hist.append(Transaction.fromTuple(transaction)) if transaction_hist == []: print("No Transactions") else: # print transactions print( "{:<23} {:<32} {:<12} {:<12}".format( "Date", "Description", "Withdrawal", "Deposit" ) ) for transaction in transaction_hist: transaction.print(user)
def transfer(user): "Transfer money to other users" print() balance = user.balance[0] if balance == 0: # check for balance print("You can't transfer money.") print("Balance: 0") else: db = get_DB() cursor = get_Cursor() acc = user.accno acc2 = int(input("Account number of the recipient:")) cursor.execute("select * from users") # cursor = get_Cursor() data = cursor.fetchall() a = False for row in data: if row[2] == acc2 and acc2 != acc: a = True money = int(input("Amount to be trasfered:")) if money > balance: print() print("Amount exceeds current balance.") print("Can't transfer.") print() else: # in users table query = "update users set balance = balance - %s where accno=%s" % ( money, acc, ) cursor.execute(query) query2 = ( "update users set balance = balance + %s where accno=%s" % ( money, acc2, )) cursor.execute(query2) db.commit # in transaction table cursor.execute( "insert into transactionhistory(user1accno, user2accno, amount) values(%s,%s,%s)", (acc, acc2, money), ) db.commit() # new balance object cursor.execute( "select balance from users where accno = %s", (acc, )) balance = cursor.fetchone() user.balance = balance print("Transfered Rs.", money, "to account no.-", acc2) print() if a == False: print("Account no.", acc2, "doesn't exist.") print()
def userauthentication(): import getpass cursor = get_Cursor() """ Gets account no., password and check in db returns user object """ flag = False cursor.execute("select * from users") # cursor = get_Cursor() data = cursor.fetchall() while flag == False: # user authentication acc = int(input("Enter your account no.:")) if acc == 0: break passwd = getpass.getpass("Enter your password:"******"Account no. or the password is wrong. Try again.") print("Press 0 to skip signin") # returns true if user verified else false if flag == True: # get firstname, lastname, datecreated, and balance cursor.execute("select firstname from users where accno = %s", (acc, )) firstname = cursor.fetchone() cursor.execute("select lastname from users where accno = %s", (acc, )) lastname = cursor.fetchone() cursor.execute("select date_created from users where accno = %s", (acc, )) datecreated = cursor.fetchone() cursor.execute("select balance from users where accno = %s", (acc, )) balance = cursor.fetchone() # return User object print("You are signed in.") return User(acc, firstname, lastname, datecreated, balance) if flag == False: return None
def print(self, currentuser): if self.user1accno is None or self.user2accno is None: # withdrawal or deposit description = "Self" if self.user1accno is None: withdrawal = "NULL" deposit = self.amount elif self.user2accno is None: withdrawal = self.amount deposit = "NULL" else: # transfer to user2accno / from user1accno cursor = get_Cursor() if self.user1accno == currentuser.accno: # balance decreased cursor.execute( "SELECT firstname, lastname FROM users WHERE accno = {}".format( self.user2accno ) ) output = cursor.fetchone() description = output[0] + " " + output[1] withdrawal = self.amount deposit = "NULL" elif self.user2accno == currentuser.accno: # balance increased cursor.execute( "SELECT firstname, lastname FROM users WHERE accno = {}".format( self.user1accno ) ) output = cursor.fetchone() description = output[0] + " " + output[1] withdrawal = "NULL" deposit = self.amount print( "{} {:<32} {:<12} {:<12}".format( self.time_of_transaction, description, withdrawal, deposit, ) )
def getusers(user): "display other users w/ bankaccout nos." cursor = get_Cursor() # acc is the account no. of the current user acc = user.accno query = "select accno, firstname, lastname from users where accno <> %s" % (acc,) cursor.execute(query) # users table print("{:<12} {:<15} {:<15}".format("Account no.", "Firstname", "Lastname")) for row in cursor.fetchall(): print( "{:<12} {:<15} {:<15}".format( row[0], row[1], row[2], ) )
def withdraw(user): "Withdraw money from current users acc." print() balance = user.balance[0] if balance == 0: # check for balance print() print("You can't withdraw money.") print("Balance: 0") print() else: money = int(input("Amount to be withdrawn:")) if money > balance: print() print("Amount exceeds current balance.") print("Can't withdraw.") print() else: balance -= money db = get_DB() cursor = get_Cursor() acc = user.accno # acc is the account no. of the current user # in users table query = "update users set balance = %s where accno=%s" % (balance, acc) cursor.execute(query) # in transaction table cursor.execute( "insert into transactionhistory(user1accno, amount) values(%s,%s)", (acc, money), ) db.commit() # new balance object cursor.execute("select balance from users where accno = %s", (acc, )) balance = cursor.fetchone() user.balance = balance print("Withdrew Rs.", money, "from account-", acc) print()
def usercreation(): """ Gets name, password(twice), and updates db. user gets auto-generated accno. """ import getpass cursor = get_Cursor() db = get_DB() fname = input("Your firstname:") lname = input("Your lastname:") flag = True while flag: # taking password twice and cnfirming password for i in range(0, 2): if i == 0: passwd = getpass.getpass("Enter a password:"******"Confirm password:"******"Password confirmed.") flag = False else: print("Passwords do not match.") print("Try again.") flag = True cursor.execute( "insert into users(firstname,lastname,passwd) values(%s,%s,%s)", (fname, lname, passwd), ) # cursor=get_Cursor() db.commit() print("Created new account.") cursor.execute("select max(accno) from users;") acc = cursor.fetchone() print("Your account number is-", acc[0]) print("Type 'details' to check your account details.")
def updateinfo(user): print() db = get_DB() acc = user.accno cursor = get_Cursor() flag = True while flag == True: print("firstname | lastname | password") print() q = input("What would you like to change from above?") if q == "firstname": fname = input("Your firstname:") query = "update users set firstname = %s where accno=%s" val = (fname, acc) cursor.execute(query, val) db.commit() print("firstname would be updated after you signout") elif q == "lastname": lname = input("Your lastname:") query = "update users set lastname = %s where accno=%s" val = (lname, acc) cursor.execute(query, val) db.commit() print("lastname would be updated after you signout.") elif q == "password": query = "select * from users where accno=%s" % (acc) cursor.execute(query) data = cursor.fetchall() cur_pass = getpass.getpass("Enter your current password:"******"Enter new password:"******"Confirm new password:"******"Password confirmed.") flag = False else: print("Passwords do not match.") print("Try again.") print() flag = True break else: print("Password incorrect.") if flag == False: query = "update users set passwd = %s where accno=%s" % ( passwd2, acc, ) cursor.execute(query) db.commit() print("Password updated") else: print("You didnt choose the correct option") print() question = input("Would you like to change anything else?(yes/no)") if question == "no": flag = False elif question == "yes": flag == True print()
def delete(user): db = get_DB() acc = user.accno cursor = get_Cursor() query = "select * from users where accno=%s" % (acc) cursor.execute(query) data = cursor.fetchall() print() print("you're account will be permanently closed") confirm = input("Are you sure you want to continue?(yes/no)") if confirm == "yes": flag = False while flag == False: # user authentication print() acc = int(input("Enter your account no.:")) if acc == 0: break passwd = getpass.getpass("Enter your password:"******"Account no. and password confirmed.") flag = True break if flag == False: print() print("Account no. or the password is wrong. Try again.") print("Press 0 to cancel") if flag == True: print() confirm2 = input( "The account will now be closed. Press 'enter' to continue or 'c' to cancel." ) if confirm2 == "c": print("Canceled account deletion.") else: money = user.balance[0] if money != 0: print() acc2 = int( input( "To which account no. would you like to transfer your money" ) ) cursor.execute("select * from users") # cursor = get_Cursor() data = cursor.fetchall() a = False for row in data: if row[2] == acc2 and acc2 != acc: a = True # in users table query = ( "update users set balance = balance - %s where accno=%s" % ( money, acc, ) ) cursor.execute(query) query2 = ( "update users set balance = balance + %s where accno=%s" % ( money, acc2, ) ) cursor.execute(query2) db.commit # in transaction table cursor.execute( "insert into transactionhistory(user1accno, user2accno, amount) values(%s,%s,%s)", (acc, acc2, money), ) db.commit() print("Transfered Rs.", money, "to account no.-", acc2) break if a == False: print("Account no.", acc2, "doesn't exist.") else: print("Balance: 0") val = ("administrator123", acc) query = "update users set passwd = %s where accno=%s" cursor.execute(query, val) db.commit() print() print("Account has been closed.") print("You are signed out.") print() return None