Question
The main() should provide this menu ======================= Choose 1-4 or 0 to exit ----------------------- [1] Create the car object [2] List the cars details [3]
The main() should provide this menu
======================= Choose 1-4 or 0 to exit ----------------------- [1] Create the car object [2] List the cars details [3] Display the cars profit [4] Reduce the cars price
[5] Delete the car object
[6] Test create car1 and car2
[0] Exit ========================
The menu will repeat as many times as necessary
All errors raised during the execution of the program should be trapped.
Specifications for [6] Test create car1 and car2
Option 6 will be unit tested, so it is important to follow the specifications the best you can.
Create a function create_two_cars(), which you will call from option [6].
In this function you add the following code
The first part of the testing code is given to you
car1 = Car("Honda", "Civic", 2015, 5000.0, 99999.99) print(car1.get_details()) print("First Cars Initial Profit is $%.2f" % car1.calc_profit()) car1.reduce_price(500.0) print("First Cars New Profit is $%.2f" % car1.calc_profit())
Add the code to: create a second Car object (call it car2) with different details than car1. It should be a 2019 BMW M3 with a price of $50000 and a cost of $30000.
- Display the details for this car
- Display the initial profit for this car
- Reduce the profit by $5000.50
- Display the new profit for this car
You have now defined a class for cars in a car lot (called Car) and created two unique car objects from that class (car1 and car2), but with different attributes.
The output from your Test option [6] should look exactly like this:
2020 Honda Civic for sale for $20000.00
First Car's Initial Profit is $5000.00
First Car's New Profit is $4000.00
2019 BMW M3 for sale for $50000.00
Second Car's Initial Profit is $20000.00
Second Car's New Profit is $14999.50
Create the class Car following the Circle and Vehicle examples, at the end of the lab
Steps:
- Create the car.py file
- Create the class Car the naming convention: nouns at singular, Camel naming notation for the class (class CourseSchedule, Product, HotelRoom)
- Create the constructor this is the first function executed when the instance is created. The constructor is right after the docstring. Pass the following parameters
def __init__(self, make, model, year, cost, price): """ Initializes the car details """
__(self, string, string, XXXX, float.2, float.2):
- Create the following class functions
Function Name | Specifications: Input, Return, Processing The User Output is for the Menu in main() |
float calc_profit() | Processing: Profit=Price - Cost The instance already has the data for the price and cost, which was passed when the constructor was called and now is stored in the memory under the __price and __cost variables. Return: Calculate the Profit and return it User Output: Display The profit is float.2 |
String get_details() | Return: a formatted string with the car details. The string should look like: 2015 Honda Civic for sale for $9999.99 User Output: the returned string |
float, float reduce_price(reduction) | Processing: reduces the value stored in the class variable _price with the value passed. If the reduction is greater than 10% of the value raise a ValueError Price is float.2, max reduction is float.2 Return: the new price and the percentage of the reduction applied. User Input: the value of the reduction User Output: nicely display 3 lines
|
5. Create Unit Tests for the 3 functions in the class.
Use as a reference (just as a reference) the Unit Tests uploaded to the lab folder.
Sample code - the Circle class
import math class Circle: '''this is a circle class''' def __init__(self, radius=0): self.__radius=radius def set_radius(self,radius): self.__radius=radius def get_radius(self): return self.__radius def get_area(self): return math.pi*self.__radius**2 #print the doc string print (Circle.__doc__) #create an instance and pass the radius value through the constructor c1=Circle(10) print("c1: Radius= ",c1.get_radius()) print("c1: Area= ",c1.get_area()) del c1 #create an instance and call the set function to set the radius c2=Circle() print("c2: Radius= ", c2.get_radius()) #the radius should be 0 #set the radius c2.set_radius(15) #check the radius value using the accessor or the get print("c2: Radius= ", c2.get_radius()) print("c2: Area= ", c2.get_area())
Sample code - the Vehicle class
class Vehicle: def __init__(self, brand, model, type): self.brand=brand self.model=model self.type=type self.gas_tank_size=14 self.fuel_level=0 def fuel_up(self): self.fuel_level=self.gas_tank_size print('Gas tank is now full.') def drive(self): print(f'The {self.model} is now driving.') def main(): #creating the instance or object or instance variable vehicle_object=Vehicle('Honda', 'Ridgeline', 'Truck') #accessing attribute values print("Attributes:") print(vehicle_object.brand) print(vehicle_object.model) print(vehicle_object.type) #calling methods print("Methods:") vehicle_object.fuel_up() vehicle_object.drive() if __name__=="__main__": main()
Best Practices
- Variable names are descriptive and should be lower_snake_case
- Function names should be lower_snake_case
- Class names should be camel notation
- All functions include Doc-String comments
- Provide a Menu to access the functions
- Provide Error handling and input validation
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