ALHComer
ALHComer

Reputation: 43

How do append an instance of a class?

I am looking to create a (very!) basic inventory management system

This is the brief:

Product Inventory Project - Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand. Then create an inventory class which keeps track of various products and can sum up the inventory value.

Here is my code so far:

class Product:
    def __init__(self, id_num, price, quantity):
        self.price = price
        self.id_num = id_num
        self.quantity = quantity

class Inventory:
    def __init__(self):
        self.product_list = []
        
    def add_item(self):
        id_num = int(input('Enter id: '))
        price = int(input('Enter price: '))
        quantity = int(input('Enter quantity: '))
        self.product_list.append(Product(id_num, price, quantity))

I don't understand how to make an instance of the product class append to the product list in testing. I feel like I am way off. Any help would be much appreciated!

Upvotes: 0

Views: 538

Answers (1)

Ibrahim Almahfooz
Ibrahim Almahfooz

Reputation: 317

The code is fine. You just need to execute :)

Look at this sample I just modified inputs and made static values for fast execution:

class Product:
    def __init__(self, id_num, price, quantity):
        self.price = price
        self.id_num = id_num
        self.quantity = quantity

class Inventory:
    def __init__(self):
        self.product_list = []
        
    def add_item(self):
        id_num = 1 #int(input('Enter id: '))
        price = 100 #int(input('Enter price: '))
        quantity = 4 #int(input('Enter quantity: '))
        self.product_list.append(Product(id_num, price, quantity))


inv = Inventory()
inv.add_item()
print(inv.product_list[0].price)

You should get the print result of 100 which is the price of the item

Upvotes: 1

Related Questions