Bassel_11

Untitled

May 16th, 2023
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. def writeToFile():
  2.     try:
  3.         with open("passengers.txt", "a") as file:
  4.             c = 'y'
  5.             while c == 'y':
  6.                 id=input("enter your ID : ")
  7.                 name=input("enter your Name : ")
  8.                 age=input("enter your Age : ")
  9.                 destination=input("enter your Destination : ")
  10.                 file.write(id + '\t' + name + '\t' + age + '\t' + destination + '\n')
  11.                 c = input("Do you want to enter a new record ( y / n) ? ")
  12.             print("record added successfully")
  13.     except Exception as e:
  14.         print("Error occurred while writing to file:", e)
  15.  
  16. def readFromFile():
  17.     try:
  18.         print("ID\tName\tAge\tDestination")
  19.         print("----------------------------")
  20.         with open("passengers.txt", "r") as file:
  21.             for line in file:
  22.                 print(line,end="")
  23.     except Exception as e:
  24.         print("Error occurred while reading from file:", e)
  25.  
  26. def SearchById():
  27.     try:
  28.         id = input("enter Id to search : ")
  29.         flag = False
  30.         with open("passengers.txt", "r") as file:
  31.             for line in file:
  32.                 l  = line.split('\t')
  33.                 if l[0]==id:
  34.                     flag=True
  35.                     print("ID\tName\tAge\tDestination")
  36.                     print("----------------------------")
  37.                     print(line)
  38.                     return
  39.         if not flag:
  40.             print("record not found")
  41.     except Exception as e:
  42.         print("Error occurred while searching for record:", e)
  43.  
  44. def deleteRecord():
  45.     try:
  46.         import os
  47.         id = input("enter Id to delete : ")
  48.         file = open("passengers.txt", "r")
  49.         tempfile = open("tmp.txt", "w")
  50.         flag = False
  51.         for line in file:
  52.             l=line.split('\t')
  53.             if id==l[0]:
  54.                 flag = True
  55.             else:
  56.                 tempfile.write(line)
  57.         file.close()
  58.         tempfile.close()
  59.         os.remove("passengers.txt")
  60.         os.rename("tmp.txt", "passengers.txt")
  61.         if not flag:
  62.             print("record does not exist")
  63.         else:
  64.             print("the record deleted successfully")
  65.     except Exception as e:
  66.         print("Error occurred while deleting record:", e)
  67.  
  68. def updateRecord():
  69.     try:
  70.         import os
  71.         id = input("enter Id you want to update : ")
  72.         field = input("Which field do you want to update \n1 - ID\n2 - Name\n3 - Age\n4 - Destination\nYour choice :  ")
  73.         file = open("passengers.txt", "r")
  74.         tempfile = open("tmp.txt", "w")
  75.         flag = False
  76.         for line in file:
  77.             l = line.split('\t')
  78.             if id != l[0]:
  79.                 tempfile.write(line)
  80.             else:
  81.                 flag = True
  82.                 if field == "1":
  83.                     new_id = input("enter new ID : ")
  84.                     tempfile.write(new_id + '\t' + l[1] + '\t' + l[2] + '\t' + l[3])
  85.                 elif field == "2":
  86.                     new_name = input("enter new Name : ")
  87.                     tempfile.write(l[0] + '\t' + new_name + '\t' + l[2] + '\t' + l[3])
  88.                 elif field == "3":
  89.                     new_age = input("enter new Age : ")
  90.                     tempfile.write(l[0] + '\t' + l[1] + '\t' + new_age + '\t' + l[3])
  91.                 elif field == "4":
  92.                     new_dest = input("enter new Destination : ")
  93.                     tempfile.write(l[0] + '\t' + l[1] + '\t' + l[2] + '\t' + new_dest)
  94.         file.close()
  95.         tempfile.close()
  96.         os.remove("passengers.txt")
  97.         os.rename("tmp.txt", "passengers.txt")
  98.         if not flag:
  99.             print("record does not exist")
  100.         else:
  101.             print("record updated successfully")
  102.     except Exception as e:
  103.         print("Error occurred while updating record:", e)
  104.  
  105. def main():
  106.     while True:
  107.         try:
  108.             print("\n------------BUS RESERVATION SYSTEM---------------")
  109.             print("(Choose one of these to do)")
  110.             print("1 - Enter new passenger")
  111.             print("2 - View all passengers")
  112.             print("3 - Search for a passenger")
  113.             print("4 - Change passenger information")
  114.             print("5 - Delete a passenger")
  115.             print("6 - Exit")
  116.             ch = int(input("Your choice : "))
  117.             if ch == 1:
  118.                 writeToFile()
  119.             elif ch == 2:
  120.                 readFromFile()
  121.             elif ch == 3:
  122.                 SearchById()
  123.             elif ch == 4:
  124.                 updateRecord()
  125.             elif ch == 5:
  126.                 deleteRecord()
  127.             elif ch == 6:
  128.                 print("\n Thanks for using our System , we hope you like it :)")
  129.                 break
  130.             else:
  131.                 print("\n please choose one of the shown choices\n")
  132.         except Exception as e:
  133.             print("Error occurred:", e)
  134.  
  135. main()
Advertisement
Add Comment
Please, Sign In to add comment