fork download
  1. import mysql.connector
  2.  
  3. def connect_to_db():
  4. return mysql.connector.connect(
  5. host="localhost", # Change if needed
  6. user="root", # Your MySQL username
  7. password="", # Your MySQL password
  8. database="BankSystem")
  9.  
  10.  
  11. # Registration Function
  12. def register(name, ph, password):
  13. if not (len(ph) == 10 and ph.isdigit()):
  14. print("Invalid phone number! Please enter a 10-digit number.")
  15. return
  16. if len(password) < 5 or len(password) > 18:
  17. print("Password must be between 5 and 18 characters.")
  18. return
  19.  
  20. db = connect_to_db()
  21. cursor = db.cursor()
  22. cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (ph,))
  23. if cursor.fetchone():
  24. print("Account with this phone number already exists.")
  25. else:
  26. cursor.execute("INSERT INTO clients (name, phone_number, password, balance) VALUES (%s, %s, %s, %s)",
  27. (name, ph, password, 100))
  28. db.commit()
  29. print("Account created successfully.")
  30. cursor.close()
  31. db.close()
  32.  
  33.  
  34. # Login Function
  35. def login(name, ph, password):
  36. db = connect_to_db()
  37. cursor = db.cursor()
  38. cursor.execute("SELECT * FROM clients WHERE name = %s AND phone_number = %s AND password = %s",
  39. (name, ph, password))
  40. user = cursor.fetchone()
  41. cursor.close()
  42. db.close()
  43. if user:
  44. print(f"Welcome, {name}!")
  45. return user[0], user[4] # Return user ID and balance
  46. else:
  47. print("Invalid credentials.")
  48. return None, None
  49.  
  50.  
  51. # Add Cash Function
  52. def add_cash(user_id, amount):
  53. if amount <= 0:
  54. print("Please enter a valid amount.")
  55. return
  56.  
  57. db = connect_to_db()
  58. cursor = db.cursor()
  59. cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, user_id))
  60. db.commit()
  61. cursor.close()
  62. db.close()
  63. print("Amount added successfully.")
  64.  
  65.  
  66. # Check Balance Function
  67. def check_balance(user_id):
  68. db = connect_to_db()
  69. cursor = db.cursor()
  70. cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,))
  71. balance = cursor.fetchone()[0]
  72. cursor.close()
  73. db.close()
  74. print("Your current balance is:", balance)
  75.  
  76.  
  77. # Transfer Cash Function
  78. def transfer_cash(user_id, recipient_name, recipient_ph, amount):
  79. if amount <= 0:
  80. print("Invalid amount. Please enter a positive value to transfer.")
  81. return
  82.  
  83. db = connect_to_db()
  84. cursor = db.cursor()
  85.  
  86. # Verify recipient
  87. cursor.execute("SELECT id, balance FROM clients WHERE name = %s AND phone_number = %s",
  88. (recipient_name, recipient_ph))
  89. recipient = cursor.fetchone()
  90. if not recipient:
  91. print("Recipient account not found.")
  92. cursor.close()
  93. db.close()
  94. return
  95.  
  96. # Check sender balance
  97. cursor.execute("SELECT balance FROM clients WHERE id = %s", (user_id,))
  98. sender_balance = cursor.fetchone()[0]
  99.  
  100. if amount > sender_balance:
  101. print("Insufficient balance.")
  102. else:
  103. # Perform transfer
  104. recipient_id = recipient[0]
  105. cursor.execute("UPDATE clients SET balance = balance - %s WHERE id = %s", (amount, user_id))
  106. cursor.execute("UPDATE clients SET balance = balance + %s WHERE id = %s", (amount, recipient_id))
  107. db.commit()
  108. print(f"Transferred {amount} to {recipient_name}.")
  109. cursor.close()
  110. db.close()
  111.  
  112.  
  113. # Change Password
  114. def change_password(user_id, new_password):
  115. if len(new_password) < 5 or len(new_password) > 18:
  116. print("Password must be between 5 and 18 characters.")
  117. return
  118. db = connect_to_db()
  119. cursor = db.cursor()
  120. cursor.execute("UPDATE clients SET password = %s WHERE id = %s", (new_password, user_id))
  121. db.commit()
  122. cursor.close()
  123. db.close()
  124. print("Password updated successfully.")
  125.  
  126.  
  127. # Change Phone Number
  128. def change_phone(user_id, new_ph):
  129. if not (len(new_ph) == 10 and new_ph.isdigit()):
  130. print("Invalid phone number! Please enter a 10-digit number.")
  131. return
  132. db = connect_to_db()
  133. cursor = db.cursor()
  134. cursor.execute("SELECT * FROM clients WHERE phone_number = %s", (new_ph,))
  135. if cursor.fetchone():
  136. print("This phone number is already registered.")
  137. else:
  138. cursor.execute("UPDATE clients SET phone_number = %s WHERE id = %s", (new_ph, user_id))
  139. db.commit()
  140. print("Phone number updated successfully.")
  141. cursor.close()
  142. db.close()
  143.  
  144.  
  145. # Logout Function
  146. def logout():
  147. print("Logged out successfully.")
  148.  
  149.  
  150. # Main Program
  151. if __name__ == "__main__":
  152. # Setup Database (Create Table If Not Exists)
  153. db = connect_to_db()
  154. cursor = db.cursor()
  155. cursor.execute("""
  156. CREATE TABLE IF NOT EXISTS clients (
  157. id INT AUTO_INCREMENT PRIMARY KEY,
  158. name VARCHAR(255) NOT NULL,
  159. phone_number VARCHAR(10) NOT NULL UNIQUE,
  160. password VARCHAR(18) NOT NULL,
  161. balance INT DEFAULT 100
  162. )
  163. """)
  164. db.commit()
  165. cursor.close()
  166. db.close()
  167.  
  168. user_id = None
  169. while True:
  170. print("\nWelcome to My Bank")
  171. print("1. Login")
  172. print("2. Create a New Account")
  173. print("3. Exit")
  174.  
  175. try:
  176. choice = int(input("Make a choice: "))
  177. except ValueError:
  178. print("Invalid input. Please enter a number.")
  179. continue
  180.  
  181. if choice == 2:
  182. name = input("Enter Name: ")
  183. ph = input("Enter Phone Number: ")
  184. password = input("Enter Password: ")
  185. register(name, ph, password)
  186.  
  187. elif choice == 1:
  188. name = input("Enter Name: ")
  189. ph = input("Enter Phone Number: ")
  190. password = input("Enter Password: ")
  191. user_id, balance = login(name, ph, password)
  192.  
  193. if user_id:
  194. while True:
  195. print("\n1. Add Amount")
  196. print("2. Check Balance")
  197. print("3. Transfer Amount")
  198. print("4. Edit Profile")
  199. print("5. Logout")
  200.  
  201. try:
  202. sub_choice = int(input("Choose an option: "))
  203. except ValueError:
  204. print("Invalid input. Please enter a number.")
  205. continue
  206.  
  207. if sub_choice == 1:
  208. try:
  209. amount = int(input("Enter amount to add: "))
  210. add_cash(user_id, amount)
  211. except ValueError:
  212. print("Please enter a valid amount.")
  213.  
  214. elif sub_choice == 2:
  215. check_balance(user_id)
  216.  
  217. elif sub_choice == 3:
  218. recipient_name = input("Enter recipient's name: ")
  219. recipient_ph = input("Enter recipient's phone number: ")
  220. try:
  221. amount = int(input("Enter amount to transfer: "))
  222. transfer_cash(user_id, recipient_name, recipient_ph, amount)
  223. except ValueError:
  224. print("Please enter a valid amount.")
  225.  
  226. elif sub_choice == 4:
  227. print("1. Change Password")
  228. print("2. Change Phone Number")
  229. try:
  230. edit_choice = int(input("Choose an option: "))
  231. if edit_choice == 1:
  232. new_password = input("Enter new password: ")
  233. change_password(user_id, new_password)
  234. elif edit_choice == 2:
  235. new_ph = input("Enter new phone number: ")
  236. change_phone(user_id, new_ph)
  237. except ValueError:
  238. print("Invalid input. Please enter a number.")
  239.  
  240. elif sub_choice == 5:
  241. logout()
  242. break
  243.  
  244. elif choice == 3:
  245. print("Thank you for using My Bank! Goodbye.")
  246. break
  247.  
  248. else:
  249. print("Invalid choice. Please try again.")
Success #stdin #stdout 0.02s 25708KB
stdin
Standard input is empty
stdout
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.")