I am working on problem 13-11 from Core Python Programming (2nd Edition).  For 
the problem, I am supposed to create the foundations of an e-commerce engine 
for a B2C business-to-consumer) retailer.  I need to create a class that 
represents the customer called User, a class for items in inventory called 
Item, and a shopping cart class called Cart.  I call my Item class Widget 
because I like the word.  Multiple widgets can go in carts, and a User can have 
multiple Carts.  Here is what I have written so far:

#!/usr/bin/python

class Widget(object):
    pass

class Cart(object):
    def __init__(self,name):
        self.name = name
        print "Created " + self.name

class User(object):
    def __init__(self,name="John Doe"):
        self.name = name
        print "Hello " + self.name + "!"
        self.cart_number = 0
        self.cart_list = []

    def make_cart(self,cart_name):
        cart = Cart(cart_name)
        self.cart_number = self.cart_number + 1
        self.cart_list.append(cart.name)
        
    def carts_info(self):
        print "You have %d carts!" % self.cart_number
        for i, c in enumerate(self.cart_list):
            print "%d %s" % (i+1, c) 
        
        
if __name__ == '__main__':
    user_name =  raw_input("Enter your name: ")
    user = User(user_name)
    
    print "You can "
    print "1) Create a cart"
    print "2) Buy a widget"
    print "3) List your carts"
    print "4) Quit"
    
    while True:
        
        while True:
            choice = raw_input("Pick an option: ")
            try:
                choice_int = int(choice)
                break
            except ValueError:
                print "Invalid choice!"
                print "Try again!"
    
        if choice_int == 1: 
            cart_name = raw_input("Enter the name of your cart: ")
            user.make_cart(cart_name)
        elif choice_int == 2:
            print "I bought a widget."
        elif choice_int == 3:
            user.carts_info()
        elif choice_int == 4:
            print "Goodbye!"
            break
        else:
            print "Invalid Choice!"
            print "Try Again!"

I am not sure how to handle putting Widgets into a Cart.  Somehow, I need the 
user to be able to select the Cart he or she wants and then put Widgets in it.  
Any hints?

Thanks!



      
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to