diff --git a/QueueClass.cpp b/QueueClass.cpp new file mode 100644 index 0000000..fe6dbb7 --- /dev/null +++ b/QueueClass.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +class Queue { + private: + node* front; + node* rear; + + public: + Queue() { + front = NULL; + rear = NULL; + } + void joinQueue(int Id); + void serveCompleted(vector& agents); + void showQueue(); + }; + void Queue::joinQueue(int Id) { + node* newUser = new node(Id); + if (rear == NULL) { + front = rear = newUser; + } else { + rear->next = newUser; + rear = newUser; + } + cout << "User " << Id << " joined the queue.\n"; + } + void Queue::serveCompleted(vector& agents) { + if (front == NULL) { + cout << "No users in queue.\n"; + return; + } + for (auto& agent : agents) { + if (agent.isAvailable) { + node* temp = front; + front = front->next; + if (front == NULL) rear = NULL; + + agent.isAvailable = false; + + cout << "Agent " << agent.ID << " is now serving User " << temp->userID << "\n"; + delete temp; + return; + } + } + cout << "No available agents at the moment. Please wait.\n"; + } + + void Queue::showQueue() { + if (front == NULL) { + cout << "Queue is empty.\n"; + return; + } + + cout << "Current Queue: "; + node* temp = front; + while (temp != NULL) { + cout << temp->userID << " "; + temp = temp->next; + } + cout << "\n"; + } + \ No newline at end of file diff --git a/agentNode.cpp b/agentNode.cpp index ef9bbf5..f60a88c 100644 --- a/agentNode.cpp +++ b/agentNode.cpp @@ -1,12 +1,26 @@ #include using namespace std; class Agent{ - private: - int ID; - node* next; public: + int ID; + bool isAvailable; Agent(int id){ ID = id; - next = NULL; + isAvailable = true; } -}; \ No newline at end of file +}; + +void releaseAgent(vector& agents, int agentId) { + for (auto& agent : agents) { + if (agent.ID == agentId) { + if (!agent.isAvailable) { + agent.isAvailable = true; + cout << "Agent " << agent.ID << " is now available.\n"; + } else { + cout << "Agent " << agent.ID << " is already available.\n"; + } + return; + } + } + cout << "Agent ID not found.\n"; +} diff --git a/code.cpp b/code.cpp new file mode 100644 index 0000000..434c21b --- /dev/null +++ b/code.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; +class node{ + public: + int userID; + node* next; + node(int id){ + userID = id; + next = NULL; + } +}; +class Agent{ + public: + int ID; + bool isAvailable; + Agent(int id){ + ID = id; + isAvailable = true; + } +}; + + + +class Queue { + private: + node* front; + node* rear; + + public: + Queue() { + front = nullptr; + rear = nullptr; + } + void joinQueue(int Id); + void serveCompleted(vector& agents); + void showQueue(); + }; + void Queue::joinQueue(int Id) { + node* newUser = new node(Id); + if (rear == nullptr) { + front = rear = newUser; + } else { + rear->next = newUser; + rear = newUser; + } + cout << "User " << Id << " joined the queue.\n"; + } + void Queue::serveCompleted(vector& agents) { + if (front == NULL) { + std::cout << "No users in queue.\n"; + return; + } + for (auto& agent : agents) { + if (agent.isAvailable) { + node* temp = front; + front = front->next; + if (front == NULL) rear = NULL; + + agent.isAvailable = false; + + cout << "Agent " << agent.ID<< " is now serving User " << temp->userID << "\n"; + delete temp; + return; + } + } + cout << "No available agents at the moment. Please wait.\n"; + } + + void Queue::showQueue() { + if (front == NULL) { + cout << "Queue is empty.\n"; + return; + } + + cout << "Current Queue: "; + node* temp = front; + while (temp != nullptr) { + cout << temp->userID << " "; + temp = temp->next; + } + cout << "\n"; + } + + + + +void releaseAgent(vector& agents, int agentId) { + for (auto& agent : agents) { + if (agent.ID == agentId) { + if (!agent.isAvailable) { + agent.isAvailable = true; + cout << "Agent " << agent.ID<< " is now available.\n"; + } else { + cout << "Agent " << agent.ID << " is already available.\n"; + } + return; + } + } + cout << "Agent ID not found.\n"; +} + + + +int main() { + Queue chatQueue; + int choice, Id = 1; + int numAgents; + + cout << "Enter number of agents: "; + cin >> numAgents; + + vector agents; + for (int i = 1; i <= numAgents; ++i) { + agents.push_back(Agent(i)); + } + + while (true) { + cout << "\n--- Chat Queue Menu ---\n"; + cout << "1. New user joins\n"; + cout << "2. Serve next user\n"; + cout << "3. Release agent\n"; + cout << "4. Show queue\n"; + cout << "5. Exit\n"; + cout << "Enter choice: "; + cin >> choice; + + switch (choice) { + case 1: + chatQueue.joinQueue(Id++); + break; + case 2: + chatQueue.serveCompleted(agents); + break; + case 3: { + int Id; + cout << "Enter Agent ID to release: "; + cin >> Id; + releaseAgent(agents, Id); + break; + } + case 4: + chatQueue.showQueue(); + break; + case 5: + return 0; + default: + std::cout << "Invalid choice.\n"; + } + } +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4e75e8c..870d96a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,47 @@ -#include -using namespace std; -int main(){ - cout<<"DS project"<> numAgents; + + vector agents; + for (int i = 1; i <= numAgents; ++i) { + agents.push_back(Agent(i)); + } + + while (true) { + cout << "\n--- Chat Queue Menu ---\n"; + cout << "1. New user joins\n"; + cout << "2. Serve next user\n"; + cout << "3. Release agent\n"; + cout << "4. Show queue\n"; + cout << "5. Exit\n"; + cout << "Enter choice: "; + cin >> choice; + + switch (choice) { + case 1: + chatQueue.joinQueue(Id++); + break; + case 2: + chatQueue.serveCompleted(agents); + break; + case 3: { + int Id; + cout << "Enter Agent ID to release: "; + cin >> Id; + releaseAgent(agents, Id); + break; + } + case 4: + chatQueue.showQueue(); + break; + case 5: + return 0; + default: + cout << "Invalid choice.\n"; + } + } +} diff --git a/queueNode.cpp b/queueNode.cpp index 7941b48..b5e1aa3 100644 --- a/queueNode.cpp +++ b/queueNode.cpp @@ -2,12 +2,11 @@ #include using namespace std; class node{ - private: - string userName; - node* next; public: - node(string name){ - userName = name; + int userID; + node* next; + node(int id){ + userID = id; next = NULL; } -} \ No newline at end of file +}; \ No newline at end of file diff --git a/text2.txt b/text2.txt index a66990d..0734bc8 100644 --- a/text2.txt +++ b/text2.txt @@ -1,3 +1,5 @@ second day of ds project agent node created -third day working on project \ No newline at end of file +third day working on project +fourth day +fifth day yestarday commit not completed \ No newline at end of file