Welcome back.
In this video we're going to see how we can use complex objects in lists and use lists to manage complex
objects.
Therefore I'm going to create a new class.
I'm going to use the namespace list app directly.
So I'm not going to create a separate file.
You can create classes inside of one file and multiple classes so to speak.
It's not great practice though, so it really is better practice to have separate files.
I just wanted to state that.
But for this particular example where I'm just showing you the ideas behind it, it's totally fine.
So it's still going to work.
So I'm going to have a class that I'm going to call products.
And now in the body of the product I'm going to have a name as well as let's say, a price.
Okay.
So we're just going to have two properties for our product.
Now obviously this could be a lot more.
We could have 1520 whatever.
But I'm going to keep it very simple.
I'm just going to have these two details about a product.
Now we're going to use our main method here in order to create a list of products.
So here we're creating a products list of type products.
You see we cannot just use strings and ints.
We can use data types that we have defined ourselves.
So this is our own little class product that we're using.
And now we can create objects of that class.
And we're going to create a list of that complex data type.
Now the thing is I can obviously go ahead and say products dot add and then add a new product.
And I'm going to do that.
So I'm going to give the product, let's say berries and a price of 2.99 okay.
And this has to be a dot.
There we are.
So this will be our new product and we're adding it to our products here.
I can however also just use curly brackets and initialize our products list with product items straight
away.
So I can just go ahead and say new product.
And then inside of my brackets, I can then define what the name of a product is going to be.
So Apple and what the price is going to be.
So price is going to be let's say an apple is $0.80 for example.
Okay.
And that's how you can create a object of our class straight away.
So you see it's it's different from what we have seen in the past where if we created a product so product
new product, then we would go ahead and add details like the properties maybe directly into the constructor,
but we can access the properties because they are public.
We can access them straight away using the curly brackets like so.
So when creating a new product, we can just use curly brackets here and then assign values to the individual
properties.
So now we can go ahead and create new products like we want.
So let's create two more.
So we have apples bananas and let's say cherries.
And now let's change the price.
So bananas let's say one banana is $0.30.
And well a pound of cherries let's say is five or a pound would be let's say $3.80 or something like
that.
Okay.
So now we have our list of products.
And you've seen a different way of creating products, right.
Or of complex data types of our own class that we created.
Now let's go ahead and display the individual items that we have inside of our products.
So I'm going to say available products colon.
And then I'm going to use a for each loop in order to iterate through all products.
So you can see here product product in products.
And then I can write the product name and the product price.
However I'm going to do it slightly differently.
I'm going to use a string here with a dollar sign.
So product name the product name.
And here we need to use curly brackets around the product name because this is code for.
And then I'm going to display the product price.
So here again we're going to use the product dot price okay.
So that's what the line is going to look like.
And this will now allow me to display all items.
So all products that are inside of my products list.
So let's run this.
And there we are available products.
Product name Apple for $0.80 0.3 and so forth.
So there we have how we can create a list of a complex data type, how we can add items to the list
of complex data types and how we can access.
The complex data types, content, all of them of a list.
And that's pretty much it for this video.
So another weapon for your arsenal.
In the next video, we're going to see how we can filter items from our project.
And in the next video we're going to see how we can filter items using link.
But we're just going to touch link really quickly.
We're going to go a lot more into depth in the link chapter, but I still think it's really nice to
give you a little bit of an idea as to how you can filter your more complex product list in the next
video, so see you there.