Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Product(object):
- def __init__(self, price, _id, quantity):
- self.price = price
- self.id = _id
- self.quantity = quantity
- def total_value(self):
- return self.price * self.quantity
- def __str__(self):
- return 'id: {} price: {} quantity: {} value: {}'.format(self.id, self.price, self.quantity, self.total_value())
- #def __len__(self):
- # return self.quantity
- class Inventory(object):
- _products = list()
- def append(self, product):
- self._products.append(product)
- def total_value(self):
- return sum(item.total_value() for item in self._products)
- def total_products(self):
- return len(self._products)
- def total_quantity(self):
- return sum(item.quantity for item in self._products)
- def __str__(self):
- return '\n'.join(str(item) for item in self._products)
- #def __len__(self):
- # return sum(len(item) for item in self._products)
- # return sum(item.quantity for item in self._products)
- inv = Inventory()
- inv.append( Product(300, 'cellphone', 12) )
- inv.append( Product(8000, 'laptop', 6) )
- inv.append( Product(2, 'sd card', 1234) )
- print('Available products:')
- print(inv)
- print('Number of products:', inv.total_products())
- print('Total quantity:', inv.total_quantity())
- print('Total value:', inv.total_value())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement