Bank Management System

Raj Aryan
5 min readAug 5, 2024

--

The bank management system presented here is implemented using Python. It provides functionalities such as account creation, deposit, withdrawal, updating account details, and checking balances. Below, I elaborate on the key points and provide detailed explanations of important code snippets and keywords.

Importing Modules

The program begins by importing several essential modules:

import pickle
import datetime
import os
import random
  • pickle: Used for serializing and deserializing Python object structures, enabling the storage and retrieval of account data to and from a file.
  • datetime: Provides functionalities to work with dates and times, useful for logging account creation times and other time-based operations.
  • os: Provides a way to interact with the operating system, enabling file operations like renaming and deleting files.
  • random: Used for generating random numbers, essential for creating unique account numbers and OTPs.

Creating an Account

The create() function allows users to create a new bank account. It ensures all necessary details are collected and validated before storing the account information.

def create():
f = open("bank.dat", "wb")
ans = 'y'
while ans == 'y':
while True:
accno = random.randint(10000000, 99999999)
r = acc_no(accno)
if r == 1:
break
name = input("Enter Account Name=").capitalize()
# (Other input fields: phone, address, city, email, date of birth, password)
deposit = int(input("Deposit Min^m rs500="))
while deposit < 500:
deposit += int(input("Add more money: "))
rec = [accno, name, phone, address, city, email, dob, passwd, deposit]
pickle.dump(rec, f)
print("....ACCOUNT SUCCESSFULLY CREATED...")
ans = input("Create More Account\nPRESS(y/n)=")
f.close()
  • Account Number Generation: A random 8-digit account number is generated using random.randint(), and acc_no() checks for uniqueness.
  • Validation: Ensures that phone number, email, date of birth, and initial deposit are valid.
  • Data Storage: Uses pickle.dump() to store account data in bank.dat

Account Number Check

The acc_no(a) function checks if an account number already exists.

def acc_no(a):
f = open("bank.dat", "rb")
found = False
try:
while True:
rec = pickle.load(f)
if rec[0] == a:
found = True
except:
if not found:
return 1
f.close()
  • Reading Data: Uses pickle.load() to read records from the file.
  • Unique Check: Returns 1 if the account number is unique.

Date of Birth Validation

The birth(dob) function ensures the provided date of birth is in the correct format and the user is at least 18 years old.

def birth(dob):
if dob[:4].isdigit() and dob[5:7].isdigit() and dob[8:10].isdigit() and dob[4] == '-' and dob[7] == '-':
if int(dob[:4]) < 2004:
# (Further validations for months and days)
return 1
else:
print("Invalid format")
  • Format Check: Validates the format YYYY-MM-DD.
  • Age Check: Ensures the user is at least 18 years old.

Displaying All Account Details

def all_details():
f = open("bank.dat", "rb")
try:
while True:
rec = pickle.load(f)
print(rec)
except EOFError:
f.close()

Viewing Specific Account Details

The detail() function shows the details of a specific account.

def detail():
f = open("bank.dat", "rb")
accno = int(input("Enter account number"))
passwd = input("Enter password")
try:
while True:
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
# (Print account details)
break
except EOFError:
print("Account Not Found")
f.close()

Authentication: Checks if the entered account number and password match.

Deleting an Account

The delete() function removes an account from the database.

def delete():
temp = open("temp.dat", "wb")
f = open("bank.dat", "rb")
accno = int(input("Enter account number"))
passwd = input("Enter account password")
found = False
try:
while True:
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
found = True
continue
pickle.dump(rec, temp)
except EOFError:
if found:
os.remove("bank.dat")
os.rename("temp.dat", "bank.dat")
  • Temporary File: Creates a temporary file to store all records except the one to be deleted.
  • File Replacement: Deletes the original file and renames the temporary file.

Updating Account Details

The update() function allows modification of account details.

def update():
accno = int(input("enter account no."))
passwd = input("enter account password")
f = open("bank.dat", "rb+")
try:
while True:
pos = f.tell()
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
while True:
z = int(input("enter for update"))
if z == 1:
rec[1] = input("enter new name")
# (Other updates)
if z == 0:
break
f.seek(pos)
pickle.dump(rec, f)
break
except EOFError:
print("Account not found")
f.close()

Seek and Update: Uses f.seek(pos) to update the specific record in place.

Depositing Money

The deposit() function adds money to an account.

def deposit():
f = open("bank.dat", "rb+")
accno = int(input("Enter account number"))
passwd = input("Enter account password")
try:
while True:
pos = f.tell()
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
d = int(input("Enter amount to deposit"))
rec[8] += d
f.seek(pos)
pickle.dump(rec, f)
break
except EOFError:
print("Account not found")
f.close()

Withdrawing Money

OTP Verification: Ensures secure transactions using a randomly generated OTP.

def withdraw():
f = open("bank.dat", "rb+")
accno = int(input("Enter account number"))
passwd = input("Enter account password")
try:
while True:
pos = f.tell()
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
d = int(input("Enter amount to withdraw"))
otp = random.randint(1000, 9999)
ou = int(input("Enter OTP"))
if otp == ou and rec[8] >= d:
rec[8] -= d
f.seek(pos)
pickle.dump(rec, f)
break
except EOFError:
print("Account not found")
f.close()

Checking Balance

The balance() function shows the balance of an account.

def balance():
f = open("bank.dat", "rb")
accno = int(input("Enter account number"))
passwd = input("Enter password")
try:
while True:
rec = pickle.load(f)
if rec[0] == accno and rec[7] == passwd:
print("Balance=", rec[8])
break
except EOFError:
print("Account not found")
f.close()

Main Menu

The main menu handles user interactions and directs them to the appropriate functions based on their choices.

ch = None
while True:
print('*'*73)
print("TODAY'S DATE:", datetime.date.today(), "\tMAIN MENU\t")
ch = int(input("Enter Your Choice="))
if ch == 1:
create()
elif ch == 2:
while True:
ch1 = int(input("enter your choice="))
if ch1 == 1:
detail()
elif ch1 == 2:
balance()
elif ch1 == 3:
deposit()
elif ch1 == 4:
withdraw()
elif ch1 == 5:
break
elif ch == 0:
break
elif ch == 9:
key = 12345
p = int(input("Enter password To See Details"))
if key == p:
while True:
ch2 = int(input("Enter your choice="))
if ch2 == 1:
all_details()
elif ch2 == 2:
update()
elif ch2 == 3:
delete()
elif ch2 == 4:
break
else:
print("Wrong Password")
else:
print("Wrong choice")
  • User Choices: Directs users to account creation, management, and transaction functionalities.
  • Password Protection: The banker-specific menu is protected by a hardcoded password (key = 12345).

Conclusion

This bank management system covers all essential functionalities required for basic banking operations. It ensures data validation, security (through OTPs and password protection), and provides a user-friendly interface for managing accounts and transactions. The use of pickle for data storage makes it easy to serialize and deserialize account information efficiently.

Full Code:

Github Portfolio instagram X linkedin

--

--