I am trying to develop a car-dealership-customer model to really understand OOP using Python.
The problem I am running into is that I'd like to define Car
as a separate class object from the Dealer
class. This causes confusion; how can I use Car
attributes and upload them into the dealership inventory, for example, and then make transaction updates depending on customer acquisition?
I broke it down to just these two classes for now (Car
& Dealer
). I'd like to create an inventory
dictionary with model
being the key and the output from my retail_cost
function being the cost of vehicle. How do i insert instances of car into the dealership inventory in the class dealer?
For example:
class Car(object):
"""This class basically defines a car in its simplest components
and then computes a retail price in the method after initializing"""
def __init__(self,model,engine,prod_cost):
self.model = model
self.engine = engine
self.prod_cost = prod_cost
def retail_cost(self):
return self.prod_cost *1.20
class Dealer(object):
"""Defines the dealership itself with its inventory and sales components"""
car_inventory = {}
def __init__(self, dealer_name):
self.dealer_name = dealer_name
The basic idea is that you add each instance of a car to the dealership, but note that I changed
car_inventory
to be an instance member of Dealer. This is because you want each new dealership you make to have its own car_inventory. If you left it as a class member of Dealer (the way you have it) then every dealership you make will have the same inventory.