0

Bank Management System in Python

Bank Management System Project in Python

Today we are going to build console based Bank Management System in Python

SO let get started our project,

Concepts Used:

Modules to imports

from random import randint

Account Class(parent class)

'''parent account class to be extending by three type of
accounts checking,business and saving'''
class Account:
#constructor to initialize class members
def __init__(self,firstName='',lastName='',uid='',address='',accType='',phoneNum='',initialBalance = 0.0):
self.firstName = firstName
self.lastName = lastName
self.uid = uid
self.address = address
self.accType = accType
self.phoneNum = phoneNum
self.initialBalance = initialBalance
def get_firstName(self):
return self.firstName

# setter method
def set_firstName(self, x):
self.firstName = x



def get_lastName(self):
return self.lastName

# setter method
def set_lastName(self, x):
self.lastName = x


def get_uid(self):
return self.uid

# setter method
def set_uid(self, x):
self.uid = x


def get_address(self):
return self.address

# setter method
def set_address(self, x):
self.address = x



def get_accType(self):
return self.accType

# setter method
def set_accType(self, x):
self.accType = x


def get_phoneNum(self):
return self.phoneNum

# setter method
def set_phoneNum(self, x):
self.phoneNum = x


def get_initialBalance(self):
return self.initialBalance

# setter method
def set_initialBalance(self, x):
self.initialBalance = x


def get_age(self):
return self.age

# setter method
def set_age(self, x):
self.age = x

'''This function will save the account holder info according to account type'''
def saveAccount(self):
'''This will check first account type then saves data according to it
It will generated random id and place account type key before it then appends the all user data to file'''
if self.get_accType() == 'c' or self.get_accType() == 'C':
secNum = randint(1000,10000)
secNum = 'che'+str(secNum)
print('your secret key is: ',secNum)
f= open("checkingAccount.txt","a+")
f.write(f"\n{self.get_firstName().replace(' ','_')} {self.get_lastName().replace(' ','_')} {self.get_uid().replace(' ','_')} {self.get_address().replace(' ','_')} {self.get_accType()} {self.get_phoneNum().replace(' ','_')} {secNum}")
f.close()
f1= open("checkingAccountBalance.txt","a+")
f1.write(f"\n{secNum} {self.get_initialBalance()}")
f1.close()
elif self.get_accType() == 's' or self.get_accType() == 'S':
secNum = randint(1000,10000)
secNum = 'sav'+str(secNum)
print('your secret key is: ',secNum)
f= open("savingAccount.txt","a+")
f.write(f"\n{self.get_firstName().replace(' ','_')} {self.get_lastName().replace(' ','_')} {self.get_uid().replace(' ','_')} {self.get_address().replace(' ','_')} {self.get_accType()} {self.get_phoneNum().replace(' ','_')} {secNum}")
f.close()
f1= open("savingAccountBalance.txt","a+")
f1.write(f"\n{secNum} {self.get_initialBalance()}")
f1.close()
elif self.get_accType() == 'b' or self.get_accType() == 'B':
secNum = randint(1000,10000)
secNum = 'bus'+str(secNum)
busiName = input('Enter your Business Name : ')
busiregNum = input('Enter your business registration number : ')
ownerName = input('Enter business owner name : ')
print('your secret key is: ',secNum)
f= open("businessAccount.txt","a+")
f.write(f"\n{self.get_firstName().replace(' ','_')} {self.get_lastName().replace(' ','_')} {self.get_uid().replace(' ','_')} {self.get_address().replace(' ','_')} {self.get_accType()} {self.get_phoneNum().replace(' ','_')} {secNum} {busiName.replace(' ','_')} {busiregNum.replace(' ','_')} {ownerName.replace(' ','_')}")
f.close()
f1= open("businessAccountBalance.txt","a+")
f1.write(f"\n{secNum} {self.get_initialBalance()}")
f1.close()
print(self.get_accType())

#This function will check id id randomly generated already present
def checkDuplication(self):
print(self.get_uid())
if self.get_accType() == 'c' or self.get_accType() == 'C':
with open('checkingAccount.txt','r') as f:
if self.get_uid() in f.read():
return True
elif self.get_accType() == 's' or self.get_accType() == 'S':
with open('savingAccount.txt','r') as f:
if self.get_uid() in f.read():
return True

elif self.get_accType() == 'b' or self.get_accType() == 'B':
with open('businessAccount.txt','r') as f:
if self.get_uid() in f.read():
return True

CheckingAccount class(child class of Account class)

#CheckingAccount class when is child class of Account class                    
class CheckingAccount(Account):
def displayAccount(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
#reading all data from file and matching user secret num to numbers in file,if it finds then it will show their info
f = open('checkingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
accdata = line
find = 1
f1 = open('checkingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
find = 1
if find == 1:
accdata = accdata.split()
name = accdata[0]+' '+accdata[1]
ide = accdata[2]
address = accdata[3].replace('_',' ')
accountType = 'Checking Account'
phone = accdata[5]
pin = accdata[6]
balance = accbalance.split()[-1]
print('Name : '+name)
print('Id : '+ide)
print('Address : '+address)
print('Account Type : ',accountType)
print('Phone Number : ',phone)
print('Balance : ',balance,' AED')

else:
print('No Account Found!')
tr = 1
return tr

def depositMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to deposit,user will enter amount then amount will be replace by adding new amount'''
f = open('checkingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('checkingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to deposit: '))
newbalance = balance + Balance
f1.close()
with open('checkingAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('checkingAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount deposited successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr

def withdrawMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to withdraw,user will enter amount then after validation amount will be replace by subtracting new amount'''
f = open('checkingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('checkingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to withdraw: '))
if Balance+100>balance:
print('Balance to withdraw must be less than Available balance(their must be AED 100 Always in Account)')
continue

newbalance = balance - Balance
f1.close()
with open('checkingAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('checkingAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount withdrawn successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr
#This method will read balance from account,and calculates 3% interest
def callInterest(self,secNum):
find = 0
accdata = ''
accbalance = ''
balance = 0.0
tr = 1
f = open('checkingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
f1 = open('checkingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
print('your interest per annum is : ',(3/100)*balance,' AED')
tr = 0
else:
print('No Account Found!')
return tr

SavingAccount Class(child class of Account class)

#SavingAccount class when is child class of Account class
class SavingAccount(Account):
#reading all data from file and matching user secret num to numbers in file,if it finds then it will show their info
def displayAccount(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
f = open('savingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
accdata = line
find = 1
f1 = open('savingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
find = 1
if find == 1:
accdata = accdata.split()
name = accdata[0]+' '+accdata[1]
ide = accdata[2]
address = accdata[3].replace('_',' ')
accountType = 'Saving Account'
phone = accdata[5]
pin = accdata[6]
balance = accbalance.split()[-1]
print('Name : '+name)
print('Id : '+ide)
print('Address : '+address)
print('Account Type : ',accountType)
print('Phone Number : ',phone)
print('Balance : ',balance,' AED')
else:
print('No Account Found!')
tr = 1
return tr
def depositMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to deposit,user will enter amount then amount will be replace by adding new amount'''
f = open('savingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('savingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to deposit: '))
newbalance = balance + Balance
f1.close()
with open('savingAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('savingAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount deposited successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr

def withdrawMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to withdraw,user will enter amount then after validation amount will be replace by subtracting new amount'''

f = open('savingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('savingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to withdrawn: '))
if Balance+1000>balance:
print('Balance to withdraw must be less than Available balance(their must be AED 100 Always in Account)')
continue

newbalance = balance - Balance
f1.close()
with open('savingAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('savingAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount withdrawn successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr
#This method will read balance from account,and calculates 7% interest
def callInterest(self,secNum):
find = 0
accdata = ''
accbalance = ''
balance = 0.0
tr = 1
f = open('savingAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
f1 = open('savingAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
print('your interest per annum is : ',(7/100)*balance,' AED')
tr = 0
else:
print('No Account Found!')
return tr

BusinessAccount Class(child class of Account class)

#BusinessAccount class when is child class of Account class
class BusinessAccount(Account):
#reading all data from file and matching user secret num to numbers in file,if it finds then it will show their info
def displayAccount(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
f = open('businessAccount.txt','r')
for line in f.readlines() :
if secNum in line :
accdata = line
find = 1
#print(accdata)
f1 = open('businessAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
find = 1

if find == 1:
accdata = accdata.split()
name = accdata[0]+' '+accdata[1]
ide = accdata[2]
address = accdata[3].replace('_',' ')
accountType = 'Business Account'
phone = accdata[5]
bname = accdata[6]
brn = accdata[7]
bon = accdata[8]
balance = accbalance.split()[-1]
print('Name : '+name)
print('Id : '+ide)
print('Address : '+address)
print('Account Type : ',accountType)
print('Phone Number : ',phone)
print('Balance : ',balance,' AED')
print('Business Name : ',bname)
print('Business Registration Number : ',brn)
print('Business owner Name : ',bon)
else:
print('No Account Found!')
tr = 1
return tr
def depositMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to deposit,user will enter amount then amount will be replace by adding new amount'''
f = open('businessAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('businessAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to deposit: '))
newbalance = balance + Balance
f1.close()
with open('businessAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('businessAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount deposited successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr


def withdrawMoney(self,secNum):
find = 0
accdata = ''
accbalance = ''
tr = 0
'''reading all data from file and matching user secret num to numbers in file,
if it finds then it will ask for amount to withdraw,user will enter amount then after validation amount will be replace by subtracting new amount'''
f = open('businessAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
while True:
try:
f1 = open('businessAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
Balance = float(input('Enter balance to withdrawn: '))
if Balance+2000>balance:
print('Balance to withdraw must be less than Available balance(their must be AED 100 Always in Account)')
continue

newbalance = balance - Balance
f1.close()
with open('businessAccountBalance.txt', 'r') as file :
filedata = file.read()

# Replace the target string
filedata = filedata.replace(str(balance), str(newbalance))

# Write the file out again
with open('businessAccountBalance.txt', 'w') as file:
file.write(filedata)
print('Amount withdrawn successfuly!')
break
except ValueError:
print('invalid input!')
else:
print('No Account Found!')
tr = 1
return tr
#This method will read balance from account,and calculates 3% interest
def callInterest(self,secNum):
find = 0
accdata = ''
accbalance = ''
balance = 0.0
tr = 1
f = open('businessAccount.txt','r')
for line in f.readlines() :
if secNum in line :
find = 1
if find == 1:
f1 = open('businessAccountBalance.txt','r')
for line in f1.readlines() :
if secNum in line :
accbalance = line
balance = float(accbalance.split()[-1])
print('your interest per annum is : ',(3/100)*balance,' AED')
tr = 0
else:
print('No Account Found!')
return tr

main method to start execution

def main():
ch=''
num=0
#creating files to read and write data
f1 = open("checkingAccountBalance.txt", "a+")
f2 = open("savingAccountBalance.txt", "a+")
f3 = open("businessAccountBalance.txt", "a+")
f4 = open("checkingAccount.txt", "a+")
f5 = open("savingAccount.txt", "a+")
f6 = open("businessAccount.txt", "a+")
while ch != '6':
#display menu
print("MAIN MENU")
print("1. OPEN ACCOUNT")
print("2. DISPLAY AMOUNT")
print("3. DEPOSIT AMOUNT")
print("4. WITHDRAW AMOUNT")
print("5 CHECK INTEREST")
print("6. EXIT")
print("\tSelect Your Option (1-6) ")
ch = input()
if ch == '1':
try:
#user will enter required data to open account
initialBalance = 0.0
firstName = input('Enter your first name :')
lastName = input('Enter your last name :')
uid = input('Enter your id number :')
address = input('Enter your address: ')
while True:
accType = input('Enter your Account Type:[c = Checking Account,s = Saving Account,b = Business Account] ')
if accType == 'c' or accType == 'C' or accType == 's' or accType == 'S' or accType == 'b' or accType == 'B':
break
else:
print('invalid Account Type!')
print("Enter 'c' = Checking Account,'s' = Saving Account,'b' = Business Account")
continue
phoneNum = input('Enter your Phone Number :')
#loop will continues untill user enter valid amount
while True:
try:
initialBalance = float(input('Enter intial balance(AED) : '))
if accType == 'c' or accType == 'C':
if initialBalance<100:
print('balance should be greater than 100')
continue
else:
break
elif accType == 's' or accType == 'S':
if initialBalance<1000:
print('balance should be greater than 1000')
continue
else:
break
elif accType == 'b' or accType == 'B':
if initialBalance<2000:
print('balance should be greater than 2000')
continue
else:
break


except ValueError:
print('invalid input!')
if not firstName or not lastName or not uid or not address or not accType or not phoneNum or not initialBalance:
print('Fill all required feilds!')
raise ValueError
input('Enter to continue... ')
a = Account(firstName,lastName,uid,address,accType,phoneNum,initialBalance)
if a.checkDuplication():
print(f'Account with id number {uid} already exist')
continue
a.saveAccount()
except ValueError:
print('invalid input!')
#this will display account of user after user will enter its secret number
elif ch == '2':
tryi = 3
tr = 1
while tryi>0:
secNum = input('Enter your secret Number :')
secNum = secNum.lower()
if secNum[0] == 'c':
c = CheckingAccount()
tr = c.displayAccount(secNum)
elif secNum[0] == 's':
c = SavingAccount()
tr = c.displayAccount(secNum)
elif secNum[0] == 'b':
c = BusinessAccount()
tr = c.displayAccount(secNum)
else:
print('invalid Number!')
if tr == 0:
break
elif tr == 1:
tryi -= 1
if tryi == 0:
print('3 tries done!')
#this will deposit money after user will enter its secret number
elif ch == '3':
tryi = 3
tr = 1
while tryi>0:
secNum = input('Enter your secret Number :')
secNum = secNum.lower()
if secNum[0] == 'c':
c = CheckingAccount()
tr = c.depositMoney(secNum)
elif secNum[0] == 's':
c = SavingAccount()
tr = c.depositMoney(secNum)
elif secNum[0] == 'b':
c = BusinessAccount()
tr = c.depositMoney(secNum)
else:
print('invalid Number!')
if tr == 0:
break
elif tr == 1:
tryi -= 1
if tryi == 0:
print('3 tries done!')

#this will withdraw money after user will enter its secret number
elif ch == '4':
tryi = 3
tr = 1
while tryi>0:
secNum = input('Enter your secret Number :')
secNum = secNum.lower()
if secNum[0] == 'c':
c = CheckingAccount()
tr = c.withdrawMoney(secNum)
elif secNum[0] == 's':
c = SavingAccount()
tr = c.withdrawMoney(secNum)
elif secNum[0] == 'b':
c = BusinessAccount()
tr = c.withdrawMoney(secNum)
else:
print('invalid Number!')
if tr == 0:
break
elif tr == 1:
tryi -= 1
if tryi == 0:
print('3 tries done!')
break
#this will calculate interest rate after user will enter its secret number
elif ch == '5':
tryi = 3
tr = 1
while tryi>0:
secNum = input('Enter your secret Number :')
secNum = secNum.lower()
if secNum[0] == 'c':
c = CheckingAccount()
tr = c.callInterest(secNum)
#print(tr)
elif secNum[0] == 's':
c = SavingAccount()
tr = c.callInterest(secNum)
elif secNum[0] == 'b':
c = BusinessAccount()
tr = c.callInterest(secNum)
else:
print('invalid Number!')
if tr == 0:
break
elif tr == 1:
tryi -= 1
if tryi == 0:
print('3 tries done!')
break

Finally calling main method

if __name__=='__main__':
main()

keep visiting : shaadcoding

shaad

Leave a Reply

Your email address will not be published. Required fields are marked *