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
Productclass. - Define constructor with names (String), price (float), and sku (int).
- Assign params to properties.
- In the
testsfolder there is a test calledtest_can_create_productthat 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
Productclass into the file. - Create
main()function that will hold all of our program runs. Call the function. - Create two instances of the
Productclass and print out the attributes. To see the output runpython3 main.pyin 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_strtest should now be passing.
- Verify that the properties print out as expected using the command
python3 main.py.
- Create a
ShoppingCartclass. - Define constructor with items (list) that will hold a list of
Productobjects. - Define a
__str()__method that prints out information about theShoppingCartobject. This string should be in the format"Shopping Cart with [NUMBER_OF_ITEMS] items."". - The
test_can_create_shoppingcartandtest_shoppingcart_strtests should now be passing.
- Import the
ShoppingCartclass into the file. - Inside
main(), create an instance of the shopping cart and print it to the console.
- Define a
add_itemsmethod 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'sitemslist. It should be added as a dictionary in the format{"product": [PRODUCT_OBJECT], "quantity": [QUANTITY]}. - Define a
get_totalmethod that takes the object as an argument and does the following:- Initialize a variable called
totalwith a value of zero. - Iterate through
itemsand total up the cost of all the items in the cart - Return
total
- Initialize a variable called
- Define a
display_cartmethod that takes the object as an argument and does the following:- Iterate through the
itemsin 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_itemsshould now be passing.
- Add each of the
Productobjects you created in step 9 to theShoppingCartinstance using theadd_itemsmethod. - Display the cart contents using the
display_cartmethod. - Call
get_totalto see the total cost of everything that you added to the cart.
