Reputation: 77
I'm doing the Thinkful Python course at the moment and I can't figure out how to use the self attribute of one class in another class.
class Bicycle(object):
# Have a model name
# Have a weight
# Have a cost to produce
def __init__(self, model):
self.model = model
pass
class BicycleShop(object):
# Create a bicycle shop that has 6 different bicycle models in stock. The shop should charge its customers 20% over the cost of the bikes
margin = 1.2
# Have a name
# Have an inventory of different bikes
# Sell bikes with a margin over their cost
# Can see a total of how much profit they have made
def __init__(self, company_name, models):
self.company_name = company_name
self.models = models
def bicycle_models(self):
for model in self.models.keys():
print(model)
def bicycle_prices(self):
for model, price in self.models.items():
if price <= customer_1.budget:
print("The {} is available for a price of ${:.2f}.".format(model, price * self.margin))
class Customer(object):
# Have a name
# Have a fund of money used to purchase the bike
# Can buy and own a new bicycle
def __init__(self, name, budget):
self.name = name
self.budget = budget
def check_funds(self):
return evans_cycles.bicycle_prices()
evans_cycles = BicycleShop("Evans Cycles", { "BMC Road Machine": 125, "Cannondale Synapse": 275, "Pinnacle Laterite": 450, "Fuji Transonic": 625, "Cervelo R2": 750, "Specialized Roubaix": 999 })
print("\nWe are {} Bicycle Shop. Please see our range of bikes, below.\n".format(evans_cycles.company_name))
evans_cycles.bicycle_models()
customer_1 = Customer('Stuart', 1000)
print("\nHello, I'm {} and my budget is ${}. What can I afford?\n".format(customer_1.name, customer_1.budget))
print(customer_1.check_funds())
Currently, I have hard coded in customer_1.budget into the bicycle_prices method and evans_cycles into the check_funds function. But I'm aware this is not the correct way to do it, but I can't figure out how to do it any other way.
What is the correct way to utilize attributes of one class in another? I have tried to use inheritance, but it didn't work, it would not accept my dictionary as a parameter I think.
Upvotes: 2
Views: 4978
Reputation: 56467
Whenever you design something you have to think about relations. So how is a customer related to a shop? Well, lets assume that each shop has customers and each customer has only one shop (does not have to be true, just as an example). In that case you would do
class BicycleShop:
...
class Customer:
def __init__(self, shop):
self.shop = shop
so now customer has a reference to the shop. Now you can expose get_models()
function in the shop:
class BicycleShop:
def get_models(self):
return self.models
and finally check_funds
on Customer
:
class Customer:
def __init__(self, name, budget, shop):
self.name = name
self.shop = shop
self.budget = budget
def check_funds(self):
models = self.shop.get_models()
for model, price in models.items():
if price <= self.budget:
print("The {} is available for a price of ${:.2f}.".format(model, self.get_price(model)))
You also have to implement def get_price(self, model)
method on BicycleShop
because (again relations) the price depends not only on the model but on the shop as well. Then it would go like this:
evans_cycles = BicycleShop("Evans Cycles", { "BMC Road Machine": 125, "Cannondale Synapse": 275, "Pinnacle Laterite": 450, "Fuji Transonic": 625, "Cervelo R2": 750, "Specialized Roubaix": 999 })
print("\nWe are {} Bicycle Shop. Please see our range of bikes, below.\n".format(evans_cycles.company_name))
customer_1 = Customer('Stuart', 1000, evans_cycles)
customer_1.check_funds()
Upvotes: 1