Object oriented programming is something that is utilized by nearly every business. To illustrate its utility, we will be building an e-commerce application together using object oriented programming over the next three units.
This week, we’ll be implementing a Product
class to represent the products in our store and a ShoppingCart
class to store the Product
(s) the user has selected.
- Define a
Product
class. - Define constructor with names (String), price (float), and sku (int).
- Assign params to properties.
- In the
tests
folder there is a test calledtest_can_create_product
that tests that you can...create a product. You can set these up to run in your VS Code by doing the following:- Press the "Testing" flask icon in the left hand toolbar.
- Press "Configure Python Tests"
- Select "pytest"
- Select the "root directory"
- You can now run some or all of the tests in the left hand menu.
- Import the
Product
class into the file. - Create
main()
function that will hold all of our program runs. Call the function. - Create two instances of the
Product
class and print out the attributes. To see the output runpython3 main.py
in the command line. - Print out the object. You should see something like
<app.Product object at 0x1007e2e40>
. This is listing the memory address on our computer for the Product object, but that's not particularly useful. We can create a__str__()
method to print out something more useful.
- Create a
__str__()
method that takes the object as an argument and returns a string printing out the information about the object. This string should be in the format"[NAME] (SKU: [SKU]) - $[PRICE]"
. - The
test_product_str
test should now be passing.
- Verify that the properties print out as expected using the command
python3 main.py
.
- Create a
ShoppingCart
class. - Define constructor with items (list) that will hold a list of
Product
objects. - Define a
__str()__
method that prints out information about theShoppingCart
object. This string should be in the format"Shopping Cart with [NUMBER_OF_ITEMS] items.""
. - The
test_can_create_shoppingcart
andtest_shoppingcart_str
tests should now be passing.
- Import the
ShoppingCart
class into the file. - Inside
main()
, create an instance of the shopping cart and print it to the console.
- Define a
add_items
method that takes the object, a product, and a quantity as an argument and does the following: - If no quantity is provided, it should be set to 1. - Add the item to the object'sitems
list. It should be added as a dictionary in the format{"product": [PRODUCT_OBJECT], "quantity": [QUANTITY]}
. - Define a
get_total
method that takes the object as an argument and does the following:- Initialize a variable called
total
with a value of zero. - Iterate through
items
and total up the cost of all the items in the cart - Return
total
- Initialize a variable called
- Define a
display_cart
method that takes the object as an argument and does the following:- Iterate through the
items
in the cart and print out a string in the format"[PRODUCT_NAME] - Quantity: [QUANTITY]')"
- When the iteration is complete, print the total cost of all the items in the format
"Total: $[TOTAL]"
.
- Iterate through the
- The
test_shoppingcart_add_items
should now be passing.
- Add each of the
Product
objects you created in step 9 to theShoppingCart
instance using theadd_items
method. - Display the cart contents using the
display_cart
method. - Call
get_total
to see the total cost of everything that you added to the cart.