-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
237 lines (216 loc) · 6.39 KB
/
main.cpp
File metadata and controls
237 lines (216 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class node{
public:
int userID;
node* next;
node(int id){
userID = id;
next = NULL;
}
//~node(){
// delete next;
//}
};
class Agent{
public:
int ID;
bool isAvailable;
Agent(int id){
ID = id;
isAvailable = true;
}
~Agent(){}
};
class Queue {
private:
node* front;
node* rear;
public:
Queue() {
front = NULL;
rear = NULL;
}
~Queue() {
node* current = front;
while (current != NULL) {
node* nextNode = current->next;
delete current;
current = nextNode;
}
}
void joinQueue(int Id);
void serveCompleted(vector<Agent>& agents);
void showQueue();
vector<pair<int,int>> servingList;
void showServingList();
void releaseUser(vector<Agent>& agents, int userId);
bool isUserAlreadyExist(int userId);
};
void Queue::joinQueue(int Id) {
if (isUserAlreadyExist(Id)) {
cout << "Error: User ID " << Id << " is already in the queue or being served.\n";
return;
}
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<Agent>& agents) {
if (front == NULL) {
cout << "No users in queue.\n";
return;
}
bool anyServed = false;
for (auto& agent : agents) {
if (agent.isAvailable && front != NULL) {
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";
servingList.push_back({agent.ID, temp->userID});
delete temp;
anyServed = true;
}
}
if (!anyServed) {
cout << "No available agents at the moment. Please wait.\n";
}
}
bool Queue::isUserAlreadyExist(int userId) {
// Check queue
node* temp = front;
while (temp != NULL) {
if (temp->userID == userId) {
return true;
}
temp = temp->next;
}
// Check serving list
for (const auto& pair : servingList) {
if (pair.second == userId) {
return true;
}
}
return false;
}
void Queue:: releaseUser(vector<Agent>& agents, int userId) {
for (auto it = servingList.begin(); it != servingList.end(); ++it) {
if (it->second == userId) {
int agentId = it->first;
// Find the agent and set them as available
for (auto& agent : agents) {
if (agent.ID == agentId) {
agent.isAvailable = true;
break;
}
}
servingList.erase(it);
cout << "User : " << userId << " has been released. Agent " << agentId << " is now available.\n";
return;
}
}
cout << "User ID : " << userId << " not found in serving list.\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 Queue:: showServingList() {
if (servingList.empty()) {
cout << "No agents are currently serving users.\n";
return;
}
cout << "Currently Serving:\n";
for (const auto& pair : servingList) {
cout << "Agent " << pair.first << " is serving User " << pair.second << "\n";
}
}
void releaseAgent(vector<Agent>& 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, userId;
int numAgents;
cout << "Enter number of agents: ";
cin >> numAgents;
vector<Agent> 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.show serving list\n";
cout<<"6.Release user\n";
cout<<"7.EXIT"<<endl;
cout <<"Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout<<"Enter User ID to join Queue :";
cin>>userId;
chatQueue.joinQueue(userId);
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:
chatQueue.showServingList();
break;
case 6:
int user;
cout<<"enter the user ID to release";
cin>>user;
chatQueue.releaseUser(agents,user);
break;
case 7:
return 0;
break;
default:
cout << "Invalid choice.\n";
}
}
}