I need this program to buy and sell items from a list. the list needs to display similar to below and have a menu for user to select options from:

ID No. Name Price Stock
1 Apples $2.50 4
Choice:

Buy - 1
Sell - 2
Quit - 0

*Needs to be done using object-oriented technique.

class Catalog(object): 

    
    pick = showMenu()
    if pick == 0:
        break
    elif pick == 1:
        sellItem()
    elif pick == 2:
        buyItem()
    else:
        print "Invalid selection"
    

def showMenu():
    pick = int(raw_input('''

Enter selection:
0 - Quit
1 - Sell Item
2 - Buy Item

Selection : ''' ))
        


    def __init__(self, ID, name, price, stock ): 
        self.ID = ID 
        self.name = name 
        self.price = price
        self.stock = [] 
    def buyStock(self, howMany): 
        self.stock += howMany
    def sellStock(self, howMany): 
        if howMany <= self.stock: 
            self.stock -= howMany 
            return True 
        else: 
            return False
    def __str__(self): 
        return str(self.ID) + " " + self.name + " $%0.2f " %self.price + str(self.stock)
         
class Products(object):
    def __init__(self, item_catalog):
        self.item_list = item_catalog
        self.catalog = {1:["Bread", 1.50, 10 ], 2:["Cheese", 5.00, 5], 3:["Apples", 2.50,12]}
        for name in self.item_list:
            self.catalog[name] = Item(name)

I've tried and failed at this too... :(

Recommended Answers

All 3 Replies

Why is the following outside of a class method?

pick = showMenu()
    if pick == 0:
        break
    elif pick == 1:
        sellItem()
    elif pick == 2:
        buyItem()
    else:
        print "Invalid selection"

You have a good start.
I would suggest adding the Sell and the Buy methods under the Product class, also I would suggest parenting Product to an owner.

You would use one separate class as a container for each item, containing the name, id, price, quantity, and any other info for that item.

class Item:
    """ contains all information for an item
    """
    def __init__(self, ID, name, price, start_qty):
        self.ID = ID 
        self.name = name 
        self.price = price
        self.quantity = start_qty

##   simulate the functions

## add 5 new items
add_to_list = []
for x in range(5):
    class_instance = Item("ID"+str(x), "test-"+str(x), 10.20+x, 10+x)
    add_to_list.append(class_instance)

##   subtract one from 'ID2'
for class_pointer in add_to_list:
    if "ID2" == class_pointer.ID:
        print "before", class_pointer.quantity
        class_pointer.quantity -= 1
        print "after", class_pointer.quantity

##   print all info
print "-" * 30
for class_pointer in add_to_list:
    print class_pointer.ID, class_pointer.name, \
          class_pointer.price, class_pointer.quantity

ok i'll give it a whirl, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.