diff --git a/ddbank.py b/ddbank.py index 210a946..1f430c1 100755 --- a/ddbank.py +++ b/ddbank.py @@ -3,6 +3,23 @@ import sqlite3 import os clear = lambda: os.system('clear') +def return_31(wrgUser,a): + clear() + print(f"31: User {wrgUser} not found. Select menu 3 to see a complete list of users.") + manageBankmenu(a) + +def userCheck(use,a): + connDb = sqlite3.connect(a) + cur = connDb.cursor() + usrlist = cur.execute("SELECT username FROM account;").fetchall() + for i in usrlist: + i = str(i)[2:-3] + userCheck.usrtst = "fail" + if use == i: + userCheck.usrtst = "pass" + if userCheck.usrtst != "pass": + return_31(use,a) + def transfer(a): clear() print() @@ -10,18 +27,26 @@ def transfer(a): cur = connDb.cursor() userIn = input('Amount to transfer: $') fromUser = input('From user: ') + # userQuery = cur.execute("SELECT username FROM account;").fetchall()[0:] + # matchs = userQuery + # if userIn != for i in matchs: + # print(i[0]) + userCheck(fromUser,a) toUser = input('To user: ') + userCheck(toUser,a) fromId = cur.execute("SELECT id FROM account where username=?", (fromUser, )).fetchone()[0] toId = cur.execute("SELECT id FROM account where username=?", (toUser, )).fetchone()[0] - prevFrom = cur.execute("SELECT balance from balance where id=?", (fromId, )).fetchone()[0] - prevTo = cur.execute("SELECT balance from balance where id=?", (toId, )).fetchone()[0] - #if prev - cur.execute("UPDATE balance SET balance=(?-?) WHERE id=?", (prevFrom, userIn, fromId, )) - cur.execute("UPDATE balance SET balance=(?+?) WHERE id=?", (prevTo, userIn, toId, )) - connDb.commit() - connDb.close() - clear() - print(f'Transfered ${userIn} from {fromUser} to {toUser}. ') + prevFrom = cur.execute("SELECT balance from balance1 where id=?", (fromId, )).fetchone()[0] + prevTo = cur.execute("SELECT balance from balance1 where id=?", (toId, )).fetchone()[0] + if int(prevFrom) < int(userIn): + print(f' {fromUser} has insufficient funds to accommodate this transfer.') + else: + cur.execute("UPDATE balance1 SET balance=(?-?) WHERE id=?", (prevFrom, userIn, fromId, )) + cur.execute("UPDATE balance1 SET balance=(?+?) WHERE id=?", (prevTo, userIn, toId, )) + connDb.commit() + connDb.close() + clear() + print(f'Transfered ${userIn} from {fromUser} to {toUser}. ') print() manBankin(a) @@ -31,31 +56,30 @@ def deposit(a): connDb = sqlite3.connect(a) cur = connDb.cursor() withUser = input("User?: ") - userIn = int(input(f'{withUser}: Amount to deposit: ')) + userIn = input(f'{withUser}: Amount to deposit: ') cur.execute("SELECT id FROM account where username=?", (withUser, )) userId = cur.fetchone()[0] - cur.execute("SELECT balance from balance where id=?", (userId, )) + cur.execute("SELECT balance from balance1 where id=?", (userId, )) prevAmt = cur.fetchone()[0] - cur.execute("UPDATE balance SET balance=(?+?) WHERE id=?", (prevAmt, userIn, userId, )) + cur.execute("UPDATE balance1 SET balance=(?+?) WHERE id=?", (prevAmt, userIn, userId, )) connDb.commit() connDb.close() def withdraw(a): - clear() print('Withdraw: ') connDb = sqlite3.connect(a) cur = connDb.cursor() withUser = input("User?: ") - userIn = int(input(f'{withUser}: Amount to withdraw: ')) + userIn = input(f'{withUser}: Amount to withdraw: ') cur.execute("SELECT id FROM account where username=?", (withUser, )) userId = cur.fetchone()[0] - cur.execute("SELECT balance from balance where id=?", (userId, )) + cur.execute("SELECT balance from balance1 where id=?", (userId, )) prevAmt = cur.fetchone()[0] - if (prevAmt - userIn) < 0: + if int(prevAmt) - int(userIn) < 0: print("*** User cannot have a negative balance. ") withdraw(a) else: - cur.execute("UPDATE balance SET balance=(?-?) WHERE id=?", (prevAmt, userIn, userId, )) + cur.execute("UPDATE balance1 SET balance=(?-?) WHERE id=?", (prevAmt, userIn, userId, )) connDb.commit() connDb.close() @@ -125,7 +149,8 @@ def addUser(a): cur = connDb.cursor() userIn = input('What user would you like to add? ') cur.execute("INSERT INTO account (username,active) VALUES(?,1)", (userIn,)) - cur.execute("INSERT INTO balance (balance) VALUES(0.00)") + cur.execute("INSERT INTO balance1 (balance) VALUES(0)") + cur.execute("INSERT INTO balance2 (balance) VALUES(0)") cur.execute("INSERT INTO banktrans (username,amount) VALUES(?,0.00)", (userIn,)) connDb.commit() connDb.close() @@ -136,7 +161,6 @@ def addUser(a): print() def manageBankmenu(a): - clear() bankNam = a.split(".")[0] print(f'You are managing the {bankNam} bank. ') print() @@ -187,9 +211,13 @@ def addBank(): (ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT NOT NULL, TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP, ACTIVE BOOLEAN NOT NULL);''') - connDb.execute('''CREATE TABLE balance + connDb.execute('''CREATE TABLE balance1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, - BALANCE DOUBLE CHECK(BALANCE >= 0), + BALANCE INTEGER CHECK(BALANCE >= 0), + TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''') + connDb.execute('''CREATE TABLE balance2 + (ID INTEGER PRIMARY KEY AUTOINCREMENT, + BALANCE INTEGER CHECK(BALANCE >= 0), TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''') connDb.execute('''CREATE TABLE banktrans (ID INTEGER PRIMARY KEY, @@ -226,15 +254,20 @@ def mainFunc(): """) mainOption = input("Choose an option: ") if mainOption == '1': + clear() selectBank() elif mainOption == '2': + clear() addBank() elif mainOption == '3': + clear() listBank() elif mainOption == '4': + clear() print('Place function here.') elif mainOption == 'Q' or mainOption == 'q': - break + exit() + else: print() print('*** Option not understood. Please try again.')