Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- class Flight:
- def __init__(self, flight_id=None, pilots=[], crew=[], departing_from=None, departure_date_time=None, destination=None, arrival_date_time=None, airplane=None):
- self.__flight_id = flight_id
- self.__pilots = pilots
- self.__crew = crew
- self.__departing_from = departing_from
- self.__departure_date_time = departure_date_time
- self.__destination = destination # id
- self.__arrival_date_time = arrival_date_time
- self.__airplane = airplane
- def calc_arrival_time(self):
- '''Calculates the arrival time of the flight'''
- distance = self.__destination.get_distance()
- ###### WARNING HARD CODED SPEED VALUE ######
- time = int(distance) / 900 # Average plane speed
- ## TO DO, GIVE AIRPLANES SPEEED UNITS ##
- # Changing the time into a datetime object
- self.__departure_date_time = datetime.datetime.strptime(self.__departure_date_time,"%Y-%m-%dT%H:%M")
- self.__arrival_date_time = self.__departure_date_time + datetime.timedelta(hours=time) # calculation arrival time
- return time # returns how long it takes to fly
- def is_valid(self):
- """Checks if the flight has all the necessary info for it to be allowed to proceed"""
- valid = True
- invalid_list = []
- if len(self.__pilots) < 2: # if flight has at least 2 pilots
- valid = False
- invalid_list.append("Missing pilot")
- if len(self.__crew) < 1: # if flight has at least 1 flight attendant
- valid = False
- invalid_list.append("Missing Flight Attendant")
- if not self.__airplane: # if airplane has not been assigned
- valid = False
- invalid_list.append("Missing Airplane")
- elif not self.__departing_from: # if flight has no departure location
- valid = False
- elif not self.__departure_date_time: # if flight has no departure time
- valid = False
- elif not self.__destination: # if flight has no destination
- valid = False
- elif not self.__flight_id: # if flight does not have an id
- valid = False
- elif not self.__arrival_date_time: # if flight has no arrival time
- valid = False
- return valid, invalid_list
- def edit(self, object_type, item):
- """Takes in an object type and a staff class object,
- passes it on to edit if the type is right"""
- if object_type == "Pilot" or object_type == "Crew":
- worked = self.edit_staff(object_type, item)
- return worked
- def edit_airplane(self, new_ID):
- """Sets a new airplane ID"""
- self.__airplane = new_ID
- def edit_options(self):
- '''Used by the ui to know what he can edit'''
- return ["Pilot", "Crew"]
- def edit_staff(self, staff_type, staff):
- ''' Used by the ui to add new staff members to an existing voyage'''
- staff_type = staff_type.lower()
- if staff_type == "pilot": # if type is pilot
- self.__pilots.append(staff) # adds pilot to the list of pilots
- else:
- self.__crew.append(staff) # adds flight attendants to crew list
- return True
- def edit_departure(self, new_departure):
- self.__departing_from = new_departure
- def edit_destination(self, new_destination):
- self.__destination = new_destination
- def edit_departure_time(self, new_time):
- '''Takes in a departure time from the user'''
- if len(new_time) == 16:
- if new_time[4] == '-' and new_time[7] == '-' and new_time[10] == ',' and new_time [13] == ':':
- temp_list = new_time.split(",")
- temp_str = "T".join(temp_list)
- self.__departure_date_time = temp_str # creates a time object
- time = self.calc_arrival_time() # gets the time the flight takes
- return (self.__arrival_date_time, time) # returns the time and arrival date
- return False
- def set_departure_time(self, new_time):
- """Sets departure time as new time"""
- self.__departure_date_time = new_time
- def set_arrival_time(self, time):
- """Sets arrival time as new time"""
- self.__arrival_date_time = time
- def set_id(self, flight_id):
- """Sets flight Id as given flight id"""
- self.__flight_id = flight_id
- def get_type(self):
- return "voyage"
- def get_date(self):
- return self.__departure_date_time
- def get_staff(self, staff_type = "all"):
- """Returns the list of pilots or crew or both based on what user wants."""
- staff_type = staff_type.lower()
- if staff_type == "pilot":
- return self.__pilots
- elif staff_type == "crew":
- return self.__crew
- elif staff_type == "all":
- return self.__pilots + self.__crew
- def get_destination(self):
- """Returns the flights destination."""
- if type(self.__destination) == str: # if the type is a string return it
- return self.__destination
- else: # else if the type is a class get the string
- return self.__destination.get_airport_id()
- def get_search_reference(self):
- """Returns the search reference to find the right line for datalayer to edit."""
- return "{},{},{},{},{}".format(self.__flight_id,self.__departing_from, self.__destination,self.__departure_date_time,self.__arrival_date_time)
- def save(self):
- '''Creates a string for the datalayer to save'''
- if self.__pilots:
- pilots_str = ";".join([pilot.get_ssn() for pilot in self.__pilots]) # joins the pilots, seperates by ;
- else:
- pilots_str = None
- if self.__crew:
- crew_str = ";".join([crew.get_ssn() for crew in self.__crew]) # joins the crew, seperates by ;
- else:
- crew_str = None
- departure_time_formatted = str(self.__departure_date_time).replace(" ", "T") # making sure the time is formatted correctly
- arrival_time_formatted = str(self.__arrival_date_time).replace(" ", "T") # making sure the time is formatted correctly
- if type(self.__departing_from) != str:
- self.__departing_from = self.__departing_from.get_airport_id()
- if type(self.__destination) != str:
- self.__destination = self.__destination.get_airport_id()
- validity = self.is_valid() # check if flight is fully manned and contains all necessary info
- return "{},{},{},{},{},{},{},{},{}".format(self.__flight_id, self.__departing_from, self.__destination, departure_time_formatted, \
- arrival_time_formatted, self.__airplane,pilots_str, crew_str, validity[0])
- def display_str(self):
- '''Used by the ui to display the string'''
- depart_str = str(self.__departure_date_time[:-3]).replace("T", " ") # Swap T between date and time with space to make more readable
- arriv_str = str(self.__arrival_date_time[:-3]).replace("T", " ") # Swap T between date and time with space to make more readable
- 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