- Learn how to use the 2 most commonly used collections: List and Dictionary.
In the last exercise we added a function to add a product to store. In this exercise, we're going to expand on that as well as add a new function to view our product list.
- We're going to add a new class to handle all of our product functionality. Add a new class called
ProductLogic
. - In this new
ProductLogic
class, add your constructor by writing thedef __init__():
. For now just add thepass
keyword inside the constructor. - In
ProductLogic
add a new private class variable called_products
which will be a list. Remember to add an underscore _ before the name because it's a private variable. Set this equal to an empty list. - Create a new function/method in your
ProductLogic
class calledAddProduct()
. This will not return anything and it will accept one argument calledproduct
and it should be of type Product. So the method declaration/signature will look like this:def AddProduct(product: Product) -> None:
- In that method,
AddProduct()
, use the_product
variable's.append()
method to add the method's argument (which should beproduct
) to the list. - Next create another method in the class called
GetAllProducts()
. The method will return a typelist[Product]
and will have no arguments. This method will be really simple, all you have to do is return our private variable:return _products
. - Back in our main "Program" class or wherever our main entry point is (which might be inside of
main()
), we're going to create an instance of our new ProductLogic. Make it a variable near or at the top of our class. Should look like:productLogic = ProductLogic()
- Replace the line where we are serializing via
json.dumps()
the product that the user is creating with theAddProduct()
method and pass the product the user created into the method. In effect we want to take the product the User is creating and pass it to theAddProduct()
which will add it to our total list of products. - After adding the product, add a nice message below it to let the user know that the product was added.
- Our next major thing to add will be some dictionaries to hold our specific types of products. So back in
ProductLogic
, add 2 more private class variables. These two variables will bedict
s and will hold a key/value mapping where thekey
is a string name of a dog product or cat product and the value is the corresponding object. You can choose what to name the variables that will contain the dog products and the cat products. - Now, in
AddProduct
add a conditionalif
statement. It's condition will check for the Type of object. If the type of object is a DogLeash it will be added to the dog product dictionary you created previously. If it's a CatFood object, it will be added to the cat products dictionary. The key will be the product'sname
value. So an if statement will look like this: if (isinstance(product, DogLeash)). - Inside the body of this conditional
if
statement, use the following technique to add the product to the appropriate dictionary:dogproducts[product.name] = product
. Make sure the.name
matches the casing (upper/lower) of the class property. - Add another conditional
if
block that does the same thing except forCatFood
instead ofDogLeash
. As a result we should have two classes,CatFood
andDogLeash
that look similar. - Add another method called
GetDogLeashByName()
. It will return an object of type DogLeash and will have one argument of type string called "name". - Inside the body of the
GetDogLeashByName()
method you will need to use thename
argument to access thedogproducts
dictionary (or whatever you named this dictionary) in order to get back theDogLeash
object. That would look like something this:return dogproducts[name]
. - Back in the main program class, add a new option for the user. It will be option number 2 and will let them get a specific object out of the list. Since they're only adding a dog leash right now, we're going to just let them retrieve a dog leash.