From b5ab02c25a68a78e0972cf016601f6219b925085 Mon Sep 17 00:00:00 2001 From: Matt Theissen Date: Sat, 4 Jun 2022 01:20:31 -0500 Subject: [PATCH] Added withdraw from user functionality --- ddbank.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/ddbank.py b/ddbank.py index 6c1351c..f4d896c 100755 --- a/ddbank.py +++ b/ddbank.py @@ -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')