‫In this video, we are going to cover cues and we're going to see how to use queues in our applications.
‫Queues are significant and used a lot by our operating system, for example, to handle tasks, scheduling
‫and many other processes.
‫Queues should be used when the order of the data is important, as we will see in our example.
‫So let's go ahead and first of all, define a queue.
‫In order to define a queue, you can use the queue.
‫Keyword queues are also part of the generic collections, which means we need to define what type a
‫queue will incorporate.
‫So in this case it will hold a bunch of integers, but it could hold any other type as well.
‫It could hold strings, it could hold even a whole objects.
‫So you are fully free to define whatever you want this item to hold.
‫So queue then less, then sign into greater sign and then the name of the queue equals.
‫And then you can initialize it straight away as we do here, where we just create an empty queue that
‫holds integers.
‫So now we can go ahead and add items to this queue.
‫In order to add items to a queue, you can use the end queue method.
‫The queue method adds an object to the end of the queue.
‫So if I add an item like one, for example, this item will be the first one because well, it's the
‫first one that we include, but it will also be the last one.
‫So as soon as we add another one, the new item will be the last item in the queue.
‫So that's how ordering is done.
‫It's similar to a stack, but the difference is that a queue when you get rid of something, so when
‫you pop an item, it's going to take it from the beginning of the queue.
‫So the item that was created first will be taken away or will be popped out of the queue, so to speak.
‫So in order to look at the queues items, we can use the peak method and this peak method will display
‫the first item in the queue, so the front of the queue, so to speak.
‫So it's like a queue of people where you're at the counter and you want to order something, for example,
‫then the peak method will give you the first person in that queue that is about to order.
‫So let's run this real quick and see how this is going to look like.
‫And we see the value at the front of the queue is one.
‫Now let's add a couple more values to see how that's going to affect our application.
‫So here I'm going to add the different Q items like so I'm going to end Q one then in Q2 and Q3, and
‫I'm just using these numbers one, two, three.
‫There could be any other numbers as well.
‫It doesn't really have to be one, two, three.
‫So with peaking, we get the item that is at the beginning of the queue.
‫You can see every single time, no matter what we add to the Q, the one is going to be our first item
‫in the Q, so it's going to be the one displayed when we use the peak method.
‫Now in order to get rid of an item from the queue, you would use the queue method.
‫So here.
‫Q dot rd q so this D q will now get rid of the first integer of our Q and it will also store it straight
‫away so we could store this let's call this one Q item, for example, and it will then store the result
‫of this dict item.
‫So basically it will store one in there because that's the first item that we have in the list.
‫And then Peking will display two because now one is not there anymore.
‫The first item is gone.
‫So now the value in the Q will be two because well, we just decode the first item.
‫So it's similar to pop in a stack, but it takes away the item at the beginning of the Q instead of
‫the end of the Q or the end of the stack, however you want to look at it.
‫You can, of course also try to queue an item at the beginning.
‫If there are no elements inside of the queue, but you will run into an invalid operation exception.
‫The queue is empty so you will need to check if the queue is empty or not before running this.
‫So here we can run the queue method because we know that we just encode something.
‫But otherwise you would use the count property of your queue in order to figure out how many items you
‫have there.
‫So this is the property that you can use, gets the number of elements contained in the queue and you
‫can then check if that one is greater than zero, for example, to check if there is anything in the
‫queue that you can, for example, the queue.
‫So in this particular example, I'm just going to go ahead and use my.
‫Do you count to double check in a while loop?
‫And for every single item I'm going to queue it and also write down the amount of items that are still
‫in the queue and also the item that was removed.
‫So this line here will remove the item by calling the queue method and at the same time it will display
‫the item right here because that's what we're using here.
‫We're using the string, right?
‫And with this position zero, it will display whatever is the result of the queue call.
‫And as you can see here, the queue method will return an integer.
‫So we are going to display a number here.
‫Okay.
‫So let me get rid of this cue that I used here in order to demonstrate how to recue an item.
‫And let's run it again.
‫And you can see the value at the front, this one.
‫And then we basically start the Y loop here.
‫That's the interesting part.
‫So one word is removed from the queue.
‫Now we only have two items left to was removed from the queue.
‫Now we only have one item left and three wasn't removed from the queue.
‫Now we only have zero items left.
‫So basically the queue is empty at that point.
‫All right.
‫So let's look at a more or less real world example.
‫Of course, we don't work with databases in this simple example here, but let's say we have an e-commerce
‫platform and we're the seller and we receive a bunch of orders.
‫These orders should be processed so that the server's time.
‫So the waiting time for the customer should be held to a minimum.
‫This is why we are going to use queues because they're very practical since we care about the order
‫of the orders coming in.
‫So the order of orders, it's a little of a tongue twister here, but the order of which the data was
‫received and by data I mean the orders themselves.
‫So let's look at this very simple class that we're going to build here, and I'm going to put it inside
‫the same file here.
‫This class order with an order ID property, the order quantity property, and then the constructor,
‫which basically just sets those two values when we create an order object and then we have this print
‫message or this process order method which will print the order ID of the item that we're currently
‫looking at, so of the order that we are currently looking at.
‫So now what I would like to do is I would like to actually create methods inside of my program class.
‫So I'm going to put it in here inside of this bracket still and the methods they are going to take care
‫of orders from the different branches.
‫So let's say we have two different branches where we can get orders from.
‫Let's say we are selling on Amazon and at the same time at Best Buy, for example.
‫And then we will now get those orders.
‫Now we're not going to make it too complicated where we are going to check which branch is, well,
‫the order of the different branches.
‫This is something you could, of course, see as a little challenge for yourself if you want to develop
‫that.
‫But we're just going to say, okay, give me the orders from Branch one in the right order and give
‫me the orders from Branch two in the right order as well.
‫Okay.
‫So here this will be Branch one with a bunch of orders and then we have the branch two with a bunch
‫of orders.
‫So we're getting the data.
‫Let's say we get this data from a database where we are checking, okay, what were the orders?
‫And we would probably check them by date.
‫So each order would get a date as well.
‫Here we have the order ID, but usually we would also try to store when it was ordered and so forth.
‫But we're going to keep it simple for this example.
‫So we have now an array of orders in both of those examples.
‫And when we call the method, receive orders from branch one and two, they will basically just re turn
‫and order array.
‫So here an array of orders.
‫So that's what this statement here does, return orders.
‫Now in our particular example, the order ID is in fact going to be unique for the two different orders.
‫So they will already be in the right order, so to speak.
‫So this is order one, this is order two.
‫Then we have order three and order four and five from branch two.
‫And then in the evening, in Branch one, there came another order and somebody purchased ten of our
‫items.
‫Now in our main method we can create a queue and let me actually get rid of all of this stuff here.
‫In our main method, we can now go ahead and create a queue of orders.
‫So here queue order.
‫You can see that this is not going to be a simple type like integer now, but it's actually going to
‫hold a bunch of orders.
‫That's the cool thing about the queue.
‫So it's not just simple types and I'm going to call this one order queue and I'm going to initialize
‫it straight away right here.
‫And then at this point, we can go ahead and and queue all of our orders that we get from Branch one
‫by using this for loop here, this for each loop.
‫So we're going through every single order that I'm going to call.
‫Oh, in this for each loop and this iteration in our receive orders from branch one.
‫Now this here will be of type order array.
‫So when we call this method, it will return an array of orders as you see here.
‫So basically this for each loop will go through the orders that we have inside of this order array and
‫it will look through every single one of them.
‫So all will be first this order, then it will be this order, then it will be that order.
‫And for every one of them it's going to end queue those orders.
‫So that's what this for each loop does.
‫And then we create another for each loop for the other branch as well.
‫So.
‫Same goes here for each loop or in orders received from branch two.
‫So this is Branch two.
‫This is branch one.
‫And the same applies.
‫So the same concept applies.
‫So to process the orders, we will go through our queue one by one, and then we will remove the elements
‫from our queue and process them until our queue is empty and all orders are finally processed.
‫That's why we have this process order method here, which will process the orders.
‫So now we can go ahead and instead of our main method up here, once we have the orders queue with all
‫of the items in it, we can go through it and we can check if the order queue is not empty and as long
‫as it's not empty.
‫So as long as the orders queue count is greater than zero, we are going to execute this while loop
‫and we're going to remove the order at the front of the queue.
‫So the order that came in first, so to speak, and store it in a variable called current order.
‫So we have this order object which is going to be called Current Order, which will basically be the
‫result of the queue in the first item of our orders queue.
‫So basically taking away this item first, then this item, then that item, then this item, then that
‫and then that one.
‫And then it will process each and every one of them step by step.
‫So processing and our case will just be something very simple.
‫It will just print it onto the console.
‫But of course, you could run something way more complex in the background here.
‫This could trigger some events sending out an email, for example, to an employee who has time to take
‫care of it or whatever processing system you have employed in your company.
‫All right.
‫And yeah, that's basically it.
‫So now let's run this application to see the results.
‫And we can see order one was processed.
‫Order two was processed, then order six, then three, four, five.
‫So we are processing the orders.
‫Of course, in the order that we queued them with that we end queue them.
‫You can see here we are in queuing from branch 1/1 and then from branch two.
‫So just imagine for the following scenario where the orders that are created in the system internally,
‫but they first have to be accepted and fully pushed to the processing entity, whatever that entity
‫is, like the another office or so, for example.
‫And they do it at the end of the day, for example.
‫So first branch one finishes their stuff and they say, okay, well we accept those orders.
‫And then later that day, because let's say they are in a different time zone.
‫For example, Branch two says, Yeah, we are agreeing.
‫Well, we send out our orders as approved at that point as well, and then they are started to being
‫processed.
‫So that's how this example could be considered, for example, right?
‫Otherwise, of course, you would order them straight away based on their ID or based on the time that
‫they were created.
‫All right.
‫So now you have a certain idea of how these orders work and how the queues work.
‫That's the more important part.
‫So now you can create your own queues and use them in your applications.
‫All right.
‫So I hope you enjoyed this video.
‫See you in the next one.