semsem_elazazy

Bank management system (python)

May 13th, 2023 (edited)
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.44 KB | None | 0 0
  1. #------------------------------------------------------------------------------
  2. #find last id in file
  3. def lastId():
  4.     l = 0
  5.     with open("project1.txt", "r+") as file:
  6.         for line in file:
  7.             l = line.split()[0]
  8.     return int(l)
  9.  
  10. # writing a record to a file
  11. def writeToFile():
  12.    
  13.     with open("project1.txt", 'a') as file:
  14.         Id = str(lastId() + 1)
  15.         name = input("enter your name : ")
  16.         age = input("enter age :  ")
  17.         balance = input("Enter the amount of your balance : ")
  18.         while int(balance) < 500 :
  19.             print("Sorry you can't create an account :( \n Your balance can't be less than <<500>> \n Put another maount  .... )")
  20.             balance = input("Enter the amount of your balance : ")
  21.         file.write(Id + '\t' + name + '\t' + age + '\t' +balance+'\n')
  22.         print("Record added sucessfully  :) ")
  23.  
  24. #------------------------------------------------------------------------------------------------
  25. #read all data from a file
  26. def readFromFile():
  27.     flag = False
  28.     try:
  29.         with open("project1.txt", "r") as file:
  30.             print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  31.             print("ID\tName\tAge\tBalance")
  32.             print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  33.             for line in file:
  34.                 flag = True
  35.                 print(line, end="")
  36.                 print("--------------------------------")
  37.     except:
  38.         print("File not found")
  39.     if flag == False:
  40.         print("No data found")    
  41. #---------------------------------------------------------------------------                
  42. #Update a record
  43. def UpdateToFile():
  44.     import os
  45.     flag = False
  46.     flag2=False
  47.     tmp = open("tmp.txt", "w")
  48.     with open("project1.txt", "r") as file:
  49.         Id = input("Enter your id : ")
  50.         for line in file:
  51.             if line.startswith(Id):
  52.                 print("Your Data :")
  53.                 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  54.                 print("ID\tName\tAge\tBalance")
  55.                 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  56.                 print(line, end="")
  57.                 print("----------------------------------------")
  58.                 flag=True
  59.  
  60.                 ch = input("Do you want to update your name ? y/n ")
  61.                 if ch == 'y':
  62.                     name = input("Enter your new name : ")
  63.                     flag2 = True
  64.                 else:
  65.                     name = line.split()[1]
  66.                 ch = input("Do you want to update your age ? y/n ")
  67.                 if ch == 'y':
  68.                     age = input("Enter your new age : ")
  69.                     flag2 = True
  70.                 else:
  71.                     age = line.split()[2]
  72.                 l = line.split()
  73.                 l[1] = name
  74.                 l[2] = age
  75.  
  76.                 tmp.write(l[0] + '\t' + l[1] + '\t' + l[2] + '\t' + l[3] + '\n' )
  77.             else:
  78.                 tmp.write(line)
  79.     tmp.close()
  80.     os.remove("project1.txt")
  81.     os.rename("tmp.txt", "project1.txt")
  82.     if flag == False:
  83.         print("There is no data for this id")
  84.     elif flag2==False:
  85.         print("There is no changes")
  86.     else:
  87.         print("your data updated successfully ")
  88. #---------------------------------------------------------------------------
  89. #delete a record
  90. def DeleteRecordFromFile():
  91.     import os
  92.     id = input("Enter id for deleteing : ")
  93.     file = open("project1.txt",'r')
  94.     tempFile = open("temp.txt",'w')
  95.     flag = False
  96.     for line in file:
  97.         l = line.split('\t')
  98.         if id != l[0]:
  99.             tempFile.write(line)
  100.         else:
  101.             flag = True
  102.     file.close()
  103.     tempFile.close()
  104.     os.remove("project1.txt")
  105.     os.rename("temp.txt","project1.txt")
  106.     if not flag:
  107.         print("record does not exit in the file!!!")
  108.     else:
  109.         print("record was deleted successfully!!")
  110. #---------------------------------------------------------------------------
  111. # search and show specific record --> by name
  112. def search():
  113.     name=input("Enter the name of Customer:\n")
  114.     with open ("project1.txt","r")as f:
  115.         flag=False
  116.         for line in f:
  117.             st=line.split("\t")
  118.             if st[1]==name:
  119.                 flag=True
  120.                 print("ID     :",st[0])
  121.                 print("Name   :",st[1])
  122.                 print("Age    :",st[2])
  123.                 print("Balance:",st[3])
  124.         if not flag:
  125.             print("Account not found!\n")
  126.             return False
  127.         else:
  128.             return True
  129. #--------------------------------------------------------------------------
  130. def dep():
  131.     import os
  132.     flag = search()
  133.     if flag == False:
  134.         return
  135.     id = input("Enter ID : ")
  136.     print("---------------------------------------")
  137.     amt=input(" Enter amount to be deposited  : ")
  138.  
  139.     flag = False
  140.     tmp = open("tmp.txt", "w")
  141.     with open("project1.txt", "r") as file:
  142.         for line in file:
  143.             l = line.split('\t')
  144.             if id == l[0]:
  145.                 flag = True
  146.                 iD = l[0]
  147.                 namee = l[1]
  148.                 lastbal = l[3]
  149.                 l[3] = int(l[3]) + int(amt)
  150.                 newbal = l[3]
  151.                 tmp.write(l[0] + '\t' + l[1] + '\t' + l[2] + '\t' + str(l[3]) + '\n')
  152.             else:
  153.                 tmp.write(line)
  154.    
  155.     tmp.close()
  156.     os.remove("project1.txt")
  157.     os.rename("tmp.txt", "project1.txt")
  158.  
  159.     if flag == False:
  160.         print("Your id not found")
  161.     else:
  162.         print("your Balance updated successfully ")
  163.     report(iD , namee , lastbal , newbal)
  164. #---------------------------------------------------------------------------
  165. def withdraw():
  166.     import os
  167.     flag=search()
  168.     if flag==False:
  169.         return
  170.     id = input("Enter ID : ")
  171.     flag = False
  172.     tmp = open("tmp.txt", "w")
  173.     with open("project1.txt", "r") as file:
  174.         for line in file:
  175.             l = line.split('\t')
  176.             if id == l[0]:
  177.                 while flag == False :
  178.                     print("---------------------------------------")
  179.                     amt = input(" Enter amount to be Withdrawed :  \n")
  180.  
  181.                     if (int(l[3]) - int(amt) < 500 ) :
  182.                         print("Sorry you can't withdraw this amount :( \n Your balance can't be less than <<500>> \n Put another maount  .... )")
  183.                     else :
  184.                         flag = True
  185.                         iD = l[0]
  186.                         namee = l[1]
  187.                         lastbal = l[3]
  188.                         l[3] = int(l[3]) - int(amt)
  189.                         newbal = l[3]
  190.                         tmp.write(l[0] + '\t' + l[1] + '\t' + l[2] + '\t' + str(l[3]) + '\n')
  191.             else:
  192.                 tmp.write(line)
  193.  
  194.     tmp.close()
  195.     os.remove("project1.txt")
  196.     os.rename("tmp.txt", "project1.txt")
  197.  
  198.     if flag == False:
  199.         print("Your id not found")
  200.     else:
  201.         print("your Balance updated successfully ")
  202.     report(iD , namee , lastbal , newbal)
  203. #---------------------------------------------------------------------------
  204. def report(iD , namee , lastbal , newbal ) :
  205.     import datetime
  206.     print("<< Employee Data  >>")
  207.     employee_name  = input(" Emolyee Name  : ")
  208.     with open("report.txt" ,'a') as file :
  209.         lastbal = int(lastbal)
  210.         newbal = int(newbal)
  211.         localdt = datetime.datetime.now()
  212.         file.write(str(iD) +'\t\t'+  str(namee) + '\t\t' + str(lastbal) +'\t\t'+ str(newbal) +'\t\t'+ str(employee_name) +'\t\t'+ str(localdt) +'\n')
  213.         print("\naddead to report succefully ^o^  ")
  214.        
  215. def readReport():
  216.  
  217.      id = input("Enter the customer id  : ")
  218.      print("\n--------------")
  219.      print("| Islamic Bank |")
  220.      print(  "--------------")
  221.      print("-------------------------------------------------------------------------------------------------------------")
  222.      print("Customer ID\tCustomer Name\tLast Balance\tNewBalance\tEmployee Name\tDate ")
  223.      print("-------------------------------------------------------------------------------------------------------------")
  224.  
  225.      with open("report.txt", "r") as file:
  226.           for line in file:
  227.             if line.startswith(id):
  228.                 print(line, end="")
  229.                 print("-------------------------------------------------------------------------------------------------------------")      
  230. #---------------------------------------------------------------------------
  231. # main function            
  232. def main():
  233.         c='y'
  234.         while(c=='y'):
  235.             print("---------------------------------------------------------")
  236.             print("|              H  e   l   l   o    In                   |")
  237.             print("|          Bank      Mangement      System              |")
  238.             print("---------------------------------------------------------")
  239.             print("\n")
  240.             print("******************  MAIN  MENU  ******************")
  241.             print("            -----------------------------")
  242.             print("            | 1 | Create  Account       |")
  243.             print("            -----------------------------")
  244.             print("            | 2 | Deposite              |")
  245.             print("            -----------------------------")
  246.             print("            | 3 | Withdraw              |")
  247.             print("            -----------------------------")
  248.             print("            | 4 | Search for Acount     |")
  249.             print("            -----------------------------")
  250.             print("            | 5 | Delete Your Acount    |")
  251.             print("            -----------------------------")
  252.             print("            | 6 | Update Your Account   |")
  253.             print("            -----------------------------")
  254.             print("            | 7 | Show all Accounts     |")
  255.             print("            -----------------------------")
  256.             print("            | 8 | Show a report         |")
  257.             print("            -----------------------------")
  258.  
  259.             L = [writeToFile,dep,withdraw,search,DeleteRecordFromFile,UpdateToFile,readFromFile ,readReport]
  260.             op=input("\nEnter Number Of Transaction You Want: ")
  261.             try:
  262.                 op = int(op)
  263.                 L[op - 1]()
  264.             except:
  265.                 print("Invalid input")
  266.  
  267.             c=input("\nDo you want to continue?y/n.\n")
  268.             if c=='n':
  269.                 print("Thank you for using our system :)")
  270. main()
Add Comment
Please, Sign In to add comment