Advertisement
Guest User

Untitled

a guest
May 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. # -------------------------------------------------#
  2. # Title: Adding a class structure to a script
  3. # Dev:   Craig Lewin
  4. # Date:  May 20, 2019
  5. # ChangeLog: (Who, When, What)
  6. #   Craig Lewin, 05/20/2019, Began coding for assignment 8
  7. # -------------------------------------------------#
  8.  
  9.  
  10. class Product(object):
  11.     """Doc String"""
  12.     # ---------------------------------------#
  13.     # Desc: Manipulates product data
  14.     # Dev: Craig Lewin
  15.     # Date: 5/20/2019
  16.     # ChangeLog: (When, Who, What)
  17.     # - 5/20/2019, CLewin, Wrote class
  18.     # ---------------------------------------#
  19.  
  20.     # --Fields--
  21.     strUserInput = None  # A string which holds user input
  22.  
  23.     # --Constructor--
  24.         # Attributes
  25.  
  26.     # --Properties--
  27.  
  28.     # --Methods--
  29.     def WriteProductUserInput(File):
  30.         try:
  31.             print("Type in a Product Id, Name, and Price you want to add to the file")
  32.             print("(Enter 'Exit' to quit!)")
  33.             while (True):
  34.                 strUserInput = input("Enter the Id, Name, and Price (ex. 1,ProductA,9.99): ")
  35.                 if (strUserInput.lower() == "exit"):
  36.                     break
  37.                 else:
  38.                     File.write(strUserInput + "\n")
  39.         except Exception as e:
  40.             print("Error: " + str(e))
  41.  
  42. # --End of Product class--
  43.  
  44.  
  45. class FileIO(object):
  46.     """Doc String"""
  47.     # ---------------------------------------#
  48.     # Desc: Interacts with file objects for reading/writing
  49.     # Dev: Craig Lewin
  50.     # Date: 5/20/2019
  51.     # ChangeLog: (When, Who, What)
  52.     # - 5/20/2019, CLewin, Wrote class
  53.     # ---------------------------------------#
  54.  
  55.     # --Fields--
  56.     objFile = None  # File Handle
  57.  
  58.     # --Constructor--
  59.         # Attributes
  60.  
  61.     # --Properties--
  62.  
  63.     # --Methods--
  64.     def ReadAllFileData(File, Message="Contents of File"):
  65.         try:
  66.             print(Message)
  67.             File.seek(0)
  68.             print(File.read())
  69.         except Exception as e:
  70.             print("Error: " + str(e))
  71.  
  72. # --End of FileIO class--
  73.  
  74. try:
  75.   objFile = open("Products.txt", "r+")
  76.   FileIO.ReadAllFileData(objFile, "Here is the current data:")
  77.   Product.WriteProductUserInput(objFile)
  78.   FileIO.ReadAllFileData(objFile, "Here is this data was saved:")
  79. except FileNotFoundError as e:
  80.      print("Error: " + str(e) + "\n Please check the file name")
  81. except Exception as e:
  82.     print("Error: " + str(e))
  83. finally:
  84.   if(objFile != None):objFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement