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.
simple code
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
Comments
Leave a comment