0% found this document useful (0 votes)
22 views11 pages

MUSKAN

This document contains Python code for a bank management system that connects to a MySQL database. It includes functions for creating accounts, logging in, viewing account details, depositing, withdrawing, transferring money, and viewing transaction history. The main menu allows users to navigate through these functionalities.

Uploaded by

skvjict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

MUSKAN

This document contains Python code for a bank management system that connects to a MySQL database. It includes functions for creating accounts, logging in, viewing account details, depositing, withdrawing, transferring money, and viewing transaction history. The main menu allows users to navigate through these functionalities.

Uploaded by

skvjict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SOURCE CODE:

import mysql.connector as sql

import sys

# Establishing MySQL database connection

conn = sql.connect(

host='localhost',

user='root',

password='123456',

database='bank_system'

if conn.is_connected():

print("Successfully connected to the database")

# Create a cursor object

cursor = conn.cursor()

# Function to create a new account

def create_account():

name = input("Enter your name: ")

phone = input("Enter your phone number: ")

email = input("Enter your email: ")

passward = input("Enter a password: ")

# Initial balance is 0

balance = 0.00
sql_insert = "INSERT INTO customers (name, balance, phone, email, passward) VALUES (%s, %s, %s,
%s, %s)"

cursor.execute(sql_insert, (name, balance, phone, email, passward))

conn.commit()

print("Account created successfully!")

# Function to login

def login():

email = input("Enter your email: ")

passward = input("Enter your password: ")

sql_query = "SELECT * FROM customers WHERE email = %s AND passward = %s"

cursor.execute(sql_query, (email, passward))

user = cursor.fetchone()

if user:

print(f"Welcome, {user[1]}!")

return user[0] # Return customer_id

else:

print("Invalid credentials!")

return None

# Function to view account details

def view_account(customer_id):

sql_query = "SELECT * FROM customers WHERE customer_id = %s"

cursor.execute(sql_query, (customer_id,))

user = cursor.fetchone()
if user:

print(f"Account ID: {user[0]}")

print(f"Name: {user[1]}")

print(f"Balance: {user[2]}")

print(f"Phone: {user[3]}")

print(f"Email: {user[4]}")

else:

print("Account not found.")

# Function to deposit money

def deposit_money(customer_id):

amount = float(input("Enter amount to deposit: "))

if amount <= 0:

print("Invalid amount!")

return

# Update balance in the customers table

sql_update = "UPDATE customers SET balance = balance + %s WHERE customer_id = %s"

cursor.execute(sql_update, (amount, customer_id))

conn.commit()

# Log the transaction

sql_insert = "INSERT INTO transactions (customer_id, transaction_type, amount) VALUES (%s,


'Deposit', %s)"

cursor.execute(sql_insert, (customer_id, amount))

conn.commit()

print(f"Successfully deposited {amount} into your account!")


# Function to withdraw money

def withdraw_money(customer_id):

amount = float(input("Enter amount to withdraw: "))

if amount <= 0:

print("Invalid amount!")

return

# Get current balance

sql_query = "SELECT balance FROM customers WHERE customer_id = %s"

cursor.execute(sql_query, (customer_id,))

current_balance = cursor.fetchone()[0]

if amount > current_balance:

print("Insufficient balance!")

return

# Update balance in the customers table

sql_update = "UPDATE customers SET balance = balance - %s WHERE customer_id = %s"

cursor.execute(sql_update, (amount, customer_id))

conn.commit()

# Log the transaction

sql_insert = "INSERT INTO transactions (customer_id, transaction_type, amount) VALUES (%s,


'Withdrawal', %s)"

cursor.execute(sql_insert, (customer_id, amount))

conn.commit()

print(f"Successfully withdrew {amount} from your account!")


# Function to transfer money

def transfer_money(customer_id):

recipient_id = int(input("Enter recipient account ID: "))

amount = float(input("Enter amount to transfer: "))

if amount <= 0:

print("Invalid amount!")

return

# Get current balance of sender

sql_query = "SELECT balance FROM customers WHERE customer_id = %s"

cursor.execute(sql_query, (customer_id,))

current_balance = cursor.fetchone()[0]

if amount > current_balance:

print("Insufficient balance!")

return

# Update balance of sender (decrease balance)

sql_update_sender = "UPDATE customers SET balance = balance - %s WHERE customer_id = %s"

cursor.execute(sql_update_sender, (amount, customer_id))

# Update balance of recipient (increase balance)

sql_update_recipient = "UPDATE customers SET balance = balance + %s WHERE customer_id = %s"

cursor.execute(sql_update_recipient, (amount, recipient_id))

# Log transactions for both sender and recipient

sql_insert_sender = "INSERT INTO transactions (customer_id, transaction_type, amount) VALUES (%s,


'Transfer', %s)"
cursor.execute(sql_insert_sender, (customer_id, amount))

sql_insert_recipient = "INSERT INTO transactions (customer_id, transaction_type, amount) VALUES


(%s, 'Received Transfer', %s)"

cursor.execute(sql_insert_recipient, (recipient_id, amount))

conn.commit()

print(f"Successfully transferred {amount} to account {recipient_id}!")

# Function to view transaction history

def view_transactions(customer_id):

sql_query = "SELECT * FROM transactions WHERE customer_id = %s ORDER BY transaction_date


DESC"

cursor.execute(sql_query, (customer_id,))

transactions = cursor.fetchall()

print("Transaction History:")

for transaction in transactions:

print(f"ID: {transaction[0]}, Type: {transaction[2]}, Amount: {transaction[3]}, Date: {transaction[4]}")

# Main menu

def main_menu():

while True:

print("\n--- BANK MANAGEMENT SYSTEM ---")

print("1. Create Account")

print("2. Login")

print("3. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:

create_account()

elif choice == 2:

customer_id = login()

if customer_id:

while True:

print("\n1. View Account")

print("2. Deposit Money")

print("3. Withdraw Money")

print("4. Transfer Money")

print("5. View Transactions")

print("6. Logout")

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

if sub_choice == 1:

view_account(customer_id)

elif sub_choice == 2:

deposit_money(customer_id)

elif sub_choice == 3:

withdraw_money(customer_id)

elif sub_choice == 4:

transfer_money(customer_id)

elif sub_choice == 5:

view_transactions(customer_id)

elif sub_choice == 6:
print("Logging out...")

break

else:

print("Invalid choice! Please try again.")

elif choice == 3:

print("Exiting the system...")

sys.exit()

else:

print("Invalid choice! Please try again.")

if __name__ == "__main__":

main_menu()

# Close the database connection when done

cursor.close()

conn.close()

image.png

OUTPUT:

You might also like