WEEK12: defining our own classes/object-oriented programming
---------------------------------------------------------------
W: more classes...
ITEM class:
class Item(object):
"""class to represent a typical item for sale"""
def __init__(self, desc, cost, what, status):
"""constructor for item class"""
self.desc = desc
self.cost = cost
self.what = what
self.status = status
# ------------------------------------------- #
def __str__(self):
"""should return a string representation of the item"""
s = "%40s, $%6.2f, %8s, %s" % (self.desc, self.cost, self.what, self.status)
return s
# ------------------------------------------- #
def getDescription(self):
return self.desc
def getWhat(self):
return self.what
def getCost(self):
return self.cost
def getStatus(self):
return self.status
# ------------------------------------------- #
def setPrice(self, p):
"""change the cost of the item"""
self.cost = float(p)
# ------------------------------------------- #
def changeStatus(self, status):
"""change the status of the item"""
self.status = status
# ------------------------------------------- #
#############################################
if __name__ == "__main__":
# test code here
i1 = Item("Python Programming",35.55,"book","in stock")
print i1
print i1.getDescription()
print i1.getWhat()
print i1.getCost()
print i1.getStatus()
i1.changeStatus("backordered")
i1.setPrice(i1.getCost() + 10)
print i1
AMAZON application: see the amazon.py file in your inclass directory.
This uses the item and shopping cart classes to do a simple shopping
example:
$ python amazon.py
Welcome to Swarthmore.com!
Here's what we have for sale today:
1. An Introduction to Computer Science -- book -- $ 35.55 (in stock)
2. The Lacuna -- book -- $ 10.80 (in stock)
3. Incognito: The Secret Lives of the Brain -- book -- $ 10.06 (in stock)
4. Data Structures and Algorithms in C++ -- book -- $ 72.85 (in stock)
5. Samsung Galaxy S III -- phone -- $589.99 (in stock)
6. Webkinz Pink Pony -- toy -- $ 7.54 (in stock)
7. FIFA Soccer 13 -- game -- $ 50.65 (in stock)
Please select the item you want to add to your cart.
Enter a blank line to proceed to checkout.
Item #: 2
Adding The Lacuna to cart...
Item #: 7
Adding FIFA Soccer 13 to cart...
Item #:
----------------------------------------
----------------------------------------
Here's what's in your cart:
######################################################################
Owner:
Email:
Items in cart:
1. The Lacuna -- $ 10.80
2. FIFA Soccer 13 -- $ 50.65
** Total: $61.45 **
######################################################################
Please enter your name and email to purchase these items...
Name: jeff
Email: knerr@swat.edu
######################################################################
Owner: jeff
Email: knerr@swat.edu
Items in cart:
1. The Lacuna -- $ 10.80
2. FIFA Soccer 13 -- $ 50.65
** Total: $61.45 **
######################################################################
Your Turn...write a SHOPPINGCART class:
- see shoppingcart.py for the specifications:
"""
amazon.com-like shopping cart
details of the ShoppingCart() class:
__init__ creates empty shopping cart (no params)
data for each cart:
list of items -- initially empty
name -- initially empty string
email -- initially empty string
total -- initially 0.0
__str__ returns string representation of cart
(design this any way you want)
setName(str) method to set buyer's name
setEmail(str) method to set buyer's email address
addItem(item) method to add an item to the cart (adds
item to list, also changes total)
removeItem(i) method to rm ith item and change total
"""
from item import *
#####################################################
# add the ShoppingCart class here...
#####################################################
if __name__ == "__main__":
# add test code here
cart = ShoppingCart()
cart.setName("Jeffrey M. Knerr")
cart.setEmail("knerr@cs.swarthmore.edu")
i1 = Item("Python Programming: Intro to Comp Sci",35.55,"book","in stock")
cart.addItem(i1)
i2 = Item("Webkinz Pink Pony",7.54,"toy","in stock")
cart.addItem(i2)
print cart
cart.removeItem(1)
print cart
CLASS WRITER vs CLASS USER:
- notice how the person writing amazon.py is a CLASS USER
- the person writing shoppingcart.py is a CLASS WRITER
- the only way to access or change a shopping cart in amazon.py is
through the methods provided by the shoppingcart class WRITER