|
| 1 | +import os |
| 2 | +import json |
| 3 | +import random |
| 4 | +import tkinter as tk |
| 5 | +from tkinter import ttk, messagebox |
| 6 | +from PIL import Image, ImageTk |
| 7 | +import pytest |
| 8 | + |
| 9 | +USER_FILE = "users.json" |
| 10 | + |
| 11 | +# ---------------- USER MANAGEMENT ---------------- |
| 12 | +def load_users(): |
| 13 | + if not os.path.exists(USER_FILE): |
| 14 | + with open(USER_FILE, "w") as file: |
| 15 | + json.dump({"users": {}}, file, indent=4) |
| 16 | + with open(USER_FILE, "r") as file: |
| 17 | + return json.load(file) |
| 18 | + |
| 19 | +def save_users(data): |
| 20 | + with open(USER_FILE, "w") as file: |
| 21 | + json.dump(data, file, indent=4) |
| 22 | + |
| 23 | +def authenticate(user_id, password): |
| 24 | + users = load_users().get("users", {}) |
| 25 | + return users.get(user_id) if user_id in users and users[user_id]["password"] == password else None |
| 26 | + |
| 27 | +def signup(username, password, account_type): |
| 28 | + users_data = load_users() |
| 29 | + while True: |
| 30 | + user_id = str(random.randint(1000, 9999)) |
| 31 | + if user_id not in users_data["users"]: |
| 32 | + break |
| 33 | + account_number = str(random.randint(1000000000, 9999999999)) |
| 34 | + users_data["users"][user_id] = { |
| 35 | + "username": username, |
| 36 | + "password": password, |
| 37 | + "account_name": f"{username}'s Account", |
| 38 | + "account_number": account_number, |
| 39 | + "account_type": account_type, |
| 40 | + "balance": 0.0 |
| 41 | + } |
| 42 | + save_users(users_data) |
| 43 | + return user_id |
| 44 | + |
| 45 | +# ---------------- BANK ACCOUNT CLASS ---------------- |
| 46 | +class BankAccount: |
| 47 | + def __init__(self, user_id, user_data): |
| 48 | + self.user_id = user_id |
| 49 | + self.username = user_data["username"] |
| 50 | + self.account_name = user_data["account_name"] |
| 51 | + self.account_number = user_data["account_number"] |
| 52 | + self.account_type = user_data["account_type"] |
| 53 | + self.balance = user_data["balance"] |
| 54 | + |
| 55 | + def deposit(self, amount): |
| 56 | + if amount <= 0: |
| 57 | + raise ValueError("Deposit amount must be positive") |
| 58 | + self.balance += amount |
| 59 | + self.update_balance() |
| 60 | + return self.balance |
| 61 | + |
| 62 | + def withdraw(self, amount): |
| 63 | + if amount <= 0: |
| 64 | + raise ValueError("Withdrawal amount must be positive") |
| 65 | + if amount > self.balance: |
| 66 | + raise ValueError("Insufficient balance") |
| 67 | + self.balance -= amount |
| 68 | + self.update_balance() |
| 69 | + return self.balance |
| 70 | + |
| 71 | + def update_balance(self): |
| 72 | + data = load_users() |
| 73 | + if self.user_id in data["users"]: |
| 74 | + data["users"][self.user_id]["balance"] = self.balance |
| 75 | + save_users(data) |
| 76 | + |
| 77 | + def get_balance(self): |
| 78 | + return self.balance |
| 79 | + |
| 80 | +# ---------------- GUI APPLICATION ---------------- |
| 81 | +class BankingApp: |
| 82 | + def __init__(self, root): |
| 83 | + self.root = root |
| 84 | + self.root.title("banking_app") |
| 85 | + self.root.geometry("600x500") |
| 86 | + self.current_user = None |
| 87 | + self.bank_account = None |
| 88 | + self.load_background() |
| 89 | + self.login_screen() |
| 90 | + |
| 91 | + def load_background(self): |
| 92 | + self.bg_image = Image.open("1.jpg").resize((600, 500)) |
| 93 | + self.bg_photo = ImageTk.PhotoImage(self.bg_image) |
| 94 | + self.canvas = tk.Canvas(self.root, width=600, height=500) |
| 95 | + self.canvas.pack(fill="both", expand=True) |
| 96 | + self.canvas.create_image(0, 0, image=self.bg_photo, anchor="nw") |
| 97 | + |
| 98 | + def clear_screen(self): |
| 99 | + for widget in self.root.winfo_children(): |
| 100 | + widget.destroy() |
| 101 | + self.load_background() |
| 102 | + |
| 103 | + def login_screen(self): |
| 104 | + self.clear_screen() |
| 105 | + frame = tk.Frame(self.root) |
| 106 | + frame.place(relx=0.5, rely=0.5, anchor="center") |
| 107 | + ttk.Label(frame, text="User ID:").grid(row=0, column=0) |
| 108 | + self.user_id_entry = ttk.Entry(frame) |
| 109 | + self.user_id_entry.grid(row=0, column=1) |
| 110 | + ttk.Label(frame, text="Password:").grid(row=1, column=0) |
| 111 | + self.password_entry = ttk.Entry(frame, show="*") |
| 112 | + self.password_entry.grid(row=1, column=1) |
| 113 | + ttk.Button(frame, text="Login", command=self.authenticate_user).grid(row=2, column=1) |
| 114 | + ttk.Button(frame, text="Sign Up", command=self.signup_screen).grid(row=3, column=1) |
| 115 | + |
| 116 | + def authenticate_user(self): |
| 117 | + user_id = self.user_id_entry.get() |
| 118 | + password = self.password_entry.get() |
| 119 | + user_data = authenticate(user_id, password) |
| 120 | + if user_data: |
| 121 | + self.current_user = user_data["username"] |
| 122 | + self.bank_account = BankAccount(user_id, user_data) |
| 123 | + messagebox.showinfo("Login Successful", f"Welcome, {self.current_user}!") |
| 124 | + else: |
| 125 | + messagebox.showerror("Login Failed", "Invalid User ID or Password") |
| 126 | + |
| 127 | + def signup_screen(self): |
| 128 | + self.clear_screen() |
| 129 | + frame = tk.Frame(self.root) |
| 130 | + frame.place(relx=0.5, rely=0.5, anchor="center") |
| 131 | + ttk.Label(frame, text="Username:").grid(row=0, column=0) |
| 132 | + self.signup_username = ttk.Entry(frame) |
| 133 | + self.signup_username.grid(row=0, column=1) |
| 134 | + ttk.Label(frame, text="Password:").grid(row=1, column=0) |
| 135 | + self.signup_password = ttk.Entry(frame, show="*") |
| 136 | + self.signup_password.grid(row=1, column=1) |
| 137 | + ttk.Label(frame, text="Account Type:").grid(row=2, column=0) |
| 138 | + self.account_type = ttk.Combobox(frame, values=["Savings", "Checking"]) |
| 139 | + self.account_type.grid(row=2, column=1) |
| 140 | + self.account_type.current(0) |
| 141 | + ttk.Button(frame, text="Register", command=self.register_user).grid(row=3, column=1) |
| 142 | + |
| 143 | + def register_user(self): |
| 144 | + username = self.signup_username.get() |
| 145 | + password = self.signup_password.get() |
| 146 | + account_type = self.account_type.get() |
| 147 | + if username and password: |
| 148 | + user_id = signup(username, password, account_type) |
| 149 | + messagebox.showinfo("Success", f"Account created! Your User ID is: {user_id}") |
| 150 | + self.login_screen() |
| 151 | + else: |
| 152 | + messagebox.showerror("Error", "Please fill all fields") |
| 153 | + |
| 154 | +# ---------------- MAIN ---------------- |
| 155 | +if __name__ == "__main__": |
| 156 | + root = tk.Tk() |
| 157 | + app = BankingApp(root) |
| 158 | + root.mainloop() |
0 commit comments