0% found this document useful (0 votes)
6 views

Updated Atm Code

Uploaded by

aakaashram8
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Updated Atm Code

Uploaded by

aakaashram8
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import mysql.

connector
import sys

# Connect to MySQL database


conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="atm_system"
)
cursor = conn.cursor()

# 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

CREATE DATABASE atm_system;

USE atm_system;

CREATE TABLE accounts (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
pin CHAR(4) NOT NULL,
balance DECIMAL(10, 2) NOT NULL
);

INSERT INTO accounts (username, pin, balance)


VALUES
('jitendra', '1111', 1000.00),
('sunny', '2222', 2000.00),
('vivek', '3333', 3000.00);
===================================================================================
==========================================

You might also like