import pickle
import os
def sortacc(F):
try:
with open(F, "rb") as f:
r = [Link](f)
[Link](key=lambda r: r[0])
with open(F, "wb") as f:
[Link](r, f)
except (EOFError, FileNotFoundError):
print(F, "file has no record")
def addr(F):
try:
if [Link](F) and [Link](F) > 0:
with open(F, "rb") as f:
r1 = [Link](f)
else:
r1 = []
while True:
try:
a = int(input("Enter account no: "))
# check if account already exists
if any(acc[0] == a for acc in r1):
print("Account number already exists! Try again.")
continue
n = input("Enter name: ")
ad = input("Enter address: ")
p = input("Enter phone no: ")
b = float(input("Enter balance: "))
if b < 5000:
print("Minimum balance should be 5000!")
continue
rec = [a, [Link](), [Link](), p, b]
[Link](rec)
except ValueError:
print("Invalid input, please try again.")
continue
ch = input("Enter more records? (y/n): ")
if [Link]() == 'n':
break
with open(F, "wb") as f:
[Link](r1, f)
print("Records added successfully!")
except Exception as e:
print("Error while adding records:", e)
def displayr(F):
try:
with open(F, "rb") as f:
r = [Link](f)
print("*" * 70)
print(f"{'Accno':<10}{'Name':<15}{'Address':<20}{'Phone':<15}
{'Balance':<10}")
print("*" * 70)
for rec in r:
print(f"{rec[0]:<10}{rec[1]:<15}{rec[2]:<20}{rec[3]:<15}
{rec[4]:<10.2f}")
print("*" * 70)
print("Records read:", len(r))
print("*" * 70)
except (EOFError, FileNotFoundError):
print("No records found in", F)
def updater(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = int(input("Enter the account no for updation: "))
found = False
for i in r:
if i[0] == cj:
found = True
if input("Change name (y/n)? ").lower() == 'y':
i[1] = input("Enter name: ").upper()
if input("Change address (y/n)? ").lower() == 'y':
i[2] = input("Enter address: ").upper()
if input("Change phone no (y/n)? ").lower() == 'y':
i[3] = input("Enter phone no: ")
if input("Change balance (y/n)? ").lower() == 'y':
i[4] = float(input("Enter balance: "))
break
if not found:
print("Account no not found")
with open(F, "wb") as f:
[Link](r, f)
except (EOFError, FileNotFoundError):
print("No records found in", F)
def deleter(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = int(input("Enter the account no for deletion: "))
for i in range(len(r)):
if r[i][0] == cj:
print("Deleted record:", [Link](i))
break
else:
print("Record not found")
with open(F, "wb") as f:
[Link](r, f)
except (EOFError, FileNotFoundError):
print("No records found in", F)
def searchacc(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = int(input("Enter the account no to be searched: "))
for rec in r:
if rec[0] == cj:
print("*" * 70)
print(f"{'Accno':<10}{'Name':<15}{'Address':<20}{'Phone':<15}
{'Balance':<10}")
print("*" * 70)
print(f"{rec[0]:<10}{rec[1]:<15}{rec[2]:<20}{rec[3]:<15}
{rec[4]:<10.2f}")
return
print("Record not found")
except (EOFError, FileNotFoundError):
print("No records found in", F)
def searchname(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = input("Enter the customer name to be searched: ").upper()
for rec in r:
if rec[1] == cj:
print("*" * 70)
print(f"{'Accno':<10}{'Name':<15}{'Address':<20}{'Phone':<15}
{'Balance':<10}")
print("*" * 70)
print(f"{rec[0]:<10}{rec[1]:<15}{rec[2]:<20}{rec[3]:<15}
{rec[4]:<10.2f}")
return
print("Record not found")
except (EOFError, FileNotFoundError):
print("No records found in", F)
def debitamt(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = int(input("Enter the account no to withdraw money: "))
for rec in r:
if rec[0] == cj:
am = float(input("Enter the amount to be withdrawn: "))
if rec[4] - am >= 5000:
rec[4] -= am
print("Amount debited successfully")
else:
print("There must be a minimum balance of Rs. 5000")
break
else:
print("Record not found")
with open(F, "wb") as f:
[Link](r, f)
except (EOFError, FileNotFoundError):
print("No records found in", F)
def creditamt(F):
try:
with open(F, "rb") as f:
r = [Link](f)
cj = int(input("Enter the account no to credit money: "))
for rec in r:
if rec[0] == cj:
am = float(input("Enter the amount to be added: "))
rec[4] += am
print("Amount credited successfully")
break
else:
print("Record not found")
with open(F, "wb") as f:
[Link](r, f)
except (EOFError, FileNotFoundError):
print("No records found in", F)
def menu():
print("*" * 70)
print("1. Add records")
print("2. Display records")
print("3. Delete record")
print("4. Update record")
print("5. Search record by account no")
print("6. Search record by name")
print("7. Debit / Withdraw amount")
print("8. Credit amount")
print("9. Exit")
print("*" * 70)
# Main program
WA = "[Link]"
while True:
menu()
try:
ch = int(input("Enter your choice: "))
except ValueError:
print("Please enter a number between 1 and 9")
continue
if ch == 1:
addr(WA)
elif ch == 2:
sortacc(WA)
displayr(WA)
elif ch == 3:
deleter(WA)
elif ch == 4:
updater(WA)
elif ch == 5:
searchacc(WA)
elif ch == 6:
searchname(WA)
elif ch == 7:
debitamt(WA)
elif ch == 8:
creditamt(WA)
elif ch == 9:
print("Exiting....")
break
else:
print("Enter values from 1 to 9 only")