/*____________________________________________________________________________________ U S I N G Q U E U E S Ron Kessler Created 7/4/2015 This project shows how to work with a queue which is just like a waiting list at your doctors office. As people enter their name, they are added to the queue. When the Being Served number = patient number we simulate them being called to a treatment room. When they click OK, I append a patient number to their fullname and add it to the queue. Later, we peek into the queue so see the next person in line and if that number I appended matches the "Being Served" number we call the ShowGreeting method. If there are no more patients in the queue the timer is stopped. If someone puts their name in the timer starts up again. ____________________________________________________________________________________ */ #pragma once namespace UsingQueues { using namespace System; using namespace System::ComponentModel; using namespace System::Collections::Generic; //Be sure to add Generic to this namespace! using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Text; //add this for string functions public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); this->CenterToScreen(); //add this } //*************START OF MY CODE************ #pragma region Fields static int custNumber = 11; //we start here static int nowServingNumber = 2; // simulate helping cust #2 String^ firstName; String^ lastName; String^ fullName; int commaPosition; // I append a cust num followed by a comma to the name so we have to strip it off later //---create our queue to hold full names static Queue^ custQueue = gcnew Queue(); #pragma endregion #pragma region Form Load private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { UpdateNumbers(); //---update GUI timer1->Enabled=true; } #pragma endregion #pragma region Button OK Click private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) { if(txtFirst->Text->Trim() != "" && txtLast->Text->Trim() != "") { custNumber +=1; lblCustNum->Text = custNumber.ToString(); firstName = txtFirst->Text->Trim(); lastName = txtLast->Text->Trim(); fullName = firstName + " " + lastName; //---add victim's name and custNumber to the waiting list using the Enqueue method custQueue->Enqueue(custNumber + "," + fullName); //---clear textboxes for faster data entry txtFirst->Clear(); txtLast->Clear(); txtFirst->Focus(); //---start the clock any time they click OK timer1->Start(); } } #pragma endregion #pragma region Timer Tick private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { //--- when the time interval is reached this code runs. I set it for 5 seconds (5000 ms) timer1->Stop(); nowServingNumber +=1; UpdateNumbers(); //---look into the queue and read the next patient's number if there are people waiting if(custQueue->Count > 0) { String^ nextPatientInQueue = custQueue->Peek(); //Peek looks at the next item in the queue commaPosition = nextPatientInQueue->IndexOf(","); //find the position of the comma int nextCustNumber = Convert::ToInt32(nextPatientInQueue->Substring(0, commaPosition)); if(nextCustNumber == nowServingNumber) ShowGreeting(); else timer1->Start(); } else if(custQueue->Count == 0) // No patients? Then decrement the ServingNumber and Update GUI { nowServingNumber -=1; UpdateNumbers(); timer1->Stop(); } } #pragma endregion #pragma region Show Greeting private:System::Void ShowGreeting() { //---grab next patients name if there are people in the queue if(custQueue->Count > 0) { lblGreeting->Text = ""; String^ currentPatient = custQueue->Dequeue(); // this is how you grab an item on the top of the queue lblGreeting->Text = "Welcome " + currentPatient->Substring(commaPosition +1)->ToString() + txtLast->Text->ToString() + ". I hope you brought a lot of money because treatment at this place costs a lot!"; //---keep counting timer1->Start(); } } #pragma endregion #pragma region Update GUI private:System::Void UpdateNumbers() { lblNowServing->Text = nowServingNumber.ToString(); lblCustNum->Text = custNumber.ToString(); } #pragma endregion }; }