Updated Atm Code
Updated Atm Code
connector
import sys
# User authentication
def authenticate_user():
while True:
username = input('\nENTER USER NAME: ').lower()
cursor.execute("SELECT id, pin, balance FROM accounts WHERE username = %s",
(username,))
result = cursor.fetchone()
if result:
user_id, pin_db, balance = result
return user_id, username, pin_db, balance
else:
print('INVALID USERNAME')
# Verify PIN
def verify_pin(user_id, pin_db):
attempts = 0
while attempts < 3:
pin = input('PLEASE ENTER PIN: ')
if pin == pin_db:
return True
else:
attempts += 1
print('INVALID PIN')
print('3 UNSUCCESSFUL ATTEMPTS, EXITING. CARD LOCKED.')
sys.exit()
# Main menu
def atm_menu(user_id, username, balance):
while True:
response = input('\nSELECT FROM FOLLOWING OPTIONS: \nStatement__(S) \
nWithdraw___(W) \nLodgement__(L) \nChange PIN_(P) \nQuit_______(Q) \n: ').lower()
if response == 's':
print(f"{username.capitalize()}, YOU HAVE {balance} RUPEES IN YOUR
ACCOUNT.")
elif response == 'w':
amount = int(input('ENTER AMOUNT TO WITHDRAW: '))
if amount % 10 != 0:
print('AMOUNT MUST MATCH 10 RUPEES NOTES.')
elif amount > balance:
print('INSUFFICIENT BALANCE.')
else:
balance -= amount
cursor.execute("UPDATE accounts SET balance = %s WHERE id = %s",
(balance, user_id))
conn.commit()
print(f'WITHDRAWAL SUCCESSFUL. NEW BALANCE: {balance} RUPEES.')
elif response == 'l':
amount = int(input('ENTER AMOUNT TO LODGE: '))
if amount % 10 != 0:
print('AMOUNT MUST MATCH 10 RUPEES NOTES.')
else:
balance += amount
cursor.execute("UPDATE accounts SET balance = %s WHERE id = %s",
(balance, user_id))
conn.commit()
print(f'LODGEMENT SUCCESSFUL. NEW BALANCE: {balance} RUPEES.')
elif response == 'p':
new_pin = input('ENTER NEW PIN: ')
if len(new_pin) == 4 and new_pin.isdigit():
cursor.execute("UPDATE accounts SET pin = %s WHERE id = %s",
(new_pin, user_id))
conn.commit()
print('PIN UPDATED SUCCESSFULLY.')
else:
print('PIN MUST CONSIST OF 4 DIGITS.')
elif response == 'q':
print('THANK YOU FOR USING OUR SERVICE.')
sys.exit()
else:
print('INVALID OPTION.')
# Main Execution
try:
user_id, username, pin_db, balance = authenticate_user()
verify_pin(user_id, pin_db)
print(f'LOGIN SUCCESSFUL. WELCOME {username.capitalize()}!')
atm_menu(user_id, username, balance)
except Exception as e:
print(f"ERROR: {e}")
finally:
cursor.close()
conn.close()
===================================================================================
=============================================
database
USE atm_system;