Answer to Question #298526 in Python for H A

Question #298526

Write a Python class named Product which has attributes product_id, product_name,

number of items. Include the constructor and other required methods to set and get the

class attributes. The class should have 2 additional methods to add and remove products from the inventory. If the number of items for a product in the inventory is less than zero, the user should be notified with a print message stating, “Product is out of stock”. Create 2 instances of the class and display all its associated attributes.


I want very simple code


1
Expert's answer
2022-02-16T08:10:20-0500
class Product:

    products__count = 0

    def __init__(self, id, name):
        self.product__id = id
        self.product__name = name
        Product.products__count += 1

    def __del__(self):
        print(f'Product deleted ({self.product__name})')
        Product.products__count -= 1

        if Product.products__count == 0:
            print('Product is out of stock')

    def changeDataProduct(self, new__id, new__name):
        self.product__id = new__id
        self.product__name = new__name
        print('Data changed')

    def printData(self):
        print('Product id:', self.product__id)
        print('Product name', self.product__name)

inventory = []

inventory.append(Product(1, 'Bacon'))
inventory.append(Product(2, 'Bread'))

num = 10
for item in inventory:
    item.printData()
    print('')
    item.changeDataProduct(num, f'Product{num}')
    print('')
    item.printData()
    print('')
    del item
    num += 10

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS