import mysql.connector def connect_to_db(): host="localhost", # Change if needed user="root", # Your MySQL username password="", # Your MySQL password database="BankSystem") # Registration Function def register(name, ph, password): if not (len(ph) == 10 and ph.isdigit()): print("Invalid phone number! Please enter a 10-digit number.") return if len(password) < 5 or len(password) > 18: print("Password must be between 5 and 18 characters.") return db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (ph,)) if cursor.fetchone(): print("Account with this phone number already exists.") else: cursor.execute("INSERT INTO clients (name, phone_number, password, balance) VALUES (%s, %s, %s, %s)", (name, ph, password, 100)) db.commit() print("Account created successfully.") cursor.close() db.close() # Login Function def login(name, ph, password): db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE name = %s AND phone_number = %s AND password = %s", (name, ph, password)) user = cursor.fetchone() cursor.close() db.close() if user: print(f"Welcome, {name}!") return user[0], user[4] # Return user ID and balance else: print("Invalid credentials.") return None, None # Add Cash Function def add_cash(user_id, amount): if amount <= 0: print("Please enter a valid amount.") return db = connect_to_db() cursor = db.cursor() cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, user_id)) db.commit() cursor.close() db.close() print("Amount added successfully.") # Check Balance Function def check_balance(user_id): db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,)) balance = cursor.fetchone()[0] cursor.close() db.close() print("Your current balance is:", balance) # Transfer Cash Function def transfer_cash(user_id, recipient_name, recipient_ph, amount): if amount <= 0: print("Invalid amount. Please enter a positive value to transfer.") return db = connect_to_db() cursor = db.cursor() # Verify recipient cursor.execute("SELECT id, balance FROM clients WHERE name = %s AND phone_number = %s", (recipient_name, recipient_ph)) recipient = cursor.fetchone() if not recipient: print("Recipient account not found.") cursor.close() db.close() return # Check sender balance cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,)) sender_balance = cursor.fetchone()[0] if amount > sender_balance: print("Insufficient balance.") else: # Perform transfer recipient_id = recipient[0] cursor.execute("UPDATE clients SET balance = balance - %s WHERE id = %s", (amount, user_id)) cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, recipient_id)) db.commit() print(f"Transferred {amount} to {recipient_name}.") cursor.close() db.close() # Change Password def change_password(user_id, new_password): if len(new_password) < 5 or len(new_password) > 18: print("Password must be between 5 and 18 characters.") return db = connect_to_db() cursor = db.cursor() cursor.execute("UPDATE clients SET password = %s WHERE id = %s", (new_password, user_id)) db.commit() cursor.close() db.close() print("Password updated successfully.") # Change Phone Number def change_phone(user_id, new_ph): if not (len(new_ph) == 10 and new_ph.isdigit()): print("Invalid phone number! Please enter a 10-digit number.") return db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (new_ph,)) if cursor.fetchone(): print("This phone number is already registered.") else: cursor.execute("UPDATE clients SET phone_number = %s WHERE id = %s", (new_ph, user_id)) db.commit() print("Phone number updated successfully.") cursor.close() db.close() # Logout Function def logout(): print("Logged out successfully.") # Main Program if __name__ == "__main__": # Setup Database (Create Table If Not Exists) db = connect_to_db() cursor = db.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS clients ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, phone_number VARCHAR(10) NOT NULL UNIQUE, password VARCHAR(18) NOT NULL, balance INT DEFAULT 100 ) """) db.commit() cursor.close() db.close() user_id = None while True: print("\nWelcome to My Bank") print("1. Login") print("2. Create a New Account") print("3. Exit") try: choice = int(input("Make a choice: ")) except ValueError: print("Invalid input. Please enter a number.") continue if choice == 2: name = input("Enter Name: ") ph = input("Enter Phone Number: ") password = input("Enter Password: ") register(name, ph, password) elif choice == 1: name = input("Enter Name: ") ph = input("Enter Phone Number: ") password = input("Enter Password: ") user_id, balance = login(name, ph, password) if user_id: while True: print("\n1. Add Amount") print("2. Check Balance") print("3. Transfer Amount") print("4. Edit Profile") print("5. Logout") try: sub_choice = int(input("Choose an option: ")) except ValueError: print("Invalid input. Please enter a number.") continue if sub_choice == 1: try: amount = int(input("Enter amount to add: ")) add_cash(user_id, amount) except ValueError: print("Please enter a valid amount.") elif sub_choice == 2: check_balance(user_id) elif sub_choice == 3: recipient_name = input("Enter recipient's name: ") recipient_ph = input("Enter recipient's phone number: ") try: amount = int(input("Enter amount to transfer: ")) transfer_cash(user_id, recipient_name, recipient_ph, amount) except ValueError: print("Please enter a valid amount.") elif sub_choice == 4: print("1. Change Password") print("2. Change Phone Number") try: edit_choice = int(input("Choose an option: ")) if edit_choice == 1: new_password = input("Enter new password: ") change_password(user_id, new_password) elif edit_choice == 2: new_ph = input("Enter new phone number: ") change_phone(user_id, new_ph) except ValueError: print("Invalid input. Please enter a number.") elif sub_choice == 5: logout() break elif choice == 3: print("Thank you for using My Bank! Goodbye.") break else: print("Invalid choice. Please try again.")
Standard input is empty
import mysql.connector def connect_to_db(): return mysql.connector.connect( host="localhost", # Change if needed user="root", # Your MySQL username password="", # Your MySQL password database="BankSystem") # Registration Function def register(name, ph, password): if not (len(ph) == 10 and ph.isdigit()): print("Invalid phone number! Please enter a 10-digit number.") return if len(password) < 5 or len(password) > 18: print("Password must be between 5 and 18 characters.") return db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (ph,)) if cursor.fetchone(): print("Account with this phone number already exists.") else: cursor.execute("INSERT INTO clients (name, phone_number, password, balance) VALUES (%s, %s, %s, %s)", (name, ph, password, 100)) db.commit() print("Account created successfully.") cursor.close() db.close() # Login Function def login(name, ph, password): db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE name = %s AND phone_number = %s AND password = %s", (name, ph, password)) user = cursor.fetchone() cursor.close() db.close() if user: print(f"Welcome, {name}!") return user[0], user[4] # Return user ID and balance else: print("Invalid credentials.") return None, None # Add Cash Function def add_cash(user_id, amount): if amount <= 0: print("Please enter a valid amount.") return db = connect_to_db() cursor = db.cursor() cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, user_id)) db.commit() cursor.close() db.close() print("Amount added successfully.") # Check Balance Function def check_balance(user_id): db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,)) balance = cursor.fetchone()[0] cursor.close() db.close() print("Your current balance is:", balance) # Transfer Cash Function def transfer_cash(user_id, recipient_name, recipient_ph, amount): if amount <= 0: print("Invalid amount. Please enter a positive value to transfer.") return db = connect_to_db() cursor = db.cursor() # Verify recipient cursor.execute("SELECT id, balance FROM clients WHERE name = %s AND phone_number = %s", (recipient_name, recipient_ph)) recipient = cursor.fetchone() if not recipient: print("Recipient account not found.") cursor.close() db.close() return # Check sender balance cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,)) sender_balance = cursor.fetchone()[0] if amount > sender_balance: print("Insufficient balance.") else: # Perform transfer recipient_id = recipient[0] cursor.execute("UPDATE clients SET balance = balance - %s WHERE id = %s", (amount, user_id)) cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, recipient_id)) db.commit() print(f"Transferred {amount} to {recipient_name}.") cursor.close() db.close() # Change Password def change_password(user_id, new_password): if len(new_password) < 5 or len(new_password) > 18: print("Password must be between 5 and 18 characters.") return db = connect_to_db() cursor = db.cursor() cursor.execute("UPDATE clients SET password = %s WHERE id = %s", (new_password, user_id)) db.commit() cursor.close() db.close() print("Password updated successfully.") # Change Phone Number def change_phone(user_id, new_ph): if not (len(new_ph) == 10 and new_ph.isdigit()): print("Invalid phone number! Please enter a 10-digit number.") return db = connect_to_db() cursor = db.cursor() cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (new_ph,)) if cursor.fetchone(): print("This phone number is already registered.") else: cursor.execute("UPDATE clients SET phone_number = %s WHERE id = %s", (new_ph, user_id)) db.commit() print("Phone number updated successfully.") cursor.close() db.close() # Logout Function def logout(): print("Logged out successfully.") # Main Program if __name__ == "__main__": # Setup Database (Create Table If Not Exists) db = connect_to_db() cursor = db.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS clients ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, phone_number VARCHAR(10) NOT NULL UNIQUE, password VARCHAR(18) NOT NULL, balance INT DEFAULT 100 ) """) db.commit() cursor.close() db.close() user_id = None while True: print("\nWelcome to My Bank") print("1. Login") print("2. Create a New Account") print("3. Exit") try: choice = int(input("Make a choice: ")) except ValueError: print("Invalid input. Please enter a number.") continue if choice == 2: name = input("Enter Name: ") ph = input("Enter Phone Number: ") password = input("Enter Password: ") register(name, ph, password) elif choice == 1: name = input("Enter Name: ") ph = input("Enter Phone Number: ") password = input("Enter Password: ") user_id, balance = login(name, ph, password) if user_id: while True: print("\n1. Add Amount") print("2. Check Balance") print("3. Transfer Amount") print("4. Edit Profile") print("5. Logout") try: sub_choice = int(input("Choose an option: ")) except ValueError: print("Invalid input. Please enter a number.") continue if sub_choice == 1: try: amount = int(input("Enter amount to add: ")) add_cash(user_id, amount) except ValueError: print("Please enter a valid amount.") elif sub_choice == 2: check_balance(user_id) elif sub_choice == 3: recipient_name = input("Enter recipient's name: ") recipient_ph = input("Enter recipient's phone number: ") try: amount = int(input("Enter amount to transfer: ")) transfer_cash(user_id, recipient_name, recipient_ph, amount) except ValueError: print("Please enter a valid amount.") elif sub_choice == 4: print("1. Change Password") print("2. Change Phone Number") try: edit_choice = int(input("Choose an option: ")) if edit_choice == 1: new_password = input("Enter new password: ") change_password(user_id, new_password) elif edit_choice == 2: new_ph = input("Enter new phone number: ") change_phone(user_id, new_ph) except ValueError: print("Invalid input. Please enter a number.") elif sub_choice == 5: logout() break elif choice == 3: print("Thank you for using My Bank! Goodbye.") break else: print("Invalid choice. Please try again.")