Advertisement
Zeinab_Hamdy

FinalCode

May 9th, 2023 (edited)
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.02 KB | None | 0 0
  1. def writeEmployee():  
  2.     with open('Employee.txt','a') as file :
  3.         c = 'y'
  4.         while c =='y' or c=='Y' :
  5.             Id=input('Enter id : ')
  6.             Name=input('Enter name : ')
  7.             file.write(Id+'\t'+Name+'\n')
  8.             c=input('Do you want to enter records again ? (y or n) ')
  9.  
  10.  
  11.  
  12. #*******************************************************************************#*******************************************************************************#
  13.  
  14.            
  15.            
  16. def readEmployee():
  17.     with open('Employee.txt','r') as file :
  18.         print('ID\tName')
  19.         print('------------------------')
  20.         for line in file:
  21.             print(line, end='')
  22.  
  23.  
  24.  
  25. #*******************************************************************************#*******************************************************************************#
  26.  
  27.  
  28.  
  29. def writeCustomer():
  30.     with open('Customer.txt','a') as file :
  31.         Name=input('Enter your  name : ')
  32.         Phone=input('Enter your phone number : ')
  33.         file.write(Name+'\t'+Phone+'\n')
  34.  
  35.  
  36.  
  37. #*******************************************************************************#*******************************************************************************#
  38.  
  39.        
  40.        
  41. def readCustomer():
  42.     with open('Customer.txt','r') as file :
  43.         print('Name\tPhoneNumber')
  44.         print('--------------------------')
  45.         for line in file:
  46.             print(line, end='')
  47.  
  48.  
  49.  
  50. #*******************************************************************************#*******************************************************************************#
  51.  
  52.            
  53.            
  54. def writeProduct(Employee_ID):
  55.     with open('Product.txt', 'a')as file:
  56.         c='y'
  57.         while c=='y' or c=='Y':
  58.             ID=input('Enter the product id: ')
  59.             Name=input('Enter the product name: ')
  60.             Price=input('Enter the product price: ')
  61.             file.write(ID+'\t'+Name+'\t'+Price+'\t'+Employee_ID+'\n')
  62.             c=input('Enter records of product again (y/n)? ')
  63.            
  64.  
  65.            
  66. #*******************************************************************************#*******************************************************************************#
  67.  
  68.  
  69.            
  70. def readProduct(): # with employeeId
  71.     with open('Product.txt', 'r')as file:
  72.         print('ID\tName\tPrice\tEmployee_ID')
  73.         print('-----------------------------------')
  74.         for line in file:
  75.             print(line , end='')
  76.  
  77.            
  78.  
  79. #*******************************************************************************#*******************************************************************************#
  80.  
  81.  
  82.  
  83. def readProduct2(): # without employeeId
  84.     with open('Product.txt', 'r')as file:
  85.         print('ID\tName\tPrice')
  86.         print('---------------------')
  87.         for line in file:
  88.              l=line.split('\t')
  89.              print(l[0] + '\t' + l[1] + '\t' + l[2])
  90.  
  91.  
  92.            
  93.  
  94.  
  95.  
  96.  
  97. #*******************************************************************************#*******************************************************************************#
  98.  
  99.  
  100.  
  101.            
  102. def searchProduct():
  103.     Name=input('Enter the name for the product to search: ')
  104.     found=False
  105.     with open('Product.txt', 'r') as file:
  106.         for line in file:
  107.             pr=line.split('\t')
  108.             if Name==pr[1]:
  109.                 found=True
  110.                 print('ID:',pr[0],'\tName:',pr[1],'\tPrice:',pr[2])
  111.         if not found:
  112.             print('\n"Sorry, The product is not found!"\n')
  113.  
  114.  
  115.  
  116.  
  117.  
  118. #*******************************************************************************#*******************************************************************************#
  119.  
  120.  
  121.        
  122.  
  123. def deleteProduct():
  124.     import os
  125.     ID=input('Enter the id for the product: ')
  126.     found=False
  127.     file=open('Product.txt', 'r')
  128.     tempProduct=open('TempProduct.txt', 'w')
  129.     for line in file:
  130.         pr=line.split('\t')
  131.         if ID==pr[0]:
  132.             found=True
  133.         else:
  134.             tempProduct.write(line)
  135.     file.close()
  136.     tempProduct.close()
  137.     os.remove('Product.txt')
  138.     os.rename('TempProduct.txt','Product.txt')
  139.     if found:
  140.         print('\n"Product record deleted successfuly"\n')
  141.     else :
  142.         print('\n"Sorry, The product is not found!"\n')
  143.  
  144.  
  145.  
  146.  
  147. #*******************************************************************************#*******************************************************************************#
  148.  
  149.  
  150.  
  151. def updateprice(EmployeeID):
  152.     import os
  153.     ID=input('Enter the ID of the product you want to update this price: ')
  154.     flag=False
  155.     file=open("Product.txt","r")
  156.     tempfile=open("TempProduct.txt","w")
  157.     for line in file:
  158.         pr=line.split('\t')
  159.         if ID==pr[0]:
  160.             flag =True
  161.             price=input('Enter the new price of the product: ')
  162.             line =pr[0]+'\t'+pr[1]+'\t'+price+'\t'+str(EmployeeID)+'\n'
  163.         tempfile.write(line)
  164.     file.close()
  165.     tempfile.close()
  166.     os.remove('Product.txt')
  167.     os.rename('TempProduct.txt','Product.txt')
  168.     if flag:
  169.        print("The price of product is sucessfully updated")
  170.        print("The employee_ID is sucessfully updated\n")
  171.     else :
  172.         print("No price_id matched to update!")
  173.  
  174.  
  175.  
  176.  
  177.        
  178.  
  179. #*******************************************************************************#*******************************************************************************#
  180.  
  181.  
  182.  
  183.  
  184.  
  185. def countCustomer(Phone):
  186.     with open('Customer.txt','r') as file :
  187.         counter=0
  188.         Phone+='\n'  # as cc[1] contain phone+'\n'
  189.         for line in file:
  190.             cc=line.split('\t')
  191.             if Phone==cc[1]:
  192.                 counter+=1
  193.     return counter
  194.  
  195.  
  196.  
  197.  
  198.  
  199. #*******************************************************************************#*******************************************************************************#
  200.  
  201.  
  202.  
  203.  
  204. def validLogIn( ID  , name ):
  205.     with open('Employee.txt', 'r') as file:
  206.         for line in file:
  207.             temp=line.strip()
  208.             l=temp.split('\t')
  209.             if l[0]==ID and l[1]==name :
  210.                return True;
  211.        
  212.         return False;
  213.  
  214.  
  215.  
  216.  
  217.  
  218. #*******************************************************************************#*******************************************************************************#
  219.  
  220.  
  221.  
  222.  
  223.  
  224. def validPhone( phone ):
  225.     valid=False;
  226.     for i in phone :
  227.         if i>='0' and  i<='9':
  228.             continue
  229.         else :
  230.             return False
  231.        
  232.     return True
  233.  
  234.  
  235. #*******************************************************************************#*******************************************************************************#
  236.  
  237.  
  238.  
  239.  
  240.  
  241. def menuEmpolyee(Id , Name) :
  242.     ch='Y'
  243.     while ch=='Y' or ch=='y' :
  244.         print('1) Add new Employees.')
  245.         print('2) Know all Employees.')
  246.         print('3) Know all products.')
  247.         print('4) Search about product by name.')
  248.         print('5) Delete product by it\'s id.')
  249.        
  250.         # op 6 and 7 need update autometically in field edited by
  251.         print('6) Update at price of product by it\'s id.')
  252.         print('7) Add new product.')
  253.         print('\n\"press any key to exit\"')
  254.         op=int(input('\n\nEnter your choice : \n'))
  255.  
  256.        
  257.         if  op== 1 :
  258.             writeEmployee()
  259.         elif op==2 :
  260.             readEmployee()
  261.         elif op==3 :
  262.             readProduct()
  263.         elif op==4 :
  264.             searchProduct()
  265.         elif op==5 :
  266.             deleteProduct()
  267.         elif op==6 :
  268.             updateprice(Id)
  269.         elif op==7 :
  270.             writeProduct(Id)
  271.         else :
  272.             return
  273.  
  274.         ch=input('\nIf you want to continue press [ Y / N ] : ')
  275.  
  276.        
  277.        
  278.  
  279.  
  280. #*******************************************************************************#*******************************************************************************#
  281.  
  282.        
  283.  
  284.  
  285.  
  286. def  menuCustomer(Name , Phone):
  287.      ch='Y' ; totalPrice =0;
  288.      count_visited = countCustomer(Phone)
  289.      with open("Sales.txt" ,'w' ) as sale:
  290.           while ch=='Y' or ch=='y' :
  291.              
  292.                print('\n1) Know all products to buy from them.')
  293.                print('2) Search about product by name.')
  294.                print('\nNote : ')
  295.                print(' *if you want to buy any product please call \'Know all products\'.')
  296.                print(' *if you want to exit press any key.')
  297.                opt= int(input('\n\nEnter your choice : \t\n'))
  298.  
  299.                
  300.                if opt== 1:
  301.                    readProduct2()
  302.                    wantedID=input('\nEnter id of the needed product : ')
  303.                    quantity=input('Enter the quantity you need of the product : ')
  304.                    with open("Product.txt" ,'r' ) as file :
  305.                         for line in file :
  306.                             l = line.split('\t')
  307.                             if wantedID== l[0] :
  308.                                 sale.write(l[0] +'\t' + l[1] + '\t' + l[2] + '\t' +quantity +'\n')
  309.                                 totalPrice+=int(l[2])*int(quantity)
  310.                                 break
  311.  
  312.            
  313.                elif opt == 2 :
  314.                     searchProduct()
  315.    
  316.                else :
  317.                     break
  318.                ch= input('\nIf you want to continue press [ Y / N ] : ')
  319.  
  320.  
  321.  
  322.           # the customer buy products
  323.      if totalPrice!=0 :
  324.          print('\n\n**********************************************  Receipt  **********************************************\n')
  325.          print('Id\tName\tPrice\tQuantity')
  326.          with open("Sales.txt" ,'r' ) as sale:
  327.                  for line in sale :
  328.                      l = line.split('\t')
  329.                      print(l[0] +'\t' + l[1] + '\t' + l[2] + '\t' +l[3] , end='' )
  330.  
  331.              
  332.          print('\n\nTotal price : ' , totalPrice)
  333.          if(count_visited > 1) :
  334.              print('Number of visited our system : ', count_visited ,' times')
  335.              print('Discount : ' , (totalPrice * 5 //100 ) )
  336.              print('Total price after discount  : ', totalPrice - (totalPrice * 5 //100 ) )
  337.  
  338.  
  339.      print('\n\n***********************************   thanks for using our system   ***********************************')
  340.  
  341.  
  342.  
  343.  
  344.        
  345. #*******************************************************************************#*******************************************************************************#
  346.  
  347.      
  348.  
  349.      
  350. def Main():
  351.  
  352.     passWord='File'
  353.    
  354.     op1 = int(input('1) Employee.\n2) Customer.\nEnter Your choice : '))
  355.    
  356.     if op1 == 1 : # Employee
  357.         newPass=input('Enter the password for employees : ')
  358.         cnt=1
  359.        
  360.         while  newPass != passWord and  cnt < 3  :
  361.             newPass=input('Invalid password!\nplease try again : ')
  362.             cnt+=1
  363.            
  364.         if newPass !=passWord :
  365.           return
  366.        
  367.         print('\n*************  Welcome  *************\n')
  368.        
  369.         Id=input('Enter your id : ')
  370.         Name=input('Enter your  name : ')
  371.  
  372.  
  373.         # validate of id , name
  374.         while not validLogIn(Id , Name):
  375.                 print('Invalid log in! , please try again')
  376.                 Id=input('Enter your id : ')
  377.                 Name=input('Enter your  name : ')  
  378.                
  379.            
  380.            
  381.         print('\n******* logged in Successfully *******\n')
  382.        
  383.         menuEmpolyee(Id , Name)
  384.  
  385.  
  386.        
  387.     elif op1==2 : # Customer
  388.        
  389.        with open('Customer.txt','a') as File :
  390.            
  391.         Name=input('Enter your  name : ')
  392.         Phone=input('Enter your phone number : ')
  393.        
  394.         while not validPhone(Phone) :
  395.             print('Invalid Phone! , please try again')
  396.             Phone=input('Enter your phone number : ')
  397.            
  398.         File.write(Name+'\t'+Phone+'\n')
  399.        
  400.         menuCustomer(Name , Phone)
  401.  
  402.  
  403.  
  404.        
  405.     else :
  406.         return
  407.  
  408.  
  409. Main()
  410.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement