Advertisement
DrAungWinHtut

optimizePythonBank.py

Jun 11th, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. from myconnection import connect_to_mysql
  2. from mysql.connector import Error
  3.  
  4. config = {
  5.     "host": "127.0.0.1",
  6.     "user": "root",
  7.     "password": "",
  8.     "database": "bankdb",
  9. }
  10.  
  11. def execute_query(connection, query, values=None):
  12.     try:
  13.         with connection.cursor() as cursor:
  14.             cursor.execute(query, values)
  15.             connection.commit()
  16.             print("Query executed successfully")
  17.     except Error as e:
  18.         print(f"Error: {e}")
  19.  
  20. def fetch_query(connection, query):
  21.     try:
  22.         with connection.cursor() as cursor:
  23.             cursor.execute(query)
  24.             return cursor.fetchall()
  25.     except Error as e:
  26.         print(f"Error: {e}")
  27.  
  28. def insert_account(connection, account_no, password, name, amount, phone):
  29.     query = "INSERT INTO accounts(account_no, password, name, amount, phone) VALUES (%s, %s, %s, %s, %s)"
  30.     values = (account_no, password, name, amount, phone)
  31.     execute_query(connection, query, values)
  32.  
  33. def signup(connection):
  34.     account_no = int(input('Enter account no: '))
  35.     password = input('Enter Password: ')
  36.     confirm_password = input('Enter Confirm Password: ')
  37.     if len(password) < 8 or password != confirm_password:
  38.         print('Password must be at least 8 characters and match the confirmation.')
  39.         return
  40.     name = input('Enter your name: ')
  41.     amount = int(input('Enter Initial Deposit amount: '))
  42.     phone = input('Enter your phone no: ')
  43.     insert_account(connection, account_no, password, name, amount, phone)
  44.  
  45. def deposit_money(connection, account_no, deposit_amount):
  46.     query = 'UPDATE accounts SET amount = amount + %s WHERE account_no = %s'
  47.     execute_query(connection, query, (deposit_amount, account_no))
  48.  
  49. def withdraw_money(connection, account_no, withdraw_amount):
  50.     query = 'UPDATE accounts SET amount = amount - %s WHERE account_no = %s AND amount >= %s'
  51.     values = (withdraw_amount, account_no, withdraw_amount)
  52.     execute_query(connection, query, values)
  53.  
  54. def show_user_accounts(connection, account_no):
  55.     query = 'SELECT * FROM accounts WHERE account_no = %s'
  56.     data = fetch_query(connection, query, (account_no,))
  57.     for row in data:
  58.         print(row)
  59.  
  60. def signin(connection):
  61.     account_no = int(input('Enter account no: '))
  62.     password = input('Enter Password: ')
  63.     query = 'SELECT * FROM accounts WHERE account_no = %s AND password = %s'
  64.     data = fetch_query(connection, query, (account_no, password))
  65.     if data:
  66.         print('Successfully Signin!')
  67.         return True, account_no
  68.     else:
  69.         print('Wrong username or password.')
  70.         return False, None
  71.  
  72. def main():
  73.     connection = connect_to_mysql(config, attempts=3)
  74.     if not connection or not connection.is_connected():
  75.         print('Connection error!, Bye')
  76.         return
  77.  
  78.     while True:
  79.         sign_in_status, account_no = signin(connection)
  80.         if sign_in_status:
  81.             break
  82.         ans = input('Try again? (y/n): ')
  83.         if ans.lower() != 'y':
  84.             print('Bye!')
  85.             return
  86.  
  87.     while True:
  88.         print('0-Exit')
  89.         print('1-Create Account')
  90.         print('2-Deposit Money')
  91.         print('3-Withdraw Money')
  92.         print('4-Show Amount')
  93.         ans = input('Enter your choice(0,1,2,3,4): ')
  94.  
  95.         if ans == '0':
  96.             print('Bye')
  97.             connection.close()
  98.             return
  99.         elif ans == '1':
  100.             print('Welcome:')
  101.             signup(connection)
  102.         elif ans == '2':
  103.             deposit_amount = int(input('Enter amount to deposit: '))
  104.             deposit_money(connection, account_no, deposit_amount)
  105.         elif ans == '3':
  106.             withdraw_amount = int(input('Enter amount to withdraw: '))
  107.             withdraw_money(connection, account_no, withdraw_amount)
  108.         elif ans == '4':
  109.             show_user_accounts(connection, account_no)
  110.  
  111. if __name__ == "__main__":
  112.     main()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement