WEEK12: defining our own classes/object-oriented programming
---------------------------------------------------------------
F: more classes...
ITEM class:
- last time we started working on a shopping cart class
- for this example, I've created a generic Item class
- copy these files:
/home/jk/inclass/item.py
/home/jk/inclass/items.txt
/home/jk/inclass/shoppingcart.py
- read through the files and make sure you understand how they work
YOUR TURN: write an app that uses these classes
- write an amazon.py program that imports item and shopping cart
- it should create an empty shopping cart and allow the user to
choose items to add to the cart
- here's an example of main() and a run of the program:
def main():
inventory = readItems() # read data from file, store as list of Items
cart = ShoppingCart() # empty cart
display(inventory) # display the inventory
shop(cart, inventory) # allow user to choose items
checkout(cart) # fake checkout (add name and email to cart)
$ python amazon.py
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 #: 6
Adding Webkinz Pink Pony to cart...
Item #:
----------------------------------------
----------------------------------------
Here's what's in your cart:
######################################################################
Owner:
Email:
Items in cart:
1. Webkinz Pink Pony -- $ 7.54
** Total: $7.54 **
######################################################################
Please enter your name and email to purchase these items...
Name: jeff knerr
Email: knerr@cs.swarthmore.edu
######################################################################
Owner: jeff knerr
Email: knerr@cs.swarthmore.edu
Items in cart:
1. Webkinz Pink Pony -- $ 7.54
** Total: $7.54 **
######################################################################
----------------------------------------
----------------------------------------
CLASS WRITER vs CLASS USER:
- notice how, in writing code that imports a class, like amazon.py,
we are the class USER (using the shoppingcart.py class file, for example)
- the only way to access or change the cart is through the
methods provided by the shoppingcart class WRITER
- the divide between the class writer and class user is called
the interface. if a class user needs access to data in the
class, the class writer makes it accessible by creating methods
amazon.py shoppingcart.py
----------- ------------------
| | data: self.items
| i | self.costs
| n | self.name
| t | self.email
| e | self.total
| r |
| f | methods:
| a | setName
| c | setEmail
| e | addItem
| | removeItem
| |
- if the amazon.py writer wants to access the total cost of
a shopping cart, we would need to add a getTotal() method
to the shoppingcart class
- if you think a credit card number should be part of a shopping
cart, you would need to add a data item (self.ccnumber?) to
the shopping cart, plus methods to get/set it
YOUR TURN:
- add a self.ccnumber data item (instance variable) to the
shoppingcart class, plus methods to get and set the number
- change the amazon.py program to ask for the credit card number
at checkout() and store it in the shoppingcart object