- [Instructor] In the last lesson,
we added code to the store to use a storage class
that implements the chain of responsibility design pattern.
In this lesson, we'll code the storage class so that
we can search for inventory items in multiple locations.
I'm inside of the exercise files for chapter four,
chapter four lesson two,
and I'm inside of the start folder.
Within this folder I'm going to go ahead
and add a new file called storage.js.
And in our storage file I'm going to
go ahead and create a new class, storage,
and we'll go ahead and give it a constructor.
And this class will take in the name of the storage location
and also the inventory for that location.
And then finally we also are taking in a delivery time.
And I am going to default the delivery time to zero
and the inventory to an empty array
in case those values are not sent.
Those this.name is going to equal our name,
this.inventory is going to equal our inventory,
and this.deliverytime will equal the delivery time.
And then finally we're going to set up another variable.
And the next variable that we're going to set up is this.next.
So this is going to point to the next
storage location that we should search
if the inventory isn't found in the current one.
Right now I'm going to set this to null,
because we don't have a next value.
So now I'm going to add a set next method to my storage class,
and the set next is going to expect another storage class.
So this.next is going to be equal to the storage
object that was sent to the set next method.
And then finally we're going to need to implement a fine.
And we're looking for items in the inventory by name.
So what I'm going to do to find inventory items
is I'm going to create a new method
called look in local inventory.
And for look in local inventory
we're going to find the item much like we
were looking for the item in the store.
That's by locating the index of the item, if we have one,
by taking this.inventory, which remember,
inventory consists of arrays of items,
so we have a name and a quantity on those items.
So we're going to take this.inventory
and we're going to map it, we're going to create a new array
of just the item name.
So this will give me an array
of all of the item names, all of the string names
that are found in this location's inventory.
And then I can use the index of
to just grab the one that matches our item.
And what I'm going to go ahead and do
is now use the index to return the item.
So if we have an item it will get
returned from the look in local inventory.
If we don't have the item we will
return it undefined from look in local inventory.
So the next thing we're going to do
is we're going to modify our find method
to first look in the local inventory.
So I'm going to create a variable called found,
and to find the item I'm going
to look in our local inventory.
And I'll just pass at the item name
that's sent to this storage's find method.
So if we find the item,
what we're going to want to do is return it.
Otherwise we're going to want to look in the next location.
So if we do have an item that
we found in the local inventory,
let's just go ahead and return it.
So name and quantity are the main data points for the item,
but the nice thing about he storage object
is we can also add some information
about where we found the item, like it's location.
And we found the item in this.name,
so this storage location is where the item was found.
We can also add how long it will take the item
to get there, so delivery time.
And we'll do a little if statement for this,
so if this.deliverytime is equal to zero
then we will go ahead and return the string now.
So meaning, if there's no days for delivery time,
we can tell the customer that they can get the item now.
Otherwise what we're going to go ahead and do
is return this.deliverytime,
and I will go ahead and add to this string days.
So now we're sending the delivery time as a string,
and the delivery time is either going to be now
or a certain number of days.
Now here's the thing.
If we don't find an item in the local inventory
for this storage object, then we want to
look into the next inventory if there is one.
So what we're going to go ahead and do
is actually add an else if here.
And if this storage object has a next object,
then we want to make sure we look there.
So I'm going to go ahead and return this.next.find item name.
Now if this storage object doesn't have a next item
and we haven't found the item itself,
what we want to do is return a string
saying that we don't carry this item.
Because we cannot find it in any
of the storage locations at this point.
So this is our storage object,
and remember, the store chains together the storage objects
using the set next methods.
So line eight to 11, we create new storage locations,
and then lines 13 through 15,
we chain those storage locations together.
So finding an item will start with
the current store's find method,
but it will look through all of these storage locations.
Let's take a look at our index.js file.
So now, if we're looking for the item ski poles,
which previously returned an undefined in the last lesson,
let's go ahead and see if we can find it.
Node index.js.
And it looks like we cannot find our storage constructor,
so I think I made my favorite mistake to make all the time,
which is forgetting to export storage
from the storage.js module.
So I'm going to go ahead and export that,
and then we can try our code again.
Clear out the screen and then node index.js.
And it looks like we found our ski poles.
Where did we find them?
In the store's backroom.
When can we have these ski poles? Now.
So let's go ahead and search for another item
inside of our index.js.
We can do our previous search for ski hats.
And we can come out and run the file.
And we can see that we have 15 ski hats on the store floor.
We can also get those items now.
Let's search for something that's not within this store.
So I'm going to go ahead and search for wax,
and if we look at the inventory
we can see that we don't have ski wax
in the floor or in the backroom,
but we do have some in the local store.
So let's see what our results return.
And we can see that we have found it in the local store
and it will take one day to get here.
Finally, let's search for something
that we only have in the warehouse, which is powder skis.
And if we go search for powder skis
and run our application,
we can see that we have 10 powder skis,
but they're located in the warehouse
so it will take them five days to get here.
So all of this is possible
because the store itself implements storage,
and the storage object implements
the chain of responsibility design pattern.