In Python Demonstrate the concept of polymorphism in the following exercise: Wri
ID: 3907321 • Letter: I
Question
In Python
Demonstrate the concept of polymorphism in the following exercise:
Write a class for Product, Garment, WomensGarment and MensGarment
Product
Properties
Name, Price
Methods
Setter, getters
Garment
Properties
ThreadCount
Make
Type
Methods
Setter, Getters
WomensGarment
Properties
Color
Designer
Method
Setter, Getters, getStyleInformation (returns sleeveless/short dress, long dress, etc)
MensGarment
Properties
Color
Designer
Methods
Setter, Getters, getStyleInformation (returns slimfit, curved edges,etc)
Write the main app – ask the user what Garment he/she wants to buy, either womens or mens. Depending on the answer of the user, instantiate the corresponding object. Ask additional information form the user to set the properties. Finally, display all the information of the object including getStyleInformation.
Explanation / Answer
ScreenShot
-----------------------------------------------------------------------------
Program
""" In this application i created four classes Product,Garment,WomensGarment and MensGarment
For test purpose i only add one if conditions in WomensGarment and MensGarment
for further expansion you can add more colors and brands using elif
"""
#Product information class
class Product:
#constructor
def __init__(self, Name,Price):
self.Name = Name
self.Price = Price
#Getters
def get_Name(self):
return self.Name
def get_Price(self):
return self.Price
#Setters
def set_Name(self, Name):
self.Name = Name
def set_Price(self, Price):
self.Price = Price
#about the garments used in the product information class
class Garment:
#Constructor
def __init__(self, ThreadCount,Make,Type):
self.ThreadCount = ThreadCount
self.Make = Make
self.Type=Type
#Getters
def get_ThreadCount(self):
return self.ThreadCount
def get_Make(self):
return self.Make
def get_Type(self):
return self.Type
#Setters
def set_Threadcount(self, ThreadCount):
self.ThreadCount = ThreadCount
def set_Make(self, Make):
self.Make = Make
def set_Type(self,Type):
self.Type = Type
#Womens Garmens class access details from above classes
class WomensGarment:
#Constructor
def __init__(self):
self.Color = ""
self.Designer =""
#Getters
def get_Color(self):
return self.Color
def get_Designer(self):
return self.Designer
#setters
def set_Color(self, Color):
self.Color = Color
def set_Designer(self,Designer):
self.Designer = Designer
#Style and details of dress return using member function
def getStyleInformation(self):
if(self.Color=="Red" and self.Designer=="BIBA"):
G=Garment(50,"Two-ply","cotton")
p=Product("Flared Dress",1700)
return self.Color,self.Designer,G.get_ThreadCount(),G.get_Make(),G.get_Type(),p.get_Name(),p.get_Price(),"long dress"
#Mens Garmens class access details from above classes
class MensGarment:
#Constructor
def __init__(self):
self.Color = ""
self.Designer =""
#Getters
def get_Color(self):
return self.Color
def get_Designer(self):
return self.Designer
#Setters
def set_Color(self, Color):
self.Color = Color
def set_Designer(self,Designer):
self.Designer = Designer
#Style and details of dress return using member function
def getStyleInformation(self):
if(self.Color=="Grey" and self.Designer=="Raymond"):
G=Garment(50,"Two-ply","Linen")
p=Product("Trousers",3000)
return self.Color,self.Designer,G.get_ThreadCount(),G.get_Make(),G.get_Type(),p.get_Name(),p.get_Price(),"Regular Fit"
#Main method
def main():
#User prompt to enter the kind of dress needed
inp=input("(W)omen/(M)en garments.Which one do you want?")
#If it's women then choose corresponding object display corresponding specifications
if inp=="W":
w=WomensGarment()
col=input("Enter the color:")
des=input("Enter the designer name:")
w.set_Color(col)
w.set_Designer(des)
c,d,t,m,ty,na,pr,style=w.getStyleInformation()
print("Designer="+d+" Name="+na+" Color="+c+" ThreadCount="+str(t)+" Make="+m+" Type="+ty+" Price="+str(pr))
#If it's men then choose corresponding object display corresponding specifications
elif inp=="M":
M=MensGarment()
col=input("Enter the color:")
des=input("Enter the designer name:")
M.set_Color(col)
M.set_Designer(des)
c,d,t,m,ty,na,pr,style=M.getStyleInformation()
print("Designer="+d+" Name="+na+" Color="+c+" ThreadCount="+str(t)+" Make="+m+" Type="+ty+" Price="+str(pr))
if __name__== "__main__":
main()
----------------------------------------------------------------------
Output
(W)omen/(M)en garments.Which one do you want?W
Enter the color:Red
Enter the designer name:BIBA
Designer=BIBA
Name=Flared Dress
Color=Red
ThreadCount=50
Make=Two-ply
Type=cotton
Style=long dress
Price=1700
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.