Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. import datetime
  2.  
  3. class Flight:
  4.  
  5. def __init__(self, flight_id=None, pilots=[], crew=[], departing_from=None, departure_date_time=None, destination=None, arrival_date_time=None, airplane=None):
  6. self.__flight_id = flight_id
  7. self.__pilots = pilots
  8. self.__crew = crew
  9. self.__departing_from = departing_from
  10. self.__departure_date_time = departure_date_time
  11. self.__destination = destination # id
  12. self.__arrival_date_time = arrival_date_time
  13. self.__airplane = airplane
  14.  
  15.  
  16. def calc_arrival_time(self):
  17. '''Calculates the arrival time of the flight'''
  18. distance = self.__destination.get_distance()
  19. ###### WARNING HARD CODED SPEED VALUE ######
  20. time = int(distance) / 900 # Average plane speed
  21. ## TO DO, GIVE AIRPLANES SPEEED UNITS ##
  22. # Changing the time into a datetime object
  23. self.__departure_date_time = datetime.datetime.strptime(self.__departure_date_time,"%Y-%m-%dT%H:%M")
  24. self.__arrival_date_time = self.__departure_date_time + datetime.timedelta(hours=time) # calculation arrival time
  25. return time # returns how long it takes to fly
  26.  
  27.  
  28. def is_valid(self):
  29. """Checks if the flight has all the necessary info for it to be allowed to proceed"""
  30. valid = True
  31. invalid_list = []
  32.  
  33. if len(self.__pilots) < 2: # if flight has at least 2 pilots
  34. valid = False
  35. invalid_list.append("Missing pilot")
  36. if len(self.__crew) < 1: # if flight has at least 1 flight attendant
  37. valid = False
  38. invalid_list.append("Missing Flight Attendant")
  39. if not self.__airplane: # if airplane has not been assigned
  40. valid = False
  41. invalid_list.append("Missing Airplane")
  42. elif not self.__departing_from: # if flight has no departure location
  43. valid = False
  44. elif not self.__departure_date_time: # if flight has no departure time
  45. valid = False
  46. elif not self.__destination: # if flight has no destination
  47. valid = False
  48. elif not self.__flight_id: # if flight does not have an id
  49. valid = False
  50. elif not self.__arrival_date_time: # if flight has no arrival time
  51. valid = False
  52.  
  53. return valid, invalid_list
  54.  
  55.  
  56. def edit(self, object_type, item):
  57. """Takes in an object type and a staff class object,
  58. passes it on to edit if the type is right"""
  59. if object_type == "Pilot" or object_type == "Crew":
  60. worked = self.edit_staff(object_type, item)
  61. return worked
  62.  
  63. def edit_airplane(self, new_ID):
  64. """Sets a new airplane ID"""
  65. self.__airplane = new_ID
  66.  
  67. def edit_options(self):
  68. '''Used by the ui to know what he can edit'''
  69. return ["Pilot", "Crew"]
  70.  
  71. def edit_staff(self, staff_type, staff):
  72. ''' Used by the ui to add new staff members to an existing voyage'''
  73. staff_type = staff_type.lower()
  74. if staff_type == "pilot": # if type is pilot
  75. self.__pilots.append(staff) # adds pilot to the list of pilots
  76. else:
  77. self.__crew.append(staff) # adds flight attendants to crew list
  78. return True
  79.  
  80. def edit_departure(self, new_departure):
  81. self.__departing_from = new_departure
  82.  
  83. def edit_destination(self, new_destination):
  84. self.__destination = new_destination
  85.  
  86. def edit_departure_time(self, new_time):
  87. '''Takes in a departure time from the user'''
  88. if len(new_time) == 16:
  89. if new_time[4] == '-' and new_time[7] == '-' and new_time[10] == ',' and new_time [13] == ':':
  90. temp_list = new_time.split(",")
  91. temp_str = "T".join(temp_list)
  92. self.__departure_date_time = temp_str # creates a time object
  93. time = self.calc_arrival_time() # gets the time the flight takes
  94. return (self.__arrival_date_time, time) # returns the time and arrival date
  95. return False
  96.  
  97. def set_departure_time(self, new_time):
  98. """Sets departure time as new time"""
  99. self.__departure_date_time = new_time
  100.  
  101. def set_arrival_time(self, time):
  102. """Sets arrival time as new time"""
  103. self.__arrival_date_time = time
  104.  
  105. def set_id(self, flight_id):
  106. """Sets flight Id as given flight id"""
  107. self.__flight_id = flight_id
  108.  
  109. def get_type(self):
  110. return "voyage"
  111.  
  112. def get_date(self):
  113. return self.__departure_date_time
  114.  
  115. def get_staff(self, staff_type = "all"):
  116. """Returns the list of pilots or crew or both based on what user wants."""
  117. staff_type = staff_type.lower()
  118. if staff_type == "pilot":
  119. return self.__pilots
  120. elif staff_type == "crew":
  121. return self.__crew
  122. elif staff_type == "all":
  123. return self.__pilots + self.__crew
  124.  
  125. def get_destination(self):
  126. """Returns the flights destination."""
  127. if type(self.__destination) == str: # if the type is a string return it
  128. return self.__destination
  129. else: # else if the type is a class get the string
  130. return self.__destination.get_airport_id()
  131.  
  132. def get_search_reference(self):
  133. """Returns the search reference to find the right line for datalayer to edit."""
  134. return "{},{},{},{},{}".format(self.__flight_id,self.__departing_from, self.__destination,self.__departure_date_time,self.__arrival_date_time)
  135.  
  136.  
  137. def save(self):
  138. '''Creates a string for the datalayer to save'''
  139. if self.__pilots:
  140. pilots_str = ";".join([pilot.get_ssn() for pilot in self.__pilots]) # joins the pilots, seperates by ;
  141. else:
  142. pilots_str = None
  143. if self.__crew:
  144. crew_str = ";".join([crew.get_ssn() for crew in self.__crew]) # joins the crew, seperates by ;
  145. else:
  146. crew_str = None
  147.  
  148. departure_time_formatted = str(self.__departure_date_time).replace(" ", "T") # making sure the time is formatted correctly
  149. arrival_time_formatted = str(self.__arrival_date_time).replace(" ", "T") # making sure the time is formatted correctly
  150.  
  151. if type(self.__departing_from) != str:
  152. self.__departing_from = self.__departing_from.get_airport_id()
  153.  
  154. if type(self.__destination) != str:
  155. self.__destination = self.__destination.get_airport_id()
  156.  
  157. validity = self.is_valid() # check if flight is fully manned and contains all necessary info
  158. return "{},{},{},{},{},{},{},{},{}".format(self.__flight_id, self.__departing_from, self.__destination, departure_time_formatted, \
  159. arrival_time_formatted, self.__airplane,pilots_str, crew_str, validity[0])
  160.  
  161. def display_str(self):
  162. '''Used by the ui to display the string'''
  163. depart_str = str(self.__departure_date_time[:-3]).replace("T", " ") # Swap T between date and time with space to make more readable
  164. arriv_str = str(self.__arrival_date_time[:-3]).replace("T", " ") # Swap T between date and time with space to make more readable
  165. return '{:8}{:5}{:5}{:15} --> {:15}'.format(self.__flight_id, self.__departing_from, self.__destination, depart_str, arriv_str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement