Now checks for valid banks names

master
Matt Theissen 3 years ago
parent 64f425e8c8
commit 2be6a56249

@ -6,7 +6,14 @@ clear = lambda: os.system('clear')
def return_31(wrgUser,a): def return_31(wrgUser,a):
clear() clear()
print(f"31: User {wrgUser} not found. Select menu 3 to see a complete list of users.") print(f"31: User {wrgUser} not found. Select menu 3 to see a complete list of users.")
print()
manageBankmenu(a) manageBankmenu(a)
def return_37(a):
clear()
print(f"37: Bank {a} not found. ")
print()
mainFunc()
def userCheck(use,a): def userCheck(use,a):
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
@ -20,19 +27,25 @@ def userCheck(use,a):
if userCheck.usrtst != "pass": if userCheck.usrtst != "pass":
return_31(use,a) return_31(use,a)
def checkBank(a):
dirOutput = os.listdir()
matchs = [match for match in dirOutput if 'db.sqlite' in match]
for i in matchs:
checkBank.bnktst = 'fail'
if a == i.split(".")[0]:
checkBank.bnktst = 'pass'
if checkBank.bnktst != "pass":
return_37(a)
def transfer(a): def transfer(a):
clear() clear()
print() print()
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
cur = connDb.cursor() cur = connDb.cursor()
userIn = input('Amount to transfer: $') userIn = input('Amount to transfer: $').lower()
fromUser = input('From user: ') fromUser = input('From user: ').lower()
# userQuery = cur.execute("SELECT username FROM account;").fetchall()[0:]
# matchs = userQuery
# if userIn != for i in matchs:
# print(i[0])
userCheck(fromUser,a) userCheck(fromUser,a)
toUser = input('To user: ') toUser = input('To user: ').lower()
userCheck(toUser,a) userCheck(toUser,a)
fromId = cur.execute("SELECT id FROM account where username=?", (fromUser, )).fetchone()[0] fromId = cur.execute("SELECT id FROM account where username=?", (fromUser, )).fetchone()[0]
toId = cur.execute("SELECT id FROM account where username=?", (toUser, )).fetchone()[0] toId = cur.execute("SELECT id FROM account where username=?", (toUser, )).fetchone()[0]
@ -55,9 +68,10 @@ def deposit(a):
print('Deposit: ') print('Deposit: ')
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
cur = connDb.cursor() cur = connDb.cursor()
withUser = input("User?: ") depUser = input("User?: ").lower()
userIn = input(f'{withUser}: Amount to deposit: ') userCheck(depUser,a)
cur.execute("SELECT id FROM account where username=?", (withUser, )) userIn = input(f'{depUser}: Amount to deposit: ').lower()
cur.execute("SELECT id FROM account where username=?", (depUser, ))
userId = cur.fetchone()[0] userId = cur.fetchone()[0]
cur.execute("SELECT balance from balance1 where id=?", (userId, )) cur.execute("SELECT balance from balance1 where id=?", (userId, ))
prevAmt = cur.fetchone()[0] prevAmt = cur.fetchone()[0]
@ -69,8 +83,9 @@ def withdraw(a):
print('Withdraw: ') print('Withdraw: ')
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
cur = connDb.cursor() cur = connDb.cursor()
withUser = input("User?: ") withUser = input("User?: ").lower()
userIn = input(f'{withUser}: Amount to withdraw: ') userCheck(withUser,a)
userIn = input(f'{withUser}: Amount to withdraw: ').lower()
cur.execute("SELECT id FROM account where username=?", (withUser, )) cur.execute("SELECT id FROM account where username=?", (withUser, ))
userId = cur.fetchone()[0] userId = cur.fetchone()[0]
cur.execute("SELECT balance from balance1 where id=?", (userId, )) cur.execute("SELECT balance from balance1 where id=?", (userId, ))
@ -94,7 +109,7 @@ def manBankin(a):
4. User Balance check 4. User Balance check
9. Go Back 9. Go Back
""") """)
userIn = input(f"Bank/{bankNam}: ") userIn = input(f"Bank/{bankNam}: ").lower()
if userIn == "1": if userIn == "1":
withdraw(a) withdraw(a)
elif userIn == "2": elif userIn == "2":
@ -115,7 +130,7 @@ def deluser(a):
bankNam = a.split(".")[0] bankNam = a.split(".")[0]
print(f'Delete a user from bank {bankNam}. ') print(f'Delete a user from bank {bankNam}. ')
print() print()
userIn = input("Which user would you like to delete? ") userIn = input("Which user would you like to delete? ").lower()
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
cur = connDb.cursor() cur = connDb.cursor()
cur.execute("""UPDATE account SET active = 0 where username = ?""", (userIn,)) cur.execute("""UPDATE account SET active = 0 where username = ?""", (userIn,))
@ -147,7 +162,7 @@ def addUser(a):
print() print()
connDb = sqlite3.connect(a) connDb = sqlite3.connect(a)
cur = connDb.cursor() cur = connDb.cursor()
userIn = input('What user would you like to add? ') userIn = input('What user would you like to add? ').lower()
cur.execute("INSERT INTO account (username,active) VALUES(?,1)", (userIn,)) cur.execute("INSERT INTO account (username,active) VALUES(?,1)", (userIn,))
cur.execute("INSERT INTO balance1 (balance) VALUES(0)") cur.execute("INSERT INTO balance1 (balance) VALUES(0)")
cur.execute("INSERT INTO balance2 (balance) VALUES(0)") cur.execute("INSERT INTO balance2 (balance) VALUES(0)")
@ -162,6 +177,7 @@ def addUser(a):
def manageBankmenu(a): def manageBankmenu(a):
bankNam = a.split(".")[0] bankNam = a.split(".")[0]
print()
print(f'You are managing the {bankNam} bank. ') print(f'You are managing the {bankNam} bank. ')
print() print()
while True: while True:
@ -172,7 +188,7 @@ def manageBankmenu(a):
4. Banking Menu. 4. Banking Menu.
9. Go Back. 9. Go Back.
""") """)
menuOption = input("Bank: Choose from the menu: ") menuOption = input("Bank: Choose from the menu: ").lower()
if menuOption == '1': if menuOption == '1':
addUser(a) addUser(a)
elif menuOption == '2': elif menuOption == '2':
@ -192,19 +208,20 @@ def selectBank():
print() print()
dirOutput = os.listdir() dirOutput = os.listdir()
matchs = [match for match in dirOutput if 'db.sqlite' in match] matchs = [match for match in dirOutput if 'db.sqlite' in match]
#banks = dict(zip(matchs, range(len(matchs))))
for i in matchs: for i in matchs:
print(i.split(".")[0]) print(i.split(".")[0])
print() print()
manBank = input('Choose which bank you would like to manage: ') manBank = input('Choose which bank you would like to manage: ').lower()
checkBank(manBank)
manBank = manBank+".db.sqlite" manBank = manBank+".db.sqlite"
clear()
manageBankmenu(manBank) manageBankmenu(manBank)
def addBank(): def addBank():
clear() clear()
print('Add a bank: ') print('Add a bank: ')
print() print()
bankName = input("What would you like to call this bank? ") bankName = input("What would you like to call this bank? ").lower()
bankName = bankName+".db.sqlite" bankName = bankName+".db.sqlite"
connDb = sqlite3.connect(bankName) connDb = sqlite3.connect(bankName)
connDb.execute('''CREATE TABLE account connDb.execute('''CREATE TABLE account
@ -241,8 +258,25 @@ def listBank():
for i in matchs: for i in matchs:
print(i.split(".")[0]) print(i.split(".")[0])
print("__________________________") print("__________________________")
print()
def delBank():
listBank()
delIn = input('Which bank would you like to delete? ').lower()
checkBank(delIn)
delIn = delIn + '.db.sqlite'
confIn = input(f'Confirm you want to delete bank {delIn}? [y/N] ').lower()
if confIn == 'n' or confIn == "":
clear()
mainFunc()
elif confIn == 'y':
cmd = 'rm {0}'.format(delIn)
os.system(cmd)
clear()
print(f'Bank {delBank} has been deleted.')
print()
def mainFunc(): def mainFunc():
clear()
print('Welcome to the Kniod D&D Banking Software! ') print('Welcome to the Kniod D&D Banking Software! ')
while True: while True:
print(""" print("""
@ -252,20 +286,16 @@ def mainFunc():
4. Delete a bank. 4. Delete a bank.
Q to quit. Q to quit.
""") """)
mainOption = input("Choose an option: ") mainOption = int(input("Choose an option: "))
if mainOption == '1': if mainOption == 1:
clear()
selectBank() selectBank()
elif mainOption == '2': elif mainOption == 2:
clear()
addBank() addBank()
elif mainOption == '3': elif mainOption == 3:
clear()
listBank() listBank()
elif mainOption == '4': elif mainOption == 4:
clear() delBank()
print('Place function here.') elif mainOption == 'Q':
elif mainOption == 'Q' or mainOption == 'q':
exit() exit()
else: else: