[Tutor] ecommerce.py

Christopher Spears cspears2002 at yahoo.com
Wed Aug 13 01:54:56 CEST 2008


Hey Everybody,

I took Kent's advice and changed the design of the script.  Tell me what you think!

#!/usr/bin/python

class Widget(object):
    def __init__(self,widget_number):
        self.widget_number = widget_number
	#print "Created Widget %d" % widget_number

class Cart(object):
    def __init__(self,name):
        self.name = name
	print "Created " + self.name
	
    def add_widgets(self,number_widgets):
        for n in range(number_widgets):
	    widget = Widget(n + 1)
        print "I put %d widgets in the cart!" % number_widgets

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

    def buy_widgets(self,cart_name,number_widgets):
        cart = Cart(cart_name)
	self.cart_number = self.cart_number + 1
	self.cart_list.append(cart.name)
	cart.add_widgets(number_widgets)
	self.cart_dict[cart_name] = number_widgets
	
    def carts_info(self):
        print "You have %d carts!" % self.cart_number
	for i, c in enumerate(self.cart_list):
	    print "%d) %s has %d widgets" % (i+1, c, self.cart_dict[c]) 
        
	
if __name__ == '__main__':
    user_name =  raw_input("Enter your name: ")
    user = User(user_name)
    
    print "You can "
    print "1) Buy a widget"
    print "2) List your carts"
    print "3) 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: ")
	    number_widgets = raw_input("How many widgets do you want? ")
	    number_widgets_int = int(number_widgets)
            user.buy_widgets(cart_name,number_widgets_int)
	elif choice_int == 2:
	    user.carts_info()
	elif choice_int == 3:
	    print "Goodbye!"
	    break
        else:
            print "Invalid Choice!"
	    print "Try Again!"
   



--- On Tue, 8/12/08, Kent Johnson <kent37 at tds.net> wrote:

> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] ecommerce.py
> To: cspears2002 at yahoo.com
> Cc: tutor at python.org
> Date: Tuesday, August 12, 2008, 4:01 AM
> On Tue, Aug 12, 2008 at 1:41 AM, Christopher Spears
> <cspears2002 at yahoo.com> wrote:
> > 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?
> 
> You figured out how to put a Cart in a User, I guess you
> want
> something similar - a cart should have a list of widgets.
> 
> Maybe the problem asks for it, but usually an online shop
> has just one
> cart per user and the cart is created for you when you ask
> to buy
> something. If you use that design it might be simpler.
> 
> Kent


      


More information about the Tutor mailing list