I would really appreiciat some help figureing out how to code these. Train class
ID: 3831675 • Letter: I
Question
I would really appreiciat some help figureing out how to code these.
Train class: Instance variables: string the name of the train name max passengers int the maximum number of passengers the train can transport num passengers nt the number of passengers currently on the train nt how fast the train travels (in feet per second) speed fps Methods: init (self, name, max passengers, speed fps) constructor, initializes all instance variables, num passengers should default to 0 when a train is created with the constructor. o Assumptions speed fps and max passengers will always be anon-negative number name will be a non-empty stringExplanation / Answer
import math
class TrainCapacityException(Exception):
def __init__(self, number, issue="full"):
self.number = number
self.issue = issue
def __str__(self):
if self.issue == "full":
return str(self.number) + " passengers cannot be loaded because the train is full!"
else:
return str(self.number) + " passengers cannot be unloaded because the train is empty!"
class Train:
def __init__(self, name, max_passengers, speed_fps):
self.name = name
self.max_passengers = max_passengers
self.num_passengers = 0
self.speed_fps = speed_fps
def __str__(self):
train = "Train named " + self.name + " with " + str(self.num_passengers)
speed_mps = 0.681818*self.speed_fps
train += " passengers will travel at " + "{0:.2f}".format(speed_mps) + "mph"
return train
def time_to_travel(self, distance_feet):
return distance_feet//self.speed_fps
def load_passengers(self, num_people):
if self.num_passengers + num_people <= self.max_passengers:
self.num_passengers += num_people
else:
over_capacity = (self.num_passengers + num_people) - self.max_passengers
raise TrainCapacityException(over_capacity)
def unload_passengers(self, num_people):
if self.num_passengers - num_people >= 0:
self.num_passengers -= num_people
else:
under_capacity = num_people - self.num_passengers
raise TrainCapacityException(under_capacity, "empty")
class City:
def __init__(self, name, loc_x, loc_y, stop_time):
self.name = name
self.loc_x = loc_x
self.loc_y = loc_y
self.stop_time = stop_time
def __str__(self):
result = self.name + " (" + str(self.loc_x) + "," + str(self.loc_y) + "). "
minutes = self.stop_time/60.0
result += "Exchange time: " + "{0:.2f}".format(minutes) + " minutes"
return result
def __eq__(self, other):
return (self.loc_x == other.loc_x) and (self.loc_y == other.loc_y)
def distance_to_city(self, city):
distance = math.sqrt((self.loc_x - city.loc_x)**2 + (self.loc_y - city.loc_y)**2)
return distance
class Journey:
def __init__(self, train, destinations=None, start_time=0):
if not isinstance(train, Train):
raise TypeError("train should be instance of Train")
self.train = train
if not destinations:
self.destinations = []
else:
if not type(destinations) is list:
raise TypeError("destinations should be list")
for city in destinations:
if not isinstance(city, City):
raise TypeError("city should be instance of City")
self.destinations = destinations
self.start_time = start_time
def city_departure_time(self, city):
if not city in self.destinations:
return
time = self.start_time
departure_time = time
for i in range(1, len(self.destinations)):
time = time + self.destinations[i-1].stop_time
if city == self.destinations[i-1]:
departure_time = time
time = time +int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps)
if self.destinations[-1] == city:
departure_time = time + city.stop_time
return departure_time
def city_arrival_time(self, city):
if not city in self.destinations:
return
time = self.start_time
arrival_time = time
for i in range(1, len(self.destinations)):
if self.destinations[i-1] == city:
return arrival_time
arrival_time = arrival_time + int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps) + self.destinations[i-1].stop_time
if self.destinations[-1] == city:
return arrival_time
return
def add_destination(self,c):
self.destinations.append(c)
def city_in_journey(self,c):
return c in self.destinations
def check_journey_includes(self, start_city, dest_city):
found_start = False
for dest in self.destinations:
if found_start and dest == dest_city:
return True
if dest==start_city:
found_start = True
return False
def total_journey_time(self):
if not self.destinations:
return 0
return self.city_departure_time(self.destinations[-1]) - self.start_time
def total_journey_distance(self):
if len(self.destinations) < 2:
return 0
distance = 0
for i in range(1, len(self.destinations)):
distance += self.destinations[i].distance_to_city(self.destinations[i-1])
return distance
def all_passengers_accommodated(self, unload_list, load_list):
for i in range(0, len(self.destinations)):
if unload_list[i] != 0:
try:
self.train.unload_passengers(unload_list[i])
except:
return False
if load_list[i] != 0:
try:
self.train.load_passengers(load_list[i])
except:
return False
return True
def __str__(self):
result = "Journey with " + str(len(self.destinations)) + " stops: "
for destination in self.destinations:
result += " " + str(destination) + " "
result += "Train Information: " + str(self.train) + " "
return result
# pastebin link for code : https://pastebin.com/rc817r8D