import pickle
import os
import pathlib
class Account:
def __init__(self):
self.accNo = 0
self.name = ""
self.deposit = 0
self.type = ""
def createAccount(self):
try:
self.accNo = int(input("Enter the account no: "))
self.name = input("Enter the account holder name: ")
self.type = input("Enter the type of account [C/S]: ")
self.deposit = int(input("Enter the Initial amount (>=500 for S, >=1000 for C): "))
if (self.type == 'S' and self.deposit < 500) or (self.type == 'C' and self.deposit <
1000):
print("Initial deposit does not meet the requirements.")
return
print("\nAccount Created\n")
except ValueError:
print("Invalid input. Please enter numeric values for account number and
deposit.")
def showAccount(self):
print(f"Account Number: {self.accNo}")
print(f"Account Holder Name: {self.name}")
print(f"Type of Account: {self.type}")
print(f"Balance: {self.deposit}")
def modify(self):
print(f"Account Number: {self.accNo}")
self.name = input("Modify Account Holder Name: ")
self.type = input("Modify type of Account: ")
self.deposit = int(input("Modify Balance: "))
def depositAmount(self, amount):
self.deposit += amount
def withdrawAmount(self, amount):
self.deposit -= amount
def report(self):
print(self.accNo, self.name, self.type, self.deposit)
def getAccountNo(self):
return self.accNo
def getAccountHolderName(self):
return self.name
def getAccountType(self):
return self.type
def getDeposit(self):
return self.deposit
# Helper functions
def writeAccountsFile(account):
try:
file = pathlib.Path("accounts.data")
if file.exists():
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
accounts.append(account)
else:
accounts = [account]
with open("accounts.data", 'wb') as outfile:
pickle.dump(accounts, outfile)
except Exception as e:
print(f"Error writing to file: {e}")
def displayAll():
file = pathlib.Path("accounts.data")
if file.exists():
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
for acc in accounts:
acc.showAccount()
print("-" * 30)
else:
print("No records to display")
def displaySp(num):
file = pathlib.Path("accounts.data")
if file.exists():
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
for acc in accounts:
if acc.accNo == num:
acc.showAccount()
return
print("No existing record with this number")
else:
print("No records to search")
def depositAndWithdraw(acc_no, option):
file = pathlib.Path("accounts.data")
if not file.exists():
print("No records to search")
return
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
for acc in accounts:
if acc.accNo == acc_no:
if option == 1:
try:
amount = int(input("Enter amount to deposit: "))
acc.depositAmount(amount)
print("Deposit successful")
except ValueError:
print("Invalid input. Please enter a numeric value.")
elif option == 2:
try:
amount = int(input("Enter amount to withdraw: "))
if amount <= acc.deposit:
acc.withdrawAmount(amount)
print("Withdrawal successful")
else:
print("Insu icient balance")
except ValueError:
print("Invalid input. Please enter a numeric value.")
break
else:
print("Account not found")
with open("accounts.data", 'wb') as outfile:
pickle.dump(accounts, outfile)
def deleteAccount(acc_no):
file = pathlib.Path("accounts.data")
if not file.exists():
print("No records to delete")
return
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
new_accounts = [acc for acc in accounts if acc.accNo != acc_no]
if len(new_accounts) == len(accounts):
print("Account not found")
else:
with open("accounts.data", 'wb') as outfile:
pickle.dump(new_accounts, outfile)
print("Account deleted")
def modifyAccount(acc_no):
file = pathlib.Path("accounts.data")
if not file.exists():
print("No records to modify")
return
with open(file, 'rb') as infile:
accounts = pickle.load(infile)
for acc in accounts:
if acc.accNo == acc_no:
acc.modify()
print("Account updated")
break
else:
print("Account not found")
with open("accounts.data", 'wb') as outfile:
pickle.dump(accounts, outfile)
def intro():
print("\n\t************")
print("\tBANK MANAGEMENT SYSTEM")
print("\t**********************\n")
# Main loop
if __name__ == "__main__":
intro()
while True:
print("\nMAIN MENU")
print("1. NEW ACCOUNT")
print("2. DEPOSIT AMOUNT")
print("3. WITHDRAW AMOUNT")
print("4. BALANCE ENQUIRY")
print("5. ALL ACCOUNT HOLDER LIST")
print("6. CLOSE AN ACCOUNT")
print("7. MODIFY AN ACCOUNT")
print("8. EXIT")
choice = input("Select Your Option (1-8): ")
if choice == '1':
acc = Account()
acc.createAccount()
writeAccountsFile(acc)
elif choice == '2':
num = int(input("Enter the account No.: "))
depositAndWithdraw(num, 1)
elif choice == '3':
num = int(input("Enter the account No.: "))
depositAndWithdraw(num, 2)
elif choice == '4':
num = int(input("Enter the account No.: "))
displaySp(num)
elif choice == '5':
displayAll()
elif choice == '6':
num = int(input("Enter the account No.: "))
deleteAccount(num)
elif choice == '7':
num = int(input("Enter the account No.: "))
modifyAccount(num)
elif choice == '8':
print("Thanks for using bank management system")
break
else:
print("Invalid choice. Try again.")