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

Import Mysql

Uploaded by

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

Import Mysql

Uploaded by

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

import mysql.

connector as sql

# Database connection
conn = sql.connect(host='localhost', user='root', password='1234', database='ATM_MACHINE')
c1 = conn.cursor()

# Function to create a new account


def create_account():
while True:
account_number = int(input("Enter a 4-digit number as account number: "))

# Check if account already exists


query = "SELECT * FROM records WHERE ACCOUNT_NO = %s"
c1.execute(query, (account_number,))
result = c1.fetchone()

if result:
print("This account number already exists.")
continue_choice = input("Do you want to continue (y/n)? ")
if continue_choice.lower() != 'y':
print("Thank you for visiting.")
return
else:
name = input("Enter your name: ")

# Insert new account into the database (no password)


insert_query = "INSERT INTO records (ACCOUNT_NO, NAME, CR_AMT, BALANCE,
WITHDRAWAL) VALUES (%s, %s, 0, 0, 0)"
c1.execute(insert_query, (account_number, name))
conn.commit()
print("Account successfully created. The minimum balance is 1000.")

# Deposit initial amount


deposit = int(input("Enter the money to be deposited: "))
deposit_query = "UPDATE records SET CR_AMT = %s, BALANCE = %s WHERE
ACCOUNT_NO = %s"
c1.execute(deposit_query, (deposit, deposit, account_number))
conn.commit()
print("Successfully deposited.")
return

# Function for user login (without password)


def login():
while True:
account_number = int(input("Enter your account number: "))
query = "SELECT * FROM records WHERE ACCOUNT_NO = %s"
c1.execute(query, (account_number,))
account_data = c1.fetchone()

if account_data:
print("Login successful.")
return account_number
else:
print("Account does not exist.")
if input("Do you want to try again (y/n)? ").lower() != 'y':
return None
# Function to handle ATM operations for a logged-in user
def account_operations(account_number):
while True:
print("\n1. Deposit money")
print("2. Withdraw money")
print("3. Transfer money")
print("4. Check balance")
print("5. Change account number")
print("6. Logout")

print("========================================================================
========")

choice = int(input("Enter your choice: "))

print("========================================================================
========")

if choice == 1:
deposit_money(account_number)
elif choice == 2:
withdraw_money(account_number)
elif choice == 3:
transfer_money(account_number)
elif choice == 4:
check_balance(account_number)
elif choice == 5:
change_account_number(account_number)
elif choice == 6:
print("Logging out.")
break
else:
print("Invalid choice. Please try again.")

# Function to deposit money into the account


def deposit_money(account_number):
amount = int(input("Enter the money to be deposited: "))
deposit_query = "UPDATE records SET CR_AMT = CR_AMT + %s, BALANCE = BALANCE + %s
WHERE ACCOUNT_NO = %s"
c1.execute(deposit_query, (amount, amount, account_number))
conn.commit()
print(f"Successfully deposited {amount}.")

# Function to withdraw money from the account


def withdraw_money(account_number):
amount = int(input("Enter the money to withdraw: "))
balance_query = "SELECT BALANCE FROM records WHERE ACCOUNT_NO = %s"
c1.execute(balance_query, (account_number,))
current_balance = c1.fetchone()[0]

if amount > current_balance:


print("Insufficient balance. Please try again.")
else:
withdraw_query = "UPDATE records SET BALANCE = BALANCE - %s, WITHDRAWAL =
WITHDRAWAL + %s WHERE ACCOUNT_NO = %s"
c1.execute(withdraw_query, (amount, amount, account_number))
conn.commit()
print(f"Successfully withdrew {amount}.")

# Function to transfer money to another account


def transfer_money(account_number):
recipient_account = int(input("Enter the account number to transfer to: "))

# Check if recipient account exists


query = "SELECT * FROM records WHERE ACCOUNT_NO = %s"
c1.execute(query, (recipient_account,))
recipient_data = c1.fetchone()

if not recipient_data:
print("Recipient account does not exist.")
return

transfer_amount = int(input("Enter the money to be transferred: "))


balance_query = "SELECT BALANCE FROM records WHERE ACCOUNT_NO = %s"
c1.execute(balance_query, (account_number,))
current_balance = c1.fetchone()[0]

if transfer_amount > current_balance:


print("Insufficient balance. Please try again.")
else:
# Deduct amount from sender and add to recipient
deduct_query = "UPDATE records SET BALANCE = BALANCE - %s, WITHDRAWAL =
WITHDRAWAL + %s WHERE ACCOUNT_NO = %s"
add_query = "UPDATE records SET BALANCE = BALANCE + %s, CR_AMT = CR_AMT + %s
WHERE ACCOUNT_NO = %s"

c1.execute(deduct_query, (transfer_amount, transfer_amount, account_number))


c1.execute(add_query, (transfer_amount, transfer_amount, recipient_account))
conn.commit()
print(f"Successfully transferred {transfer_amount} to account {recipient_account}.")

# Function to check the account balance


def check_balance(account_number):
query = "SELECT BALANCE FROM records WHERE ACCOUNT_NO = %s"
c1.execute(query, (account_number,))
balance = c1.fetchone()[0]
print(f"Your current balance is: {balance}")

# Function to change the account number


def change_account_number(account_number):
new_account_number = int(input("Enter your new account number: "))

# Check if the new account number already exists


query = "SELECT * FROM records WHERE ACCOUNT_NO = %s"
c1.execute(query, (new_account_number,))
if c1.fetchone():
print("This account number already exists. Please try again.")
return

# Update the account number


update_query = "UPDATE records SET ACCOUNT_NO = %s WHERE ACCOUNT_NO = %s"
c1.execute(update_query, (new_account_number, account_number))
conn.commit()
print(f"Your account number has been successfully changed to {new_account_number}.")

# Main menu for user options


def main_menu():
while True:

print("========================================================================
========")
print(" WELCOME TO OUR ATM ")

print("========================================================================
========")
print("1. Create an account")
print("2. Login")
print("3. Exit")

print("========================================================================
========")

choice = int(input("Enter your choice: "))

print("========================================================================
========")

if choice == 1:
create_account()
elif choice == 2:
account_number = login()
if account_number:
account_operations(account_number)
elif choice == 3:
print("Exiting. Please close this file before exiting.")
c1.close()
conn.close()
break

# Ask if user wants to continue or exit after performing an action


continue_choice = input("Do you want to perform another operation (y/n)? ").lower()
if continue_choice != 'y':
print("Thank you for using the ATM. Goodbye!")
break

# Main function
if __name__ == "__main__":
main_menu()

You might also like