You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
336 lines
10 KiB
336 lines
10 KiB
#!/usr/bin/env python3
|
|
import sqlite3
|
|
import os
|
|
clear = lambda: os.system('clear')
|
|
|
|
def return_31(wrgUser,a):
|
|
clear()
|
|
print(f"31: User {wrgUser} not found. Select menu 3 to see a complete list of users.")
|
|
print()
|
|
manageBankmenu(a)
|
|
|
|
def return_37(a):
|
|
clear()
|
|
print(f"37: Bank {a} not found. ")
|
|
print()
|
|
mainFunc()
|
|
|
|
def userCheck(use,a):
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
usrlist = cur.execute("SELECT username FROM account;").fetchall()
|
|
for i in usrlist:
|
|
i = str(i)[2:-3]
|
|
userCheck.usrtst = "fail"
|
|
if use == i:
|
|
userCheck.usrtst = "pass"
|
|
if userCheck.usrtst != "pass":
|
|
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:
|
|
print(i)
|
|
checkBank.bnktst = 'fail'
|
|
if a == i.split(".")[0]:
|
|
checkBank.bnktst = 'pass'
|
|
if checkBank.bnktst != "pass":
|
|
return_37(a)
|
|
|
|
def userBalance(a):
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
balUser = input('User? ').lower()
|
|
userCheck(balUser,a)
|
|
cur.execute("SELECT id FROM account where username=?", (balUser, ))
|
|
userId = cur.fetchone()[0]
|
|
bal = cur.execute("SELECT balance from balance1 where id=?", (userId, )).fetchall()
|
|
print(bal)
|
|
|
|
def transfer(a):
|
|
clear()
|
|
print()
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
userIn = input('Amount to transfer: $').lower()
|
|
fromUser = input('From user: ').lower()
|
|
userCheck(fromUser,a)
|
|
toUser = input('To user: ').lower()
|
|
userCheck(toUser,a)
|
|
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 balance1 where id=?", (fromId, )).fetchone()[0]
|
|
prevTo = cur.execute("SELECT balance from balance1 where id=?", (toId, )).fetchone()[0]
|
|
if int(prevFrom) < int(userIn):
|
|
print(f' {fromUser} has insufficient funds to accommodate this transfer.')
|
|
else:
|
|
cur.execute("UPDATE balance1 SET balance=(?-?) WHERE id=?", (prevFrom, userIn, fromId, ))
|
|
cur.execute("UPDATE balance1 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: ')
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
depUser = input("User?: ").lower()
|
|
userCheck(depUser,a)
|
|
userIn = input(f'{depUser}: Amount to deposit: ').lower()
|
|
cur.execute("SELECT id FROM account where username=?", (depUser, ))
|
|
userId = cur.fetchone()[0]
|
|
cur.execute("SELECT balance from balance1 where id=?", (userId, ))
|
|
prevAmt = cur.fetchone()[0]
|
|
cur.execute("UPDATE balance1 SET balance=(?+?) WHERE id=?", (prevAmt, userIn, userId, ))
|
|
connDb.commit()
|
|
connDb.close()
|
|
|
|
def withdraw(a):
|
|
print('Withdraw: ')
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
withUser = input("User?: ").lower()
|
|
userCheck(withUser,a)
|
|
userIn = input(f'{withUser}: Amount to withdraw: ').lower()
|
|
cur.execute("SELECT id FROM account where username=?", (withUser, ))
|
|
userId = cur.fetchone()[0]
|
|
cur.execute("SELECT balance from balance1 where id=?", (userId, ))
|
|
prevAmt = cur.fetchone()[0]
|
|
if int(prevAmt) - int(userIn) < 0:
|
|
print("*** User cannot have a negative balance. ")
|
|
withdraw(a)
|
|
else:
|
|
cur.execute("UPDATE balance1 SET balance=(?-?) WHERE id=?", (prevAmt, userIn, userId, ))
|
|
connDb.commit()
|
|
connDb.close()
|
|
|
|
def manBankin(a):
|
|
bankNam = a.split(".")[0]
|
|
print(f'Transactional menu for the {bankNam} bank. ')
|
|
while True:
|
|
print("""
|
|
1. Withdraw
|
|
2. Deposit
|
|
3. Transfer
|
|
4. User Balance check
|
|
9. Go Back
|
|
""")
|
|
userIn = input(f"Bank/{bankNam}: ").lower()
|
|
if userIn == "1":
|
|
withdraw(a)
|
|
elif userIn == "2":
|
|
deposit(a)
|
|
elif userIn == "3":
|
|
transfer(a)
|
|
elif userIn == "4":
|
|
userBalance(a)
|
|
elif userIn == "9":
|
|
manageBankmenu(a)
|
|
elif userIn == "8":
|
|
mainFunc()
|
|
else:
|
|
print('*** Option not understood. Please try again.')
|
|
|
|
def deluser(a):
|
|
clear()
|
|
bankNam = a.split(".")[0]
|
|
print(f'Delete a user from bank {bankNam}. ')
|
|
print()
|
|
userIn = input("Which user would you like to delete? ").lower()
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
cur.execute("""UPDATE account SET active = 0 where username = ?""", (userIn,))
|
|
connDb.commit()
|
|
connDb.close()
|
|
print()
|
|
print(f'*** Deleted user {userIn} from bank {bankNam}. ')
|
|
print()
|
|
print(f'You are managing the {bankNam} bank. ')
|
|
print()
|
|
|
|
def listuser(a):
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
userQuery = cur.execute("SELECT username FROM account;").fetchall()[0:]
|
|
connDb.close()
|
|
matchs = userQuery
|
|
print()
|
|
print("User list: ")
|
|
print("__________________________")
|
|
for i in matchs:
|
|
print(i[0])
|
|
print("__________________________")
|
|
|
|
def addUser(a):
|
|
clear()
|
|
bankNam = a.split(".")[0]
|
|
print(f'Add a user to bank {bankNam}. ')
|
|
print()
|
|
connDb = sqlite3.connect(a)
|
|
cur = connDb.cursor()
|
|
userIn = input('What user would you like to add? ').lower()
|
|
cur.execute("INSERT INTO account (username,active) VALUES(?,1)", (userIn,))
|
|
cur.execute("INSERT INTO balance1 (balance) VALUES(0)")
|
|
cur.execute("INSERT INTO balance2 (balance) VALUES(0)")
|
|
cur.execute("INSERT INTO banktrans (username,amount) VALUES(?,0.00)", (userIn,))
|
|
connDb.commit()
|
|
connDb.close()
|
|
clear()
|
|
print(f'*** Added user {userIn} to bank {bankNam}. ')
|
|
print()
|
|
print(f"You are managing the {bankNam} bank. ")
|
|
print()
|
|
|
|
def manageBankmenu(a):
|
|
bankNam = a.split(".")[0]
|
|
print()
|
|
print(f'You are managing the {bankNam} bank. ')
|
|
print()
|
|
while True:
|
|
print("""
|
|
1. Add a user.
|
|
2. Delete a user.
|
|
3. List users.
|
|
4. Banking Menu.
|
|
9. Go Back.
|
|
""")
|
|
menuOption = input("Bank: Choose from the menu: ").lower()
|
|
if menuOption == '1':
|
|
addUser(a)
|
|
elif menuOption == '2':
|
|
deluser(a)
|
|
elif menuOption == '3':
|
|
listuser(a)
|
|
elif menuOption == '4':
|
|
manBankin(a)
|
|
elif menuOption == '9':
|
|
mainFunc()
|
|
else:
|
|
print('*** Option not understood. Please try again.')
|
|
|
|
def selectBank():
|
|
clear()
|
|
print('Select a bank: ')
|
|
print()
|
|
dirOutput = os.listdir()
|
|
matchs = [match for match in dirOutput if 'db.sqlite' in match]
|
|
for i in matchs:
|
|
print(i.split(".")[0])
|
|
print()
|
|
manBank = input('Choose which bank you would like to manage: ').lower()
|
|
checkBank(manBank)
|
|
manBank = manBank+".db.sqlite"
|
|
clear()
|
|
manageBankmenu(manBank)
|
|
|
|
def addBank():
|
|
print('Add a bank: ')
|
|
print()
|
|
bankName = input("What would you like to call this bank? ").lower()
|
|
if not bankName:
|
|
print()
|
|
print('Not a valid name. ')
|
|
mainFunc()
|
|
elif bankName.isspace() == True:
|
|
print()
|
|
mainFunc()
|
|
dirOutput = os.listdir()
|
|
matchs = [match for match in dirOutput if 'db.sqlite' in match]
|
|
for i in matchs:
|
|
checkBank.nametst = 'pass'
|
|
if bankName == i.split(".")[0]:
|
|
addBank.nametst = 'fail'
|
|
if addBank.nametst != "pass":
|
|
print()
|
|
print('~Name already exists! ')
|
|
print()
|
|
mainFunc()
|
|
|
|
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 balance1
|
|
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
BALANCE INTEGER CHECK(BALANCE >= 0),
|
|
TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);''')
|
|
connDb.execute('''CREATE TABLE balance2
|
|
(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()
|
|
# clear()
|
|
print()
|
|
print(f'*** Added bank {bankName}')
|
|
print()
|
|
|
|
def listBank():
|
|
clear()
|
|
print()
|
|
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("__________________________")
|
|
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():
|
|
print('Welcome to the Kniod D&D Banking Software! ')
|
|
while True:
|
|
print("""
|
|
1. Select a Bank.
|
|
2. Add new bank.
|
|
3. List banks.
|
|
4. Delete a bank.
|
|
9 to quit.
|
|
""")
|
|
mainOption = int(input("Choose an option: "))
|
|
if mainOption == 1:
|
|
selectBank()
|
|
elif mainOption == 2:
|
|
addBank()
|
|
elif mainOption == 3:
|
|
listBank()
|
|
elif mainOption == 4:
|
|
delBank()
|
|
elif mainOption == 9:
|
|
exit()
|
|
|
|
else:
|
|
print()
|
|
print('*** Option not understood. Please try again.')
|
|
mainFunc()
|
|
|
|
|