diff --git a/ddbank.py b/ddbank.py index 7fc4c4d..210a946 100755 --- a/ddbank.py +++ b/ddbank.py @@ -2,6 +2,29 @@ import sqlite3 import os clear = lambda: os.system('clear') + +def transfer(a): + clear() + print() + connDb = sqlite3.connect(a) + cur = connDb.cursor() + userIn = input('Amount to transfer: $') + fromUser = input('From user: ') + toUser = input('To user: ') + 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}. ') + print() + manBankin(a) + def deposit(a): clear() print('Deposit: ') @@ -37,7 +60,6 @@ def withdraw(a): connDb.close() def manBankin(a): - clear() bankNam = a.split(".")[0] print(f'Transactional menu for the {bankNam} bank. ') while True: @@ -54,7 +76,7 @@ def manBankin(a): elif userIn == "2": deposit(a) elif userIn == "3": - print("Transfer") + transfer(a) elif userIn == "4": print("User balance") elif userIn == "9": @@ -84,15 +106,14 @@ def deluser(a): def listuser(a): connDb = sqlite3.connect(a) cur = connDb.cursor() - userQuery = cur.execute("SELECT username FROM account;") - userQuery = cur.fetchone() + userQuery = cur.execute("SELECT username FROM account;").fetchall()[0:] connDb.close() matchs = userQuery print() print("User list: ") print("__________________________") for i in matchs: - print(i.split(".")[0]) + print(i[0]) print("__________________________") def addUser(a): @@ -168,7 +189,7 @@ def addBank(): TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP, ACTIVE BOOLEAN NOT NULL);''') connDb.execute('''CREATE TABLE balance (ID INTEGER PRIMARY KEY AUTOINCREMENT, - BALANCE INTEGER CHECK(BALANCE >= 0), + BALANCE DOUBLE CHECK(BALANCE >= 0), TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''') connDb.execute('''CREATE TABLE banktrans (ID INTEGER PRIMARY KEY,