Home > Blockchain >  Cannot Increment Product count stored in a nested Dictionary - Pyhton OOP
Cannot Increment Product count stored in a nested Dictionary - Pyhton OOP

Time:06-28

So, I am working on my OOP concepts and knowledge by creating dummy projects. Right now I am creating like an online shopping system where admins can upload different products and users can purchase products.

I am having problems with this specific feature where I want to increment product count.

Product Class:

class Product:
  def __init__(self, product_name, description, price, company, product_count):
   self.product_name = product_name
   self.description = description
   self.price = price
   self.company = company
   self.product_count = product_count

and that is how you create a product:

#CREATE PRODUCT
laptop = Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100)
tv = Product('Sony TV', 'Best Sony TV Ever', 2000, 'Sony', 100)
smartwatch = Product('Fossil 6 Gen Smartwatch', 'Fossil watch with wear OS', 300, 'Fossil',100)
shoes = Product('Running shoes', 'Medium size running shoes', 100, 'Nike',100)
gloves = Product('Rock CLimbing gloves', 'Medium gloves for rock clmbing', 250, 'Timberland',100)

Only Admin can upload products and I am storing them in a dictionary:

Admin Class:

class Admin:
  def __init__(self, name):
    self.name = name
    self.products = {}
    self.order = {}
    self.total_products = 0
    self.orders_recieved = {}
    self.order_status = 'Order Recieved'

Logic to add_products:

def add_products(self, product):
    if product not in self.products:
      self.products[product.product_name] = {
        'name':product.product_name,
        'description':product.description,
        'price': product.price,
        'company':product.company,
        'count':product.product_count
      }
      if product not in s.site_products:  #I am also storing all products in different dict() ignore this
        s.site_products[product.product_name] = {
          'name':product.product_name,
          'description':product.description,
          'price': product.price,
          'company':product.company,
          'count':product.product_count
        }
      s.product_total  = 1
      self.total_products  = 1
    else:
      product.product_count  = product.product_count
      
      print('Product already exists')

#This is how you add products:

admin1.add_products(laptop)
admin1.add_products(laptop)

admin1.add_products(tv)
admin1.add_products(smartwatch)

admin2.add_products(shoes)
admin2.add_products(gloves)

Admin has to pass in the product created using Product class in add_Product function(). What I want to do is when an Admin tries to upload the same product twice, only its count changes by adding the previous count and the new one.

But for some reason I am unable to do that I have tried multiple ways but still stuck on it. It is probably something very simple that I am missing out.

Thanks in advance!

CodePudding user response:

Your check for whether a product is already in the dictionary uses the product object rather than its product_name attribute, which is the key you use to add items to the dictionary.

If you change that line, it should work.

Test code:

class Product:
    def __init__(self, product_name, description, price, company, product_count):
        self.product_name = product_name
        self.description = description
        self.price = price
        self.company = company
        self.product_count = product_count
class Admin:
    def __init__(self, name):
        self.name = name
        self.products = {}
        self.total_products = 0
    def add_products(self, product):
        if product.product_name not in self.products:
            self.products[product.product_name] = {
                'name':product.product_name,
                'description':product.description,
                'price': product.price,
                'company':product.company,
                'count':product.product_count
             }
            self.total_products  = 1
        else:
            self.products[product.product_name]['count']  = product.product_count
            print('Product already exists')
a = Admin("foo")
print('try a product')
a.add_products(Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100))
print('try it again')
a.add_products(Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100))

Output:

try a product
try it again
Product already exists
  • Related