-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
314 lines (246 loc) · 9.44 KB
/
main.py
File metadata and controls
314 lines (246 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# 🏦 Banking App (SQL Version with CSV Audit + Test Accounts)
# Author: Alen Chavez
# Description:
# A console-based banking application using SQLite as the primary datastore
# and CSV as a secondary audit trail. Includes 3 auto-loaded test accounts
# for easier testing.
import sqlite3
import hashlib
import csv
import os
from datetime import datetime
DB_FILE = "bank.db"
CSV_LOG = "transactions.csv"
# -------------------- Database Setup --------------------
def init_db():
"""Initializes the SQLite database and creates tables if they don't exist."""
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
pin_hash TEXT NOT NULL,
name TEXT NOT NULL
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS accounts (
user_id INTEGER PRIMARY KEY,
checking REAL DEFAULT 0,
savings REAL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
account_type TEXT NOT NULL,
old_balance REAL NOT NULL,
amount REAL NOT NULL,
new_balance REAL NOT NULL,
timestamp TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
)
""")
# ---- Add sample test accounts if database is empty ----
cur.execute("SELECT COUNT(*) FROM users")
user_count = cur.fetchone()[0]
if user_count == 0:
print("🧪 Adding 3 sample test accounts...")
sample_users = [
("alice", hashlib.sha256("1111".encode()).hexdigest(), "Alice Smith", 1500.00, 500.00),
("bob", hashlib.sha256("2222".encode()).hexdigest(), "Bob Johnson", 2500.00, 1000.00),
("carla", hashlib.sha256("3333".encode()).hexdigest(), "Carla Gomez", 300.00, 700.00),
]
for username, pin_hash, name, checking, savings in sample_users:
cur.execute("INSERT INTO users (username, pin_hash, name) VALUES (?, ?, ?)", (username, pin_hash, name))
user_id = cur.lastrowid
cur.execute("INSERT INTO accounts (user_id, checking, savings) VALUES (?, ?, ?)",
(user_id, checking, savings))
print("✅ Sample accounts created: alice (1111), bob (2222), carla (3333)")
conn.commit()
conn.close()
# -------------------- Helper Functions --------------------
def hash_pin(pin: str) -> str:
return hashlib.sha256(pin.encode()).hexdigest()
def get_user(username):
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("SELECT id, username, pin_hash, name FROM users WHERE username = ?", (username,))
row = cur.fetchone()
conn.close()
return row
def get_user_by_id(user_id):
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("SELECT id, username, pin_hash, name FROM users WHERE id = ?", (user_id,))
row = cur.fetchone()
conn.close()
return row
def create_user(username, name, pin_hash, checking, savings):
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("INSERT INTO users (username, pin_hash, name) VALUES (?, ?, ?)", (username, pin_hash, name))
user_id = cur.lastrowid
cur.execute("INSERT INTO accounts (user_id, checking, savings) VALUES (?, ?, ?)",
(user_id, checking, savings))
conn.commit()
conn.close()
return user_id
def get_balance(user_id, account_type):
column = "checking" if account_type == "C" else "savings"
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute(f"SELECT {column} FROM accounts WHERE user_id = ?", (user_id,))
balance = cur.fetchone()[0]
conn.close()
return balance
def update_balance(user_id, account_type, new_balance):
column = "checking" if account_type == "C" else "savings"
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute(f"UPDATE accounts SET {column} = ? WHERE user_id = ?", (new_balance, user_id))
conn.commit()
conn.close()
def ensure_csv_header():
if not os.path.exists(CSV_LOG):
with open(CSV_LOG, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["timestamp", "username", "account_type", "old_balance", "amount", "new_balance"])
def log_to_csv(username, account_type, old_balance, amount, new_balance):
ensure_csv_header()
with open(CSV_LOG, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([datetime.now().isoformat(timespec='seconds'),
username, account_type,
f"{old_balance:.2f}", f"{amount:.2f}", f"{new_balance:.2f}"])
def record_transaction(user_id, account_type, old_balance, amount, new_balance):
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("""
INSERT INTO transactions (user_id, account_type, old_balance, amount, new_balance, timestamp)
VALUES (?, ?, ?, ?, ?, ?)
""", (user_id, account_type, old_balance, amount, new_balance, datetime.now().isoformat(timespec='seconds')))
conn.commit()
conn.close()
user = get_user_by_id(user_id)
if user:
log_to_csv(user[1], account_type, old_balance, amount, new_balance)
# -------------------- Core Features --------------------
def create_account():
username = input("Enter new username: ").strip()
if not username:
print("Username cannot be empty.")
return
if get_user(username):
print("Username already exists.")
return
pin_input = input("Enter a 4-digit PIN: ").strip()
if not (pin_input.isdigit() and len(pin_input) == 4):
print("Invalid PIN format.")
return
name = input("Enter your name: ").strip()
try:
checking = float(input("Initial deposit for Checking: "))
except ValueError:
checking = 0.0
try:
savings = float(input("Initial deposit for Savings: "))
except ValueError:
savings = 0.0
create_user(username, name, hash_pin(pin_input), checking, savings)
print("✅ Account created successfully.")
def delete_account():
username = input("Enter username to delete: ").strip()
user = get_user(username)
if not user:
print("User not found.")
return
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("DELETE FROM transactions WHERE user_id = ?", (user[0],))
cur.execute("DELETE FROM accounts WHERE user_id = ?", (user[0],))
cur.execute("DELETE FROM users WHERE id = ?", (user[0],))
conn.commit()
conn.close()
print("🗑 Account deleted successfully.")
def make_transaction():
username = input("Enter your username: ").strip()
user = get_user(username)
if not user:
print("No such user.")
return
pin = input("Enter your 4-digit PIN: ").strip()
if hash_pin(pin) != user[2]:
print("Invalid PIN.")
return
account_type = input("Enter C for Checking or S for Savings: ").strip().upper()
if account_type not in ["C", "S"]:
print("Invalid account type.")
return
old_balance = get_balance(user[0], account_type)
print(f"Current balance: ${old_balance:,.2f}")
try:
amount = float(input("Enter transaction amount (negative for withdrawal): "))
except ValueError:
print("Invalid amount.")
return
if old_balance + amount < 0:
print("❌ Insufficient funds.")
return
new_balance = old_balance + amount
update_balance(user[0], account_type, new_balance)
record_transaction(user[0], account_type, old_balance, amount, new_balance)
print(f"✅ Transaction complete. New balance: ${new_balance:,.2f}")
def view_statistics():
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
cur.execute("SELECT AVG(checking), AVG(savings) FROM accounts")
avg_checking, avg_savings = cur.fetchone()
print(f"\n📊 Average Checking: ${avg_checking or 0:,.2f}")
print(f"📊 Average Savings: ${avg_savings or 0:,.2f}")
cur.execute("""
SELECT u.username FROM users u
JOIN accounts a ON u.id = a.user_id
WHERE a.checking > ?
""", (avg_checking,))
above_checking = [row[0] for row in cur.fetchall()]
cur.execute("""
SELECT u.username FROM users u
JOIN accounts a ON u.id = a.user_id
WHERE a.savings > ?
""", (avg_savings,))
above_savings = [row[0] for row in cur.fetchall()]
print("\nUsers above average (Checking):", ", ".join(above_checking) or "None")
print("Users above average (Savings):", ", ".join(above_savings) or "None")
conn.close()
# -------------------- Main Menu --------------------
def main_menu():
print("\n===== Cactus Bank (SQL Edition) =====")
print("[1] Create Account")
print("[2] Delete Account")
print("[3] Make Transaction")
print("[4] View Statistics")
print("[5] Exit")
return input("Enter your choice: ").strip()
def main():
init_db()
while True:
choice = main_menu()
if choice == '1':
create_account()
elif choice == '2':
delete_account()
elif choice == '3':
make_transaction()
elif choice == '4':
view_statistics()
elif choice == '5':
print("👋 Goodbye!")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()