Question
SilverServiceTaxi File: silver_service_taxi.py and silver_service_taxi_test.py Now create a new class for a SilverServiceTaxi that inherits from Taxi. Do you see the pattern for naming class
SilverServiceTaxi
File: silver_service_taxi.py and silver_service_taxi_test.py
Now create a new class for a SilverServiceTaxi that inherits from Taxi. Do you see the pattern for naming class files?
So SilverServiceTaxi is a Taxi and Taxi is a Car (which also means SilverServiceTaxi is a Car)
This allows you to have a different effective price_per_km, based on the fanciness of the SilverServiceTaxi.
Add a new attribute, fanciness, which is a float that scales (multiplies) the price_per_km. Pass the fanciness value into the constructor and multiply self.price_per_km by it. Note that here we can get the initial base price using Taxi.price_per_km, then customise our object's instance variable, self.price_per_km. So, if the class variable (for all taxis) goes up, the price change is inherited by all SilverServiceTaxis. If you're not sure about this, please ask! Don't go on without "getting it".
SilverServiceTaxi also has an extra charge for each new fare, so add a flagfall class variable set to 4.50.
Add or override whatever method you need to (think about it...) in order to calculate the fare.
Write a __str__ method so that you can add the flagfall to the end. E.g., for a Hummer with a fanciness of 4, it should display like:
Hummer, fuel=200, odo=0, 0km on current fare, $4.92/km plus flagfall of $4.50
Note that you can reuse the parent class's __str__ method like: super().__str__()
Write some test code in a file called silver_service_taxi_test.py to see that your SilverServiceTaxi calculates fares correctly. For an 18 km trip in a SilverServiceTaxi with fanciness of 2, the fare should be $48.78 (yikes!)
class Taxi(Car): """Specialised version of a Car that includes fare costs.""" price_per_km = 1.23 def __init__(self, name, fuel):
""" CP1404/CP5632 Practical Car class """ from prac_09.car import Car class Taxi(Car): """Specialised version of a Car that includes fare costs.""" def __init__(self, name, fuel, price_per_km): """Initialise a Taxi instance, based on parent class Car.""" super().__init__(name, fuel) self.price_per_km = price_per_km self.current_fare_distance = 0 def __str__(self): """Return a string like a Car but with current fare distance.""" return f"{super().__str__()}, {self.current_fare_distance}km on current fare, ${self.price_per_km:.2f}/km" def get_fare(self): """Return the price for the taxi trip.""" return self.price_per_km * self.current_fare_distance def start_fare(self): """Begin a new fare.""" self.current_fare_distance = 0 def drive(self, distance): """Drive like parent Car but calculate fare distance as well.""" distance_driven = super().drive(distance) self.current_fare_distance += distance_driven return distance_driven
test.py
my_taxi = Taxi("Prius 1", 100, 1.23) my_taxi.drive(40) print(my_taxi) print("Current fare: ${:.2f}".format(my_taxi.get_fare())) my_taxi.start_fare() my_taxi.drive(100) print(my_taxi) print("Current fare: ${:.2f}".format(my_taxi.get_fare()))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started