Question: Q 5 - Car Inventory ( 1 . 4 points ) For this question you will create an object to store information on an inventory

Q5- Car Inventory (1.4 points)
For this question you will create an object to store information on an inventory of cars.
Create a class called CarInventory
Instance Attibutes:
n_cars : intcars : list
Methods
add_car()compare()
Details:
The constructor (__init__) should take no inputs, but set n_cars to 0, and cars to be [](an empty list).
Method: add_car
Input(s):
manufacturer : string
model : string
year : int
mpg : float
Procedure(s):
Create a dictionary from the values passed with the corresponding keys 'manufacturer', 'model', 'year' and 'mpg'
Append this dictionary to the cars attribute
Increment the n_cars attribute by 1
Method: compare
This method will compare all the cars on a specified attribute ('year', or 'mpg'), and find the highest and lowest values. It will return either the highest of lowest value, depending on the setting in a parameter direction.
Input(s):
attribute : string
direction : string, default: 'highest'
Procedure(s):
Initialize a variable called lowest to be the 0th element of cars, and another variable highest to be the same
Loop through each car in cars
If the value of the attribute for the car is less than the car stored as lowest, replace lowest to be the current carSimilarly, if the value of the attribute for the car is greater than the car stored as highest, replace highest to be the current car
After the loop, if the value of direction is 'highest', set a new variable output to be the variable highest
Else if the value of direction is 'lowest', set output to be lowest
Return output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!