0

The Product class seems to work fine but I'm trying to figure out how to get the Inventory class to separate each product into there specific categories. I feel like I'm close but whenever I try and print out the inventory it just shows where it's stored in memory and doesn't actually print anything out. The output i receive when running is at the bottom. I want it to print out the actual products and data, not the instance of it stored in memory.

class Product:

    def __init__(self, pid, price, quantity):
        self.pid = pid 
        self.price = price
        self.quantity = quantity

    def __str__(self):
        #Return the strinf representing the product
        return "Product ID: {}\t Price: {}\t Quantity: {}\n".format(self.pid, self.price, self.quantity)

    def get_id(self):
        #returns id 
        return self.pid

    def get_price(self):
        #returns price
        return self.price

    def get_quantity(self):
        #returns quantity
        return self.quantity

    def increase_quantity(self):
        self.quantity += 1

    def decrease_quantity(self):
        self.quantity -= 1 


    def get_value(self):
        value = self.quantity * self.price
        return 'value is {}'.format(value)


product_1 = Product('fishing', 20, 10)
product_2 = Product('apparel', 35, 20)


class Inventory:

    def __init__(self, products):
        self.products = products
        self.fishing_list = []
        self.apparel_list = []
        self.value = 0 


    def __repr__(self):
    return "Inventory(products: {}, fishing_list: {}, apparel_list: {}, value: {})".format(self.products, self.fishing_list, self.apparel_list, self.value)

    def add_fishing(self):
        for product in self.products:
            if product.get_id() == 'fishing':
                self.fishing_list.append(product)
        return '{} is in the fishing section'.format(self.fishing_list)

    def add_apparel(self):
        for product in self.products:
            if product.get_id() == 'apparel':
                self.apparel_list.append(product)
        return '{} is in the apparel section'.format(self.apparel_list)


inventory_1 = Inventory([product_1, product_2])
inventory_1.add_fishing()
print(inventory_1)

OUTPUT = Inventory(products: [<main.Product instance at 0x10dbc8248>, <main.Product instance at 0x10dbc8290>], fishing_list: [<main.Product instance at 0x10dbc8248>], apparel_list: [], value: 0)

jhackso
  • 1
  • 4
  • *"doesn't actually print anything out."*: Thats correct, you didn't implement **how** to print, therefore you got the default output. Read about [object.__str__](https://docs.python.org/3/reference/datamodel.html#object.__str__) – stovfl May 04 '19 at 18:49

1 Answers1

0

You need to specify how an object of the class Inventory should be printed.

To do this you need to implement at least one of the following functions in your class.

  • __repr__
  • __str__

This answer helps, which of both you should use: https://stackoverflow.com/a/2626364/8411228

An implementation could look something like this:

class Inventory:
    # your code ...
    def __repr__(self):
        return str(self.products) + str(self.fishing_list) + str(self.apparel_list) + str(self.value)

    # or even better with formatting
    def __repr__(self):
        return f"Inventory(products: {self.products}, fishing_list: {self.fishing_list}, apparel_list: {self.apparel_list}, value: {self.value})

Note that I used in the second example f strings, to format the output string.

Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19