Advertisement
DrAungWinHtut

banksystem.py

Jun 5th, 2024
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. from myconnection import connect_to_mysql  # pip install mysql-connector-python, myconnection.py must exist
  2. from icecream import ic  # pip install icecream
  3. from mysql.connector import Error
  4.  
  5. config = {
  6.     "host": "127.0.0.1",
  7.     "user": "root",
  8.     "password": "",
  9.     "database": "bankdb",
  10. }
  11.  
  12. user_signIn = False
  13. admin_signIn = False
  14. acc = 0
  15.  
  16. def execute_query(connection, query, values=None):
  17.     cursor = connection.cursor()
  18.     try:
  19.         cursor.execute(query, values)
  20.         connection.commit()
  21.         print("Query executed successfully")
  22.     except Error as e:
  23.         print(f"The error '{e}' occurred")
  24.     finally:
  25.         cursor.close()
  26.  
  27. def fetch_query(connection, query, values=None):
  28.     cursor = connection.cursor()
  29.     try:
  30.         cursor.execute(query, values)
  31.         return cursor.fetchall()
  32.     except Error as e:
  33.         print(f"The error '{e}' occurred")
  34.     finally:
  35.         cursor.close()
  36.  
  37. def insert_data(connection, accountno, password, name, amount, phone):
  38.     query = "INSERT INTO accounts (account_no, password, name, amount, phone) VALUES (%s, %s, %s, %s, %s)"
  39.     values = (accountno, password, name, amount, phone)
  40.     execute_query(connection, query, values)
  41.  
  42. def signup(connection):
  43.     accountno = int(input('Enter account no: '))
  44.     while True:
  45.         password = input('Enter Password: ')
  46.         confirm_password = input('Enter Confirm Password: ')
  47.         if len(password) < 8:
  48.             print('Password must be at least 8 characters long')
  49.         elif password != confirm_password:
  50.             print('Passwords did not match, please try again!')
  51.         else:
  52.             break
  53.  
  54.     name = input('Enter your name: ')
  55.     amount = int(input('Enter Initial Deposit amount: '))
  56.     phone = input('Enter your phone no: ')
  57.     insert_data(connection, accountno, password, name, amount, phone)
  58.  
  59. def delete_account(connection, accountno):
  60.     query = "DELETE FROM accounts WHERE account_no = %s"
  61.     execute_query(connection, query, (accountno,))
  62.  
  63. def update_amount(connection, accountno, amount):
  64.     query = "UPDATE accounts SET amount = %s WHERE account_no = %s"
  65.     execute_query(connection, query, (amount, accountno))
  66.  
  67. def deposit_money(connection, account_no, deposit_amount):
  68.     query = "SELECT amount FROM accounts WHERE account_no = %s"
  69.     data = fetch_query(connection, query, (account_no,))
  70.     if data:
  71.         amount = data[0][0] + deposit_amount
  72.         update_amount(connection, account_no, amount)
  73.         print(f'Your new balance is: {amount}')
  74.  
  75. def withdraw_money(connection, account_no, withdraw_amount):
  76.     query = "SELECT amount FROM accounts WHERE account_no = %s"
  77.     data = fetch_query(connection, query, (account_no,))
  78.     if data:
  79.         amount = data[0][0]
  80.         if withdraw_amount > amount:
  81.             print('Insufficient funds!')
  82.             return
  83.         amount -= withdraw_amount
  84.         update_amount(connection, account_no, amount)
  85.         print(f'Your new balance is: {amount}')
  86.  
  87. def show_all_accounts(connection):
  88.     query = "SELECT * FROM accounts"
  89.     data = fetch_query(connection, query)
  90.     print('\n\nUser Accounts\n-------------')
  91.     for d in data:
  92.         print(f'id = {d[0]}, account_no = {d[1]}, name = {d[2]}, amount = {d[3]}, phone = {d[4]}')
  93.  
  94. def check_account(connection, account_no, password):
  95.     query = "SELECT * FROM accounts WHERE account_no = %s AND password = %s"
  96.     data = fetch_query(connection, query, (account_no, password))
  97.     if data:
  98.         check_admin(connection, account_no)
  99.         return True
  100.     return False
  101.  
  102. def check_admin(connection, account_no):
  103.     global admin_signIn, user_signIn
  104.     query = "SELECT id FROM accounts WHERE account_no = %s"
  105.     data = fetch_query(connection, query, (account_no,))
  106.     if data:
  107.         account_id = data[0][0]
  108.         query = "SELECT * FROM admin WHERE accounts_id = %s"
  109.         data = fetch_query(connection, query, (account_id,))
  110.         if data:
  111.             admin_signIn = True
  112.             user_signIn = False
  113.             print('Admin Login')
  114.         else:
  115.             admin_signIn = False
  116.             user_signIn = True
  117.             print('User Login')
  118.  
  119. def show_user_accounts(connection, account_no):
  120.     query = "SELECT * FROM accounts WHERE account_no = %s"
  121.     data = fetch_query(connection, query, (account_no,))
  122.     for d in data:
  123.         print(d)
  124.  
  125. def sign_in(connection):
  126.     global acc
  127.     acc = int(input('Enter account no: '))
  128.     password = input('Enter Password: ')
  129.     if check_account(connection, acc, password):
  130.         print('Successfully Signed in!')
  131.         return True
  132.     print('Wrong username or password.')
  133.     return False
  134.  
  135. # Program start
  136. cnx = connect_to_mysql(config, attempts=3)
  137. if not cnx or not cnx.is_connected():
  138.     print('Connection error!, Bye')
  139.     exit(1)
  140.  
  141. while True:
  142.     if sign_in(cnx):
  143.         break
  144.     if input('Try again? (y/n): ').lower() != 'y':
  145.         print('Bye!')
  146.         exit(0)
  147.  
  148. while True:
  149.     print('0-Exit')
  150.     print('1-Create Account')
  151.     print('2-Deposit Money')
  152.     print('3-Withdraw Money')
  153.     print('4-Show Amount')
  154.     if admin_signIn:
  155.         print('5-Show All Users (for admin only)')
  156.         print('6-Delete Account (for admin only)')
  157.         choice = int(input('Enter your choice (0,1,2,3,4,5,6): '))
  158.     else:
  159.         choice = int(input('Enter your choice (0,1,2,3,4): '))
  160.  
  161.     if choice == 0:
  162.         print('Bye')
  163.         cnx.close()
  164.         exit(0)
  165.     elif choice == 1:
  166.         if admin_signIn:
  167.             signup(cnx)
  168.         else:
  169.             print('You don’t have privileges to create an account!')
  170.     elif choice == 2:
  171.         deposit_amount = int(input('Please enter amount to deposit: '))
  172.         deposit_money(cnx, acc, deposit_amount)
  173.     elif choice == 3:
  174.         withdraw_amount = int(input('Please enter amount to withdraw: '))
  175.         withdraw_money(cnx, acc, withdraw_amount)
  176.     elif choice == 4:
  177.         show_user_accounts(cnx, acc)
  178.     elif choice == 5 and admin_signIn:
  179.         show_all_accounts(cnx)
  180.     elif choice == 6 and admin_signIn:
  181.         acc_to_delete = int(input('Enter account no. to delete: '))
  182.         delete_account(cnx, acc_to_delete)
  183.     else:
  184.         print('Invalid choice or insufficient privileges')
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement