- 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. I 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 class, add a new private variable called "products" of type
List<Product>. Remember to add an underscore_before the name because it's a private variable. - Create a constructor for the class and in that constructor, create a new instance of the list class. It should look something
_products = new List<Product>(); - Create a new function/method called
AddProduct. It should have no return type (so it'll returnvoid) and accept one argument called "product" and it should be of typeProduct. So the method will look like this:public void AddProduct(Product product) - In that method, use the
_productvariablesAddmethod to add the method's argument to the private list. - Next create another method called
GetAllProducts. The method's return type will beList<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, we're going to create an instance of our new
ProductLogic. Make it a variable at the top of the file under theusingstatments.var productLogic = new ProductLogic(); - Replace the line where we are serializing the product that the user is creating with the
AddProduct()method and pass the product the user created into the method. - 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 variables. The first will be of typeDictionary<string, DogLeash>and the second will beDictionary<string, CatFood>. Thestringis the key andDogLeash/CatFoodare the types of objects being stored in the Dictionary. I'll let you choose some names for these variables. - Now, in
AddProductadd an if statement. It's condition will check for the Type of object. If it's aDogLeashit will be added to the dog leash dictionary. If it's aCatFoodobject, it will be added to the cat food dictionary. The key will be the product's name value. So an if statement will look like this:if (product is DogLeash). - Inside of this if statement, use the correct dictionaries
Addmethod to add the product to it. Note: you will probably have to convert the product to the correct child class using theaskeyword. That would look like this:product as DogLeash. - Add another if block that does the same thing except for
CatFoodinstead ofDogLeash. - Add another method called
GetDogLeashByNameit will have a return type of DogLeash and will have one argument of typestringcalled "name". - This method will use the indexing brackets
[]to use the name key being passed in to return the object with that key in the dictionary. That would look like something this:return _dogLeash[name]. - Back in the 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 view a dog leash as well. Use option number 1 as a template for what you need to do right now and only use the soultion when you get stuck.
- All you have left to do now is test your program to make sure it's working!