krishbaresha commited on
Commit
5e13bae
Β·
verified Β·
1 Parent(s): cbd1632

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -112
app.py CHANGED
@@ -1,71 +1,89 @@
1
  import streamlit as st
2
- import sqlite3
 
3
  import random
 
 
4
 
5
- # ---------------- DATABASE SETUP ----------------
6
- conn = sqlite3.connect('bank.db', check_same_thread=False)
7
- cursor = conn.cursor()
8
 
9
- cursor.execute('''
10
- CREATE TABLE IF NOT EXISTS accounts (
11
- name TEXT,
12
- age INTEGER,
13
- password TEXT,
14
- account_number INTEGER PRIMARY KEY,
15
- balance INTEGER
16
- )
17
- ''')
18
- conn.commit()
19
 
20
- # ---------------- FUNCTIONS ----------------
 
 
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
47
- cursor.execute("UPDATE accounts SET balance=? WHERE account_number=?",
48
- (new_balance, user[3]))
49
- conn.commit()
50
- return new_balance
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:
@@ -75,74 +93,99 @@ else:
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")
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import json
3
+ import os
4
  import random
5
+ import hashlib
6
+ from datetime import datetime
7
 
8
+ # ---------------- FILE SETUP ----------------
9
+ USER_FILE = "users.json"
10
+ TRANS_FILE = "transactions.json"
11
 
12
+ if not os.path.exists(USER_FILE):
13
+ with open(USER_FILE, "w") as f:
14
+ json.dump({}, f)
 
 
 
 
 
 
 
15
 
16
+ if not os.path.exists(TRANS_FILE):
17
+ with open(TRANS_FILE, "w") as f:
18
+ json.dump({}, f)
19
 
20
+ # ---------------- PASSWORD HASHING ----------------
21
+ def hash_password(password):
22
+ return hashlib.sha256(password.encode()).hexdigest()
23
 
24
+ # ---------------- USER FUNCTIONS ----------------
25
+ def load_users():
26
+ with open(USER_FILE, "r") as f:
27
+ return json.load(f)
28
 
29
+ def save_users(users):
30
+ with open(USER_FILE, "w") as f:
31
+ json.dump(users, f)
32
 
33
+ def create_account(name, age, password):
34
+ users = load_users()
35
+ if name in users:
36
+ return None, "❌ Account already exists!"
37
+ acc_no = random.randint(1000, 9999)
38
+ users[name] = {
39
+ "age": age,
40
+ "password": hash_password(password),
41
+ "account_number": acc_no,
42
+ "balance": 0
43
+ }
44
+ save_users(users)
45
  return acc_no, "βœ… Account Created!"
46
 
47
  def login(name, acc_no, password):
48
+ users = load_users()
49
+ if name in users:
50
+ user = users[name]
51
+ if user["account_number"] == acc_no and user["password"] == hash_password(password):
52
+ return user
53
+ return None
54
+
55
+ def update_balance(name, new_balance):
56
+ users = load_users()
57
+ users[name]["balance"] = new_balance
58
+ save_users(users)
59
+
60
+ # ---------------- TRANSACTION FUNCTIONS ----------------
61
+ def record_transaction(name, type_, amount):
62
+ with open(TRANS_FILE, "r") as f:
63
+ transactions = json.load(f)
64
+ if name not in transactions:
65
+ transactions[name] = []
66
+ transactions[name].append({
67
+ "type": type_,
68
+ "amount": amount,
69
+ "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
70
+ })
71
+ with open(TRANS_FILE, "w") as f:
72
+ json.dump(transactions, f)
73
+
74
+ def get_transactions(name):
75
+ with open(TRANS_FILE, "r") as f:
76
+ transactions = json.load(f)
77
+ return transactions.get(name, [])
78
+
79
+ # ---------------- STREAMLIT UI ----------------
80
+ st.set_page_config(page_title="Baryashah Bank", layout="wide")
81
+ st.title("🏦 Baryashah Bank")
82
+
83
  if "logged_in" not in st.session_state:
84
  st.session_state.logged_in = False
85
+ if "user_name" not in st.session_state:
86
+ st.session_state.user_name = None
 
 
87
 
88
  # ---------------- SIDEBAR ----------------
89
  if not st.session_state.logged_in:
 
93
 
94
  # ---------------- CREATE ACCOUNT ----------------
95
  if menu == "Create Account":
96
+ st.subheader("πŸ†• Create New Account")
97
+ col1, col2 = st.columns([2,3])
98
+ with col1:
99
+ name = st.text_input("Name")
100
+ age = st.number_input("Age", min_value=1)
101
+ with col2:
102
+ password = st.text_input("Password", type="password")
103
+ if st.button("Create Account"):
104
+ acc_no, msg = create_account(name, age, password)
105
+ if acc_no:
106
+ st.success(f"{msg} πŸŽ‰")
107
+ st.info(f"Your Account Number: {acc_no}")
108
+ else:
109
+ st.error(msg)
110
 
111
  # ---------------- LOGIN ----------------
112
  elif menu == "Login":
113
+ st.subheader("πŸ”‘ Login")
114
+ col1, col2 = st.columns([2,3])
115
+ with col1:
116
+ name = st.text_input("Name")
117
+ acc_no = st.number_input("Account Number")
118
+ with col2:
119
+ password = st.text_input("Password", type="password")
120
+ if st.button("Login"):
121
+ user = login(name, acc_no, password)
122
+ if user:
123
+ st.session_state.logged_in = True
124
+ st.session_state.user_name = name
125
+ st.success("βœ… Login Successful")
126
+ else:
127
+ st.error("❌ Invalid Credentials")
128
 
129
  # ---------------- DASHBOARD ----------------
130
  elif menu == "Dashboard":
131
+ name = st.session_state.user_name
132
+ users = load_users()
133
+ user = users[name]
134
+
135
+ st.subheader(f"πŸ‘‹ Welcome, {name}")
136
+
137
+ # ---------------- BALANCE CARD ----------------
138
+ st.markdown(f"""
139
+ <div style="background-color:#d1f7c4;padding:20px;border-radius:15px">
140
+ <h3>πŸ’° Balance: {user['balance']}</h3>
141
+ <p>Account Number: {user['account_number']}</p>
142
+ </div>
143
+ """, unsafe_allow_html=True)
144
+
145
+ # ---------------- ACTIONS ----------------
146
+ col1, col2 = st.columns(2)
147
+ with col1:
148
+ option = st.selectbox("Choose Action", ["Check Balance", "Deposit", "Withdraw", "Transactions"])
149
+ with col2:
150
+ st.markdown("<br>", unsafe_allow_html=True)
151
+ if st.button("Logout"):
152
+ st.session_state.logged_in = False
153
+ st.session_state.user_name = None
154
+ st.success("πŸ‘‹ Logged out successfully")
155
+
156
+ # ---------------- CHECK BALANCE ----------------
157
  if option == "Check Balance":
158
+ st.success(f"πŸ’° Your Balance: {user['balance']}")
159
 
160
+ # ---------------- DEPOSIT ----------------
161
  elif option == "Deposit":
162
  amount = st.number_input("Enter Amount", min_value=1)
163
+ if st.button("Deposit Now"):
164
+ user["balance"] += amount
165
+ update_balance(name, user["balance"])
166
+ record_transaction(name, "Deposit", amount)
167
+ st.success(f"βœ… Deposited {amount}")
168
+ st.info(f"πŸ’° New Balance: {user['balance']}")
169
+
170
+ # ---------------- WITHDRAW ----------------
171
  elif option == "Withdraw":
172
  amount = st.number_input("Enter Amount", min_value=1)
173
+ if st.button("Withdraw Now"):
174
+ if user["balance"] < amount:
175
+ st.error("❌ Insufficient Balance")
 
 
176
  else:
177
+ user["balance"] -= amount
178
+ update_balance(name, user["balance"])
179
+ record_transaction(name, "Withdraw", amount)
180
+ st.success(f"βœ… Withdrawn {amount}")
181
+ st.info(f"πŸ’° Remaining Balance: {user['balance']}")
182
+
183
+ # ---------------- TRANSACTIONS ----------------
184
+ elif option == "Transactions":
185
+ txns = get_transactions(name)
186
+ if txns:
187
+ st.subheader("πŸ“œ Transaction History")
188
+ for t in reversed(txns):
189
+ st.info(f"{t['time']} β†’ {t['type']}: {t['amount']}")
190
+ else:
191
+ st.info("No transactions yet")