#!/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] print(""" 1. Withdraw 2. Deposit 3. Transfer 4. User Balance check Q. Go Back """) userIn = input(f"Bank/{bankNam}: ") if userIn == "1": withdraw(a) elif userIn == "2": print("Deposit") elif userIn == "3": print("Transfer") elif userIn == "4": print("User balance") elif userIn == "q" or userIn == "Q": break else: print('*** Option not understood. Please try again.') def deluser(a): userIn = input("Which user would you like to delete? ") connDb = sqlite3.connect(a) cur = connDb.cursor() cur.execute("""UPDATE account SET active = 0 where username = ?""", (userIn,)) connDb.commit() connDb.close() def listuser(a): connDb = sqlite3.connect(a) cur = connDb.cursor() userQuery = cur.execute("SELECT username FROM account;") userQuery = cur.fetchone() connDb.close() matchs = userQuery print() print("User list: ") print("__________________________") for i in matchs: print(i.split(".")[0]) print("__________________________") def addUser(a): connDb = sqlite3.connect(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 banktrans (username,amount) VALUES(?,0.00)", (userIn,)) connDb.commit() connDb.close() def manageBankmenu(a): while True: print(""" 1. Add a user. 2. Delete a user. 3. List users. 4. Banking Menu. Q. Go Back. """) menuOption = input("Bank: Choose from the menu: ") if menuOption == '1': addUser(a) elif menuOption == '2': deluser(a) elif menuOption == '3': listuser(a) elif menuOption == '4': manBankin(a) elif menuOption == 'q' or menuOption == 'Q': break else: print('*** Option not understood. Please try again.') def selectBank(): dirOutput = os.listdir() matchs = [match for match in dirOutput if 'db.sqlite' in match] #banks = dict(zip(matchs, range(len(matchs)))) for i in matchs: print(i.split(".")[0]) print() manBank = input('Choose which bank you would like to manage: ') manBank = manBank+".db.sqlite" manageBankmenu(manBank) def addBank(): bankName = input("What would you like to call this bank? ") bankName = bankName+".db.sqlite" connDb = sqlite3.connect(bankName) connDb.execute('''CREATE TABLE account (ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT NOT NULL, TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP, ACTIVE BOOLEAN NOT NULL);''') connDb.execute('''CREATE TABLE balance (ID INTEGER PRIMARY KEY AUTOINCREMENT, BALANCE INTEGER CHECK(BALANCE >= 0), TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''') connDb.execute('''CREATE TABLE banktrans (ID INTEGER PRIMARY KEY, USERNAME TEXT NOT NULL, AMOUNT INTEGER NOT NULL, TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''') connDb.close() os.system('clear') print() print(f'*** Added bank {bankName}') print() def listBank(): dirOutput = os.listdir() matchs = [match for match in dirOutput if 'db.sqlite' in match] print() print('Banks found: ') print("__________________________") for i in matchs: print(i.split(".")[0]) print("__________________________") while True: print(""" 1. Select a Bank. 2. Add new bank. 3. List banks. 4. Delete a bank. Q to quit. """) mainOption = input("Choose an option: ") if mainOption == '1': selectBank() elif mainOption == '2': addBank() elif mainOption == '3': listBank() elif mainOption == '4': print('Place function here.') elif mainOption == 'Q' or mainOption == 'q': break else: print() print('*** Option not understood. Please try again.')