Real time example of queue collection class in c#:-
Here is one simple real-time working example where a Queue class can be used.
When you walk into a bank or a passport office, you will collect a token and wait in the queue for your token number to be called. From the application perspective, when a token is issued, the token number will be added to the end of the Queue. When a representative at the counter is available to server a customer, he will push the "Next" button and the token number that is present at the beginning of the queue, will be dequeued. So, this is one example, where a Queue collection class can be effectively used.
WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["TokenQueue"] == null)
{
Queue tok
//---------------------WebForm1.aspx.cs-----------------------
WebForm1.aspx.cs Session["TokenQueue"] = tokenQueue;
}
}
protected void btnCounter1_Click(object sender, EventArgs e)
{
ServerNextCustomer(txtCounter1, 1);
}
protected void btnCounter2_Click(object sender, EventArgs e)
{
ServerNextCustomer(txtCounter2, 2);
}
protected void btnCounter3_Click(object sender, EventArgs e)
{
ServerNextCustomer(txtCounter3, 3);
}
protected void btnPrintToken_Click(object sender, EventArgs e)
{
Queue tokenQueue = (Queue)Session["TokenQueue"];
lblCurrentStatus.Text = "There are " + tokenQueue.Count.ToString()
+ " customers before you in the queue";
if (Session["lastTokenNumberIssued"] == null)
{
Session["lastTokenNumberIssued"] = 0;
}
int nextTokenNumberToIssue = (int)Session["lastTokenNumberIssued"] + 1;
Session["lastTokenNumberIssued"] = nextTokenNumberToIssue;
tokenQueue.Enqueue(nextTokenNumberToIssue);
AddTokenNumbersToListBox(tokenQueue);
}
private void AddTokenNumbersToListBox(Queue tokenQueue)
{
listTokens.Items.Clear();
foreach (int token in tokenQueue)
{
listTokens.Items.Add(token.ToString());
}
}
private void ServerNextCustomer(TextBox textBox, int counterNumnber)
{
Queue tokenQueue = (Queue)Session["TokenQueue"];
if (tokenQueue.Count > 0)
{
int tokenNumberToBeServed = tokenQueue.Dequeue();
textBox.Text = tokenNumberToBeServed.ToString();
txtNextToken.Text = "Token Number : " + tokenNumberToBeServed.ToString()
+ ", please go to Counter " + counterNumnber.ToString();
AddTokenNumbersToListBox(tokenQueue);
}
else
{
textBox.Text = "No cutomers in Queue";
}
}
}
When you walk into a bank or a passport office, you will collect a token and wait in the queue for your token number to be called. From the application perspective, when a token is issued, the token number will be added to the end of the Queue. When a representative at the counter is available to server a customer, he will push the "Next" button and the token number that is present at the beginning of the queue, will be dequeued. So, this is one example, where a Queue collection class can be effectively used.
WebForm1.aspx
Counter 1 | Counter 2 | Counter 3 |
|
|
|
|
|
|
|
||
|
||
|
||
|