Added withdraw from user functionality

master
Matt Theissen 3 years ago
parent 390b70d562
commit b5ab02c25a

@ -1,6 +1,24 @@
#!/usr/bin/env python3
import sqlite3
import os
def withdraw(a):
connDb = sqlite3.connect(a)
cur = connDb.cursor()
withUser = input("User?: ")
userIn = int(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, ))
prevAmt = cur.fetchone()[0]
if (prevAmt - userIn) < 0:
print("*** User cannot have a negative balance. ")
withdraw(a)
else:
cur.execute("UPDATE balance SET balance=(?-?) WHERE id=?", (prevAmt, userIn, userId, ))
connDb.commit()
connDb.close()
def manBankin(a):
while True:
bankNam = a.split(".")[0]
@ -9,11 +27,11 @@ def manBankin(a):
2. Deposit
3. Transfer
4. User Balance check
Q. Quit/Go Back
Q. Go Back
""")
userIn = input(f"Bank/{bankNam}: ")
if userIn == "1":
print("Withdraw")
withdraw(a)
elif userIn == "2":
print("Deposit")
elif userIn == "3":
@ -53,7 +71,7 @@ def addUser(a):
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 banktrans (username) VALUES(?)", (userIn,))
cur.execute("INSERT INTO banktrans (username,amount) VALUES(?,0.00)", (userIn,))
connDb.commit()
connDb.close()
@ -64,7 +82,7 @@ def manageBankmenu(a):
2. Delete a user.
3. List users.
4. Banking Menu.
Q to quit.
Q. Go Back.
""")
menuOption = input("Bank: Choose from the menu: ")
if menuOption == '1':
@ -101,11 +119,12 @@ def addBank():
TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP, ACTIVE BOOLEAN NOT NULL);''')
connDb.execute('''CREATE TABLE balance
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
BALANCE REAL NOT NULL,
BALANCE INTEGER CHECK(BALANCE >= 0),
TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''')
connDb.execute('''CREATE TABLE banktrans
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
(ID INTEGER PRIMARY KEY,
USERNAME TEXT NOT NULL,
AMOUNT INTEGER NOT NULL,
TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''')
connDb.close()
os.system('clear')