Order class: Instance variables: Make instance variables appropriately to accomp
ID: 3721024 • Letter: O
Question
Order class: Instance variables: Make instance variables appropriately to accomplish the tasks you need.
Methods:
? __init__ :: constructor, initializes instance variables
o Additional Parameters: No additional parameters.
o Note: Just because you don’t have any parameters doesn’t mean it wouldn’t be good to set up your instance variables. You will need to keep track of wheels (and number of wheels) ordered somehow…
? add_wheels :: adds some number of wheels to the order.
o Additional Parameters: wheel and number where number indicates how many wheels of the given wheel type to add to the order.
o Assumptions: ? wheel will be an instance of a Wheel ? number will be an integer
o Temporary Assumption (we will fix this later): number will be a positive number above 0
o Hint: You probably want to keep a count of the wheels
? remove_wheels :: removes all wheels from the order which match the wheel parameter.
o Additional Parameters: wheel (the type of wheel being removed)
o Assumption: wheel will be an instance of a Wheel
? count_unique_wheels :: returns the number of unique wheels in the order.
o Additional Parameters: No additional parameters.
? count_total_wheels :: returns the total number of wheels in the order.
o Additional Parameters: No additional parameters.
? count_unique_materials :: returns the number of unique materials in the order.
o Additional Parameters: No additional parameters.
? count_wheels_with_material :: returns the number of wheels in the order with a certain material.
o Additional Parameters: material (the type of material the wheel must be made out of)
o Assumption: material will be an instance of a Material
? get_cost :: returns the total cost for the order (in cents).
o Additional Parameters: No additional parameters.
? __str__ :: returns string representation with this format: "5 Wheels, cost: $297.16"
o Additional Parameters: No additional parameters.
Explanation / Answer
# classes(except other) are used for refrence purpose only please use your own version. add __hash__() method #wherever specified
#THIS QUESTION WAS POSTED EARLIER THAT IS WHY I REMEMBER OTHER CLASSES ALSO AND #METHODS
from _ast import Num
class Cylinders:
def __init__(self, diameter, height):
self.diameter=diameter
self.height=height
def get_volume(self):
return 3.14*self.diameter*self.diameter*self.height/4
class Material:
def __init__(self, cost, name):
self.cost=cost
self.name=name
def get_cost(self, num_units):
return self.cost*num_units
def __eq__(self, other):
if self.name==other.name and self.cost==other.cost:
return True
return False
def __str__(self):
return str(self.cost)+" "+self.name
#please add this method to cylinder class so that it can be used as a dictionary key
def __hash__(self):
return hash((self.name, self.cost))
class Wheel:
def __init__(self, inner, outer, material):
self.inner=inner
self.outer=outer
self.material=material
def get_volume(self):
return self.inner.get_volume()+self.outer.get_volume()
def get_cost(self, number_of_wheels=1):
return self.material.get_cost(number_of_wheels)*self.get_volume()
#Use your own version of this method. created just to use as key
def __eq__(self, other):
if(self.inner.diameter==other.inner.diameter and self.inner.height==other.inner.height and self.inner.diameter==other.inner.diameter and self.inner.height==other.inner.height and self.material==other.material):
return True
return False
#please add this method to cylinder class so that it can be used as a dictionary key
def __hash__(self):
return hash((self.material, self.inner, self.outer))
def __str__(self):
return "";
class Orders:
def __init__(self):
self.dictionary=dict()
def add_wheels(self, wheel, number):
if(wheel in self.dictionary.keys()):
self.dictionary[wheel]+=number
else:
self.dictionary[wheel]=number
def remove_wheels(self, wheel):
if(wheel in self.dictionary.keys()):
self.dictionary.pop(wheel)
def count_unique_wheels(self):
return len(self.dictionary.keys())
def count_total_wheels(self):
count=0
for value in self.dictionary.values():
count+=value
return count
def count_unique_material(self):
material_dict=dict()
for key in self.dictionary.keys():
if(key.material in material_dict):
continue
else:
material_dict[key.material]=1
return len(material_dict.keys())
def count_wheels_with_material(self, material):
count=0
for wheel in self.dictionary.keys():
if(wheel.material==material):
count+=1
return count
def get_cost(self):
cost=0
for wheel in self.dictionary.keys():
cost+=wheel.get_cost()*self.dictionary[wheel]
print(cost)
return cost
def __str__(self):
return str(self.count_total_wheels())+" Wheels, cost: $"+str(self.get_cost())
# this is adriver program
inner=Cylinders(2, 1)
outer=Cylinders(2, 1)
material=Material(1, "Silver")
wheel=Wheel(inner, outer, material)
order=Orders()
order.add_wheels(wheel, 1)
print(order)
#the cost for above order will be $6.28 which is 3.14 for each inner and outer respecti
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.