0

Sorry if I'm vague in my question, first time posting on stackoverflow. I think I'm overlooking something really basic here, gone over several tutorials on classes but cannot for the life of me figure out where I'm going wrong. My code is the following:-

class catalogue(object):

    def __init__(self, catalogueitem):
        self.catalogueitem = catalogueitem
        self.colors = []
        self.stock = "No Stock"

    def setStock(self, stock):
        if self.stock == "No Stock":
            self.stock = stock

class shop(object):

    def __init__(self, items):
        self.shopItems = []
        for item in items:
            self.shopItems.append(catalogue(item))

    def setStock(self, stock, item="purse"):
        self.shopItems.catalogue(item).setStock(stock)

newshop = shop( ["purse","handbag","wallet", "clutchbag"] )

newshop.setStock(10, "handbag")

Basically what I'm trying to do is call the method inside the class catalogue, from within the class shop, and update the item with a new stock value of 10 within the instance newshop. I think I'm lacking a basic understanding of how to do this, and I think I've overlooked something very basic, can anyone help me figure it out please?

Thanks Betty

2 Answers 2

1

When you call

self.shopItems.catalogue(item).setStock(stock)

You're trying to call the methos catalogue of the object self.shopItems. The thing is, self.shopItems is a list. I think you should try with a code like this:

class catalogue(object):

    def __init__(self, catalogueitem):
        self.catalogueitem = catalogueitem
        self.colors = []
        self.stock = "No Stock"

    def setStock(self, stock):
        if self.stock == "No Stock":
            self.stock = stock

class shop(object):

    def __init__(self, items):
        self.shopItems = []
        for item in items:
            self.shopItems.append(catalogue(item))

    def setStock(self, stock, item="purse"):
        c = catalogue(item) #Instance the catalogue class
        c.setStock(stock) #Call the method setStock
        self.shopitems.append(c) #this adds the items into the instance, but # the original objects created by __init__ are still there, i.e. they've not # updated just added.

newshop = shop( ["purse","handbag","wallet", "clutchbag"] )

newshop.setStock(10, "handbag")

Changed only the function setStock from shop class.

EDIT 1:

When you do self.shopItems = catalogue(item) you're overriding what you have in the list. You should change that to:

self.shopItems.append(catalogue(item))

And call the method on the last element afterwars:

self.shopItems[-1].setStock(stock)

Result being:

    def setStock(self, stock, item="purse"):
        self.shopItems.append(catalogue(item))
        self.shopItems[-1].setStock(stock)

EDIT 2:

Ok, to update the existent items you should first check if they exist, iterating through the list:

    def setStock(self, stock, item="purse"):
        c = catalogue(item) #Instance the catalogue class
        c.setStock(stock) #Call the method setStock
        index = -1
        i = 0 #index
        for shItem in self.shopItems:
            if shItem.catalogueitem == item:
                index = i #Saves the index to update
                break #Breaks out of loop
            i += 1
        if index == -1:
            self.shopitems.append(c) #index wasn't updated, the item is new
        else:
            self.shopitems[index] = (c) #replaces the item info
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, many thanks for replying! I tried your code, I think its along the right lines but I want to update the stock for the instance 'shopItems'. I modified the function setStock to:
self.shopItems = catalogue(item) #Instance the catalogue class self.shopItems.setStock(stock) #Call the method setStock
What this does is put the correct stock against "handbag" in instance shopItems, however it removes all other items (purse, wallet, clutchbag), rather than just update the handbag item.
0

Firstly, you want to be able to look up shop items by name. An appropriate data structure to use for this is a dictionary (rather than a list). So, this changes the constructor for shop to:

def __init__(self, items):
    self.shopItems = {}
    for item in items:
        self.shopItems[item] = catalogue(item)

Then, it is easy to update a shop item:

self.shopItems[item].setStock(stock)

Each shopItem is already a catalogue instance, so you can call setStock() on it directly. You should really check that there is such an item first, otherwise you will throw an exception. For example, if you call newshop.setStock(10, 'bag') -- there is no such item. You could protect against that as follows:

def setStock(self, stock, item="purse"):
    if item not in self.shopItems:
        self.shopItems[item] = catalogue(item)
    self.shopItems[item].setStock(stock)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.