Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please use python An online shop sells products (class Product) that are described by name (string), mass (a number in kg), quantity in stock (integer)
Please use python
An online shop sells products (class Product) that are described by name (string), mass (a number in kg), quantity in stock (integer) and the price (float, USD). The class has methods named accordingly and a method ___str_that returns a user-friendly string, as seen below. A new version of their web software must add support for discounted products. A discounted product is also a product (subclass of Product, inheriting all methods) and adds a new attribute, the discount. The design is a bit peculiar, in the sense that the Discounted Product object must encapsulate a Product object on which it applies the discount. The Discounted Product object depends on the encapsulated Product object to implement all its methods: the name is modified (as seen below) to include the discount, the price is the Product's price minus the discount, while the mass and weight are identical. The only inherited attributes actually used in a Discounted Product object are the reference to the encapsulated Product and the discount. Example of using these classes: # create a product object for Lavalamps, priced at $100, and with 123 of them in stock: p = Product (name="Lavalamp", price=30, mass=0.8, stock=123) print(p) # prints "Lavalamp, $30, 0.8 kg, 123 in stock" # p.price() returns 30.0 # create a discounted product of p, with a 20% discount: discup = DiscountedProduct(0.2, p) print(disc_p.price() # prints "24" (24 == 30 - 20% * 30) print(disc_p) # prints "discounted 20%: Lavalamp, $24, 0.8 kg, 123 in stock" # now, we change the product p: p.set_price(20) print (p.price) # prints "20" # the price change also affects the discounted product object that embeds p: print(disc_p) # prints "discounted 20%: Lavalamp, $16, 0.8 kg, 123 in stock" # disc_p.price() returns 16 (16 == 20 - 20% * 20) Implement the Product and Discounted Product classes, following the requirements above, and making sure the Discounted Product's methods rely on the results returned by the encapsulated Product object. (This design follows the Decorator design pattern.) Write a main function that illustrates how the two classes are used. You can start from the sample code aboveStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started