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

Code Bms Project

Uploaded by

Nihal Saade
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)
8 views

Code Bms Project

Uploaded by

Nihal Saade
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/ 15

CODE

import mysql.connector as con

import random

def connect_to_db():

return con.connect(

host="localhost",

user="root",

password="Isalain",

database="Bank"

def create_database():

mycon = con.connect(

host="localhost",

user="root",

password="Isalain"

mycursor = mycon.cursor()

mycursor.execute("CREATE DATABASE IF NOT EXISTS Bank")

mycursor.close()

mycon.close()

print("Database created or already exists.")

def create_table_pat():

create_database()

mycon = connect_to_db()

mycursor = mycon.cursor()

sql_a = """
CREATE TABLE IF NOT EXISTS accounts (

account_id INT PRIMARY KEY,

name VARCHAR(255) NOT NULL,

balance DECIMAL(10, 2) NOT NULL,

pincode CHAR(4) NOT NULL,

phone_number VARCHAR(15),

email_address VARCHAR(255)

"""

sql_l = """

CREATE TABLE IF NOT EXISTS loans (

account_id INT,

amount DECIMAL(10, 2),

interest_rate DECIMAL(5, 2),

time_period INTEGER,

interest DECIMAL(10, 2),

total_amount DECIMAL(10, 2),

FOREIGN KEY (account_id) REFERENCES accounts(account_id)

"""

mycursor.execute(sql_a)

mycursor.execute(sql_l)

mycon.commit()

mycursor.close()

mycon.close()

print("Tables created successfully")

# Function to create a new account

def create_account(cursor):

name = input("Enter your name: ")

balance = float(input("Enter initial deposit: "))


while True:

pincode = input("Enter a four-digit pincode: ")

if pincode.isdigit() and len(pincode) == 4:

pincode = int(pincode)

break

print("Error: Pincode must be a four-digit number.")

while True:

phone_number = input("Enter phone number: ")

if phone_number.isdigit() and len(phone_number)==10:

phone_number = int(phone_number)

break

else:

print("Error: Phone number must be a ten-digit number.")

valid_domains = ["@gmail.com", "@yahoo.com", "@hotmail.com", "@outlook.com"]

while True:

email_address = input("Enter email address (use gmail, yahoo, hotmail, or outlook domain): ")

email_valid = False

if len(email_address) >= 10:

for domain in valid_domains:

domain_length = len(domain)

if email_address[-domain_length:] == domain:

email_valid = True

break
if email_valid:

break

else:

print("Error: The email address entered is invalid. Please use a valid domain.")

while True:

account_id = random.randint(10000, 99999)

cursor.execute("SELECT COUNT(*) FROM accounts WHERE account_id = %s", (account_id,))

if cursor.fetchone()[0] == 0:

break

query = """

INSERT INTO accounts (account_id, name, balance, pincode, phone_number, email_address)

VALUES (%s, %s, %s, %s, %s, %s)

"""

cursor.execute(query, (account_id, name, balance, pincode, phone_number, email_address))

print("Account created successfully with Account ID: ",account_id)

# Function to deposit money

def deposit(cursor):

account_id = int(input("Enter account ID: "))

pincode = input("Enter a four-digit pincode: ")

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

if not pincode.isdigit() or len(pincode) != 4:

print("Error: Pincode must be a four-digit number.")

return

check_query = "SELECT COUNT(*) FROM accounts WHERE account_id = %s AND pincode = %s"
cursor.execute(check_query, (account_id, pincode))

result = cursor.fetchone()

if result[0] == 1:

query = "UPDATE accounts SET balance = balance + %s WHERE account_id = %s"

cursor.execute(query, (amount, account_id))

print("Deposit successful!")

else:

print("Error: Invalid account ID or pincode.")

# Function to withdraw money

def withdraw(cursor):

account_id = int(input("Enter account ID: "))

pincode = input("Enter a four-digit pincode: ")

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

if not pincode.isdigit() or len(pincode) != 4:

print("Error: Pincode must be a four-digit number.")

return

if amount > 1000000:

print("Error: Cannot withdraw more than 10 lakh in a single transaction.")

return

cursor.execute("SELECT balance FROM accounts WHERE account_id = %s AND pincode = %s",


(account_id, pincode))

result = cursor.fetchone()

if result:

balance = result[0]

if balance >= amount:


query = "UPDATE accounts SET balance = balance - %s WHERE account_id = %s"

cursor.execute(query, (amount, account_id))

print("Withdrawal successful!")

else:

print("Insufficient balance!")

else:

print("Error: Invalid account ID or pincode.")

# Function to handle loan application

def loan(cursor):

account_id = int(input("Enter account ID: "))

cursor.execute("SELECT COUNT(*) FROM accounts WHERE account_id = %s", (account_id,))

if cursor.fetchone()[0] == 0:

print("Error: Account does not exist.")

return

while True:

loan_amount = float(input("Enter loan amount (up to 50 lakhs): "))

if loan_amount > 5000000:

print("Error: Loan amount exceeds the limit of 50 lakhs. Please enter a valid amount.")

else:

break

time = float(input("Enter the loan duration in years: "))

if loan_amount < 100000:

base_rate = 3.0

elif 100000 <= loan_amount <= 1000000:

base_rate = 5.0

elif 1000000 < loan_amount <= 5000000:

base_rate = 7.0
else:

base_rate = 0.0

if time < 1:

interest_rate = base_rate - 0.5

elif 1 <= time <= 5:

interest_rate = base_rate

else:

interest_rate = base_rate + 1.0

interest_rate_decimal = interest_rate / 100

interest = loan_amount * interest_rate_decimal * time

total_amount = loan_amount + interest

query = """

INSERT INTO loans (account_id, amount, interest_rate, time_period, interest, total_amount)

VALUES (%s, %s, %s, %s, %s, %s)

"""

cursor.execute(query, (account_id, loan_amount, interest_rate, time, interest, total_amount))

print(f"Loan applied successfully! Interest Rate: {interest_rate:.2f}%, Interest: {interest:.2f}, Total


amount to be repaid: {total_amount:.2f}")

# Function to view account details

def view_account(cursor):

while True:

account_id = int(input("Enter account ID: "))

pincode = input("Enter a four-digit pincode: ")


if not pincode.isdigit() or len(pincode) != 4:

print("Error: Pincode must be a four-digit number.")

return

query = "SELECT * FROM accounts WHERE account_id = %s AND pincode = %s"

cursor.execute(query, (account_id, pincode))

account = cursor.fetchone()

if account:

print(f"Account ID: {account[0]}\nName: {account[1]}\nBalance: {account[2]}\nPincode:


{account[3]}\nPhone Number: {account[4]}\nEmail Address: {account[5]}")

else:

print("Account not found or incorrect pincode!")

# Menu-driven interface

def menu():

conn = connect_to_db()

cursor = conn.cursor()

while True:

print("\n--- Bank Management System ---")

print("1. Create Account")

print("2. Deposit Money")

print("3. Withdraw Money")

print("4. Apply for Loan")

print("5. View Account Details")

print("6. Exit")

choice = input("Enter your choice: ")

if choice == '1':

create_account(cursor)
elif choice == '2':

deposit(cursor)

elif choice == '3':

withdraw(cursor)

elif choice == '4':

loan(cursor)

elif choice == '5':

view_account(cursor)

elif choice == '6':

break

else:

print("Invalid choice!")

conn.commit()

cursor.close()

conn.close()

if __name__ == "__main__":

create_table_pat()

create_database()

menu()
Sample output

Enter a four-digit pincode: 7777

Enter phone number: 9123456789

Enter email address , please use your gmail, yahoo, hotmail or outlook domain: [email protected]

Database created or already exists.

Tables created successfully

Database created or already exists.

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 1

Enter your name: Amal

Enter initial deposit: 10000

Enter a four-digit pincode: 77777

Error: Pincode must be a four-digit number.

Enter a four-digit pincode: 3333

Enter phone number: 91263784648

Error: Phone number must be a ten-digit number

Enter phone number: 9123546789

Enter email address (use gmail, yahoo, hotmail, or outlook domain): [email protected]

Error: The email address entered is invalid. Please use a valid domain.

Enter email address , please use your gmail, yahoo, hotmail or outlook domain: [email protected]

Account created successfully with Account ID: 14371

--- Bank Management System ---


1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 1

Enter your name: Nihal

Enter initial deposit: 15000

Enter a four-digit pincode: 4444

Enter phone number: 9123786543

Enter email address , please use your gmail, yahoo, hotmail or outlook domain: [email protected]

the email id entered is invalid

Enter email address , please use your gmail, yahoo, hotmail or outlook domain: [email protected]

the email id entered is invalid

Enter email address , please use your gmail, yahoo, hotmail or outlook domain:

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 1

Enter your name: Jeriah

Enter initial deposit: 10000

Enter a four-digit pincode: 6666

Enter phone number: 9165892323

Enter email address (use gmail, yahoo, hotmail, or outlook domain): gff

Error: The email address entered is invalid. Please use a valid domain.

Enter email address (use gmail, yahoo, hotmail, or outlook domain): [email protected]
Account created successfully with Account ID: 99740

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 1

Enter your name: Aman

Enter initial deposit: 7000

Enter a four-digit pincode: 3333

Enter phone number: 91674544547

Error: Phone number must be a ten-digit number.

Enter phone number: 9134567889

Enter email address (use gmail, yahoo, hotmail, or outlook domain): [email protected]

Account created successfully with Account ID: 17257

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 2

Enter account ID: 17257

Enter a four-digit pincode: 3333

Enter amount to deposit: 5600

Deposit successful!
--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 3

Enter account ID: 17257

Enter a four-digit pincode: 3333

Enter amount to withdraw: 4000

Withdrawal successful!

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 4

Enter account ID: 17257

Enter loan amount (up to 50 lakhs): 4900000

Enter the loan duration in years: 3

Loan applied successfully! Interest Rate: 7.00%, Interest: 1029000.00, Total amount to be repaid:
5929000.00

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money
4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 5

Enter account ID: 17257

Enter a four-digit pincode: 3333

Account ID: 17257

Name: Aman

Balance: 12600.00

Pincode: 3333

Phone Number: 9134567889

Email Address: [email protected]

--- Bank Management System ---

1. Create Account

2. Deposit Money

3. Withdraw Money

4. Apply for Loan

5. View Account Details

6. Exit

Enter your choice: 6

>>>

MYSQL

mysql> use bank;

Database changed

mysql> show tables;

+----------------+

| Tables_in_bank |
+----------------+

| accounts |

| loans |

+----------------+

2 rows in set (0.02 sec)

mysql> select * from accounts;

+------------+--------+----------+---------+--------------+----------------------+

| account_id | name | balance | pincode | phone_number | email_address |

+------------+--------+----------+---------+--------------+----------------------+

| 17257 | Aman | 8600.00 | 3333 | 9134567889 | [email protected] |

| 68678 | Nihal | 10000.00 | 4444 | 9135753587 | [email protected] |

| 83411 | Amal | 5000.00 | 7777 | 9167432356 | [email protected] |

| 99740 | Jeriah | 10000.00 | 6666 | 9165892323 | [email protected] |

+------------+--------+----------+---------+--------------+----------------------+

4 rows in set (0.00 sec)

mysql> select * from loans;

+------------+------------+---------------+-------------+------------+--------------+

| account_id | amount | interest_rate | time_period | interest | total_amount |

+------------+------------+---------------+-------------+------------+--------------+

| 17257 | 4900000.00 | 7.00 | 3 | 1029000.00 | 5929000.00 |

+------------+------------+---------------+-------------+------------+--------------+

1 row in set (0.00 sec)

You might also like