Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,24 +21,26 @@ conn.commit()
|
|
| 21 |
|
| 22 |
def create_account(name, age, password):
|
| 23 |
acc_no = random.randint(1000, 9999)
|
| 24 |
-
|
| 25 |
cursor.execute("SELECT * FROM accounts WHERE name=?", (name,))
|
| 26 |
if cursor.fetchone():
|
| 27 |
-
return "β Account already exists!"
|
| 28 |
|
| 29 |
cursor.execute("INSERT INTO accounts VALUES (?, ?, ?, ?, ?)",
|
| 30 |
(name, age, password, acc_no, 0))
|
| 31 |
conn.commit()
|
| 32 |
-
|
| 33 |
-
return
|
| 34 |
|
| 35 |
def login(name, acc_no, password):
|
| 36 |
cursor.execute("SELECT * FROM accounts WHERE name=? AND account_number=? AND password=?",
|
| 37 |
(name, acc_no, password))
|
| 38 |
return cursor.fetchone()
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def deposit(user, amount):
|
| 44 |
new_balance = user[4] + amount
|
|
@@ -49,60 +51,98 @@ def deposit(user, amount):
|
|
| 49 |
|
| 50 |
def withdraw(user, amount):
|
| 51 |
if user[4] < amount:
|
| 52 |
-
return "β Insufficient Balance"
|
| 53 |
-
|
| 54 |
new_balance = user[4] - amount
|
| 55 |
cursor.execute("UPDATE accounts SET balance=? WHERE account_number=?",
|
| 56 |
(new_balance, user[3]))
|
| 57 |
conn.commit()
|
| 58 |
-
return new_balance
|
| 59 |
|
| 60 |
-
# ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
st.title("π¦ Baryashah Bank")
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# CREATE ACCOUNT
|
| 67 |
if menu == "Create Account":
|
| 68 |
st.subheader("Create New Account")
|
|
|
|
| 69 |
name = st.text_input("Name")
|
| 70 |
age = st.number_input("Age", min_value=1)
|
| 71 |
password = st.text_input("Password", type="password")
|
| 72 |
|
| 73 |
-
if st.button("Create"):
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
# LOGIN
|
| 78 |
elif menu == "Login":
|
| 79 |
st.subheader("Login")
|
|
|
|
| 80 |
name = st.text_input("Name")
|
| 81 |
acc_no = st.number_input("Account Number")
|
| 82 |
password = st.text_input("Password", type="password")
|
| 83 |
|
| 84 |
if st.button("Login"):
|
| 85 |
user = login(name, acc_no, password)
|
| 86 |
-
|
| 87 |
if user:
|
|
|
|
|
|
|
| 88 |
st.success("β
Login Successful")
|
| 89 |
-
|
| 90 |
-
option = st.selectbox("Choose", ["Check Balance", "Deposit", "Withdraw"])
|
| 91 |
-
|
| 92 |
-
if option == "Check Balance":
|
| 93 |
-
st.info(f"π° Balance: {get_balance(user)}")
|
| 94 |
-
|
| 95 |
-
elif option == "Deposit":
|
| 96 |
-
amount = st.number_input("Amount", min_value=1)
|
| 97 |
-
if st.button("Deposit"):
|
| 98 |
-
new_balance = deposit(user, amount)
|
| 99 |
-
st.success(f"New Balance: {new_balance}")
|
| 100 |
-
|
| 101 |
-
elif option == "Withdraw":
|
| 102 |
-
amount = st.number_input("Amount", min_value=1)
|
| 103 |
-
if st.button("Withdraw"):
|
| 104 |
-
result = withdraw(user, amount)
|
| 105 |
-
st.success(result)
|
| 106 |
-
|
| 107 |
else:
|
| 108 |
-
st.error("β Invalid Credentials")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def create_account(name, age, password):
|
| 23 |
acc_no = random.randint(1000, 9999)
|
| 24 |
+
|
| 25 |
cursor.execute("SELECT * FROM accounts WHERE name=?", (name,))
|
| 26 |
if cursor.fetchone():
|
| 27 |
+
return None, "β Account already exists!"
|
| 28 |
|
| 29 |
cursor.execute("INSERT INTO accounts VALUES (?, ?, ?, ?, ?)",
|
| 30 |
(name, age, password, acc_no, 0))
|
| 31 |
conn.commit()
|
| 32 |
+
|
| 33 |
+
return acc_no, "β
Account Created!"
|
| 34 |
|
| 35 |
def login(name, acc_no, password):
|
| 36 |
cursor.execute("SELECT * FROM accounts WHERE name=? AND account_number=? AND password=?",
|
| 37 |
(name, acc_no, password))
|
| 38 |
return cursor.fetchone()
|
| 39 |
|
| 40 |
+
def get_user(name, acc_no, password):
|
| 41 |
+
cursor.execute("SELECT * FROM accounts WHERE name=? AND account_number=? AND password=?",
|
| 42 |
+
(name, acc_no, password))
|
| 43 |
+
return cursor.fetchone()
|
| 44 |
|
| 45 |
def deposit(user, amount):
|
| 46 |
new_balance = user[4] + amount
|
|
|
|
| 51 |
|
| 52 |
def withdraw(user, amount):
|
| 53 |
if user[4] < amount:
|
| 54 |
+
return None, "β Insufficient Balance"
|
| 55 |
+
|
| 56 |
new_balance = user[4] - amount
|
| 57 |
cursor.execute("UPDATE accounts SET balance=? WHERE account_number=?",
|
| 58 |
(new_balance, user[3]))
|
| 59 |
conn.commit()
|
| 60 |
+
return new_balance, "β
Withdrawal Successful"
|
| 61 |
|
| 62 |
+
# ---------------- SESSION INIT ----------------
|
| 63 |
+
if "logged_in" not in st.session_state:
|
| 64 |
+
st.session_state.logged_in = False
|
| 65 |
+
|
| 66 |
+
# ---------------- UI ----------------
|
| 67 |
|
| 68 |
st.title("π¦ Baryashah Bank")
|
| 69 |
|
| 70 |
+
# ---------------- SIDEBAR ----------------
|
| 71 |
+
if not st.session_state.logged_in:
|
| 72 |
+
menu = st.sidebar.selectbox("Menu", ["Create Account", "Login"])
|
| 73 |
+
else:
|
| 74 |
+
menu = "Dashboard"
|
| 75 |
|
| 76 |
+
# ---------------- CREATE ACCOUNT ----------------
|
| 77 |
if menu == "Create Account":
|
| 78 |
st.subheader("Create New Account")
|
| 79 |
+
|
| 80 |
name = st.text_input("Name")
|
| 81 |
age = st.number_input("Age", min_value=1)
|
| 82 |
password = st.text_input("Password", type="password")
|
| 83 |
|
| 84 |
+
if st.button("Create Account"):
|
| 85 |
+
acc_no, msg = create_account(name, age, password)
|
| 86 |
+
if acc_no:
|
| 87 |
+
st.success(f"{msg} π")
|
| 88 |
+
st.info(f"Your Account Number: {acc_no}")
|
| 89 |
+
else:
|
| 90 |
+
st.error(msg)
|
| 91 |
|
| 92 |
+
# ---------------- LOGIN ----------------
|
| 93 |
elif menu == "Login":
|
| 94 |
st.subheader("Login")
|
| 95 |
+
|
| 96 |
name = st.text_input("Name")
|
| 97 |
acc_no = st.number_input("Account Number")
|
| 98 |
password = st.text_input("Password", type="password")
|
| 99 |
|
| 100 |
if st.button("Login"):
|
| 101 |
user = login(name, acc_no, password)
|
| 102 |
+
|
| 103 |
if user:
|
| 104 |
+
st.session_state.logged_in = True
|
| 105 |
+
st.session_state.user = (name, acc_no, password)
|
| 106 |
st.success("β
Login Successful")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
+
st.error("β Invalid Credentials")
|
| 109 |
+
|
| 110 |
+
# ---------------- DASHBOARD ----------------
|
| 111 |
+
elif menu == "Dashboard":
|
| 112 |
+
|
| 113 |
+
name, acc_no, password = st.session_state.user
|
| 114 |
+
user = get_user(name, acc_no, password) # π₯ always fresh data
|
| 115 |
+
|
| 116 |
+
st.subheader(f"Welcome, {name} π")
|
| 117 |
+
|
| 118 |
+
option = st.selectbox("Choose Action", ["Check Balance", "Deposit", "Withdraw"])
|
| 119 |
+
|
| 120 |
+
# CHECK BALANCE
|
| 121 |
+
if option == "Check Balance":
|
| 122 |
+
st.info(f"π° Your Balance: {user[4]}")
|
| 123 |
+
|
| 124 |
+
# DEPOSIT
|
| 125 |
+
elif option == "Deposit":
|
| 126 |
+
amount = st.number_input("Enter Amount", min_value=1)
|
| 127 |
+
if st.button("Deposit"):
|
| 128 |
+
new_balance = deposit(user, amount)
|
| 129 |
+
st.success(f"β
Deposited Successfully")
|
| 130 |
+
st.info(f"π° New Balance: {new_balance}")
|
| 131 |
+
|
| 132 |
+
# WITHDRAW
|
| 133 |
+
elif option == "Withdraw":
|
| 134 |
+
amount = st.number_input("Enter Amount", min_value=1)
|
| 135 |
+
if st.button("Withdraw"):
|
| 136 |
+
new_balance, msg = withdraw(user, amount)
|
| 137 |
+
|
| 138 |
+
if new_balance is None:
|
| 139 |
+
st.error(msg)
|
| 140 |
+
else:
|
| 141 |
+
st.success(msg)
|
| 142 |
+
st.info(f"π° Remaining Balance: {new_balance}")
|
| 143 |
+
|
| 144 |
+
# LOGOUT
|
| 145 |
+
if st.button("Logout"):
|
| 146 |
+
st.session_state.logged_in = False
|
| 147 |
+
st.session_state.user = None
|
| 148 |
+
st.success("π Logged out successfully")
|