Untitled
Anonymous
Plain Text
10/02/2025 11:57 AM
1.8 KB
21
Indexable
Never
import abc
# --- Abstract Base Class ---
class Transportation(abc.ABC):
def __init__(self, start_place, end_place, distance):
self.start_place = start_place
self.end_place = end_place
self.distance = distance # in km
@abc.abstractmethod
def find_cost(self):
# This method must be implemented by all concrete classes
pass
# --- Concrete Classes ---
class Walk(Transportation):
def __init__(self, start_place, end_place, distance):
super().__init__(start_place, end_place, distance)
def find_cost(self):
# Walk has no cost
return 0.0
class Taxi(Transportation):
# Taxi charges 40 Bahts per kilometer
def __init__(self, start_place, end_place, distance):
super().__init__(start_place, end_place, distance)
def find_cost(self):
# Cost = 40 Bahts * distance (in km)
return 40.0 * self.distance
class Train(Transportation):
# Train costs 5 Bahts for each station
def __init__(self, start_place, end_place, distance, num_stations):
super().__init__(start_place, end_place, distance)
self.num_stations = num_stations
def find_cost(self):
# Cost = 5 Bahts * number of stations
return 5.0 * self.num_stations
# --- Main Program (Testing Code from Image) ---
print("# main program")
# Walk(start, end, distance_km)
wa1 = Walk("KMITL", "Lawson at KMITL", 0.6)
# Taxi(start, end, distance_km)
ta1 = Taxi("Lawson at KMITL", "Ladkrabang station", 5)
# Train(start, end, distance_km, num_stations)
tr1 = Train("Ladkrabang station", "Payathai station", 40, 6)
# Taxi(start, end, distance_km)
ta2 = Taxi("Payathai station", "The British Council", 3)
transport = [wa1, ta1, tr1, ta2]
total = 0.0
for tran in transport:
fc = tran.find_cost()
print(f"Cost: {fc:.1f}")
total += fc
print(f"Total: {total:.1f}")Editor is loading...
Leave a Comment