This unit we'll be continuing to build out our e-commerce store by incorporating inheritance into our system. Last unit we created Product and ShoppingCart classes. However, there are many different types of products that our store could have that require special information.
This unit we'll be adding classes for ClothingProduct and ElectronicProduct using inheritance. You can use the code you completed last week, but we have included the completed unit 3 in app.py.
- Define a
ClothingProductclass that is a child of theProductclass. - Call the
__init__method in the child class using eitherProductorsuper(). Set thename,price, andskuproperties. - Set the
sizeandcolorto the values that are passed into the constructor. - Finally, create a
clothing_infomethod that returns information about the object in the form"[NAME] (SKU: [SKU]) - $[PRICE] - Size: [SIZE], Color: [COLOR]"" - Run the tests!
test_clothing_product,test_clothing_product_attributes, andtest_clothing_infotests should now be passing!
- Import the
ClothingProductclass into the file. TheProductandShoppingCartclass was imported last week. - Inside the
main()function that was created last week, create an instance of theClothingProductclass andprint()out the object. Show how it inherited the__str__method from theProductclass.- NOTE: Polymorphism is covered next week, so we're explicitly not adding
__str__method this week.
- NOTE: Polymorphism is covered next week, so we're explicitly not adding
- Call the
clothing_info()method to see the information about the items that you created. - Add both items the cart using
add_items. Print out the new cart info.
- Define a
ElectronicsProductclass that is a child of theProductclass. - Call the
__init__method in the child class using eitherProductorsuper(). Set thename,price, andskuproperties. - Set the
warranty_monthsto the value that are passed into the constructor. - Create a method called
electronics_infothat takes the object as an argument. It should return a string in the format"[NAME] (SKU: [SKU]) - $[PRICE] - Warranty: [WARRANTY_MONTHS] months". - Run the tests!
test_electronics_product,test_electronics_product_attributes, andtest_electronics_infoshould now be passing. - Create a method called
under_warrantythat takes the object andmonthsas an argument. If the number of months passed in is less than thewarranty_months, then returnTrue. Otherwise, returnFalse. - Run the tests!
test_under_warrantyshould now be passing.
- Import the
ElectronicsProductclass into the file. - Inside the
main()function, create an instance of theElectronicsProductclass andprint()out the object. Show how it also inherited the__str__method from theProductclass. - Call
electronics_info()to see the additional information about the object. - Call the
under_warranty()method and print out the resulting boolean. - Finally, add both items the cart using
add_items. Print out the new cart info.
