Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Programming Problem 3. An online shop sells products (class Product) that are described by name (string), mass (a number in kg), quantity in stock

Python Programming

image text in transcribed

Problem 3. 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 DiscountedProduct object must encapsulate a Product object on which it applies the discount. The DiscountedProduct 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 DiscountedProduct object are the reference to the encapsulated Product and the discount. Examples # create a product object for Lava!amps, priced at $100, and with 123 of them in stock: p = Product(name-"Lavalamp", price-39, print (p) #prints "Lavalamp, $39, .8 # p.price() returns 38. mass-8.8, stock=123) kg, 123 in stock" # create a discounted product of p, with a 20% discount: disc pDiscountedProduct (0.2, p) print (disc-p . price()) # prints "24" print (disc-p) #prints "discounted (2439-29% * 39) 20%; Lavalamp, $24, .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, .8 kg, 123 in stock" disc-p . price() (16-29-29% * # returns 16 29) Implement the Product and DiscountedProduct classes, following the requirements above, and making sure the DiscountedProduct'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 above

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions