-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
255 lines (200 loc) · 6.87 KB
/
Copy pathlibrary.cpp
File metadata and controls
255 lines (200 loc) · 6.87 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "Lifeguard.h"
#include "clockthread.cpp"
#ifndef LIFEGUARD_APP_LIBRARY_CPP
#define LIFEGUARD_APP_LIBRARY_CPP
string opt();
void list(vector<Lifeguard> &rot);
void add(vector<Lifeguard> &rot, TimeData timeData);
void AddByName(vector<Lifeguard>& rot, TimeData timeData, string name);
void remove(vector<Lifeguard> &rot, int numofguards = 2);
void UpdateList(vector<Lifeguard>& rot, TimeData timeData, int numofguards = 1);
void ending();
void swap(vector<Lifeguard> &rot);
// Function to print the choices as well as grab their choice
string opt()
{
string input;
// Display the available choices
cout << "What do you want to do?\n"
" List rotation (list) "
" End Program (end)\n"
" Update List (update) "
" Swap Lifeguards (swap)\n"
" Add Lifeguard (add) "
" Remove lifeguard (remove)\n";
// Get the user's choice
cin >> input;
return input;
}
// Lists out the entire rotation
// Showing the name of the lifeguard and when they get on
void list(vector<Lifeguard> &rot)
{
cout << endl;
// Iterate through the rotation and display lifeguard information
for (Lifeguard & i : rot)
{
// Display the lifeguard's name and their shift start time
cout << i.getName() << " - " << setw(2) << setfill('0') << i.getHour() << ":" << setw(2)
<< setfill('0') << i.getMin() << endl;
}
// Call the ending function
ending();
}
// Add a new lifeguard to the rotation
void add(vector<Lifeguard>& rot, TimeData timeData) {
string name;
// Prompt user for lifeguard's name
cout << "What is the name of the lifeguard?" << endl;
cin >> name;
// Check for duplicate name
for (const Lifeguard& guard : rot) {
if (guard.getName() == name) {
cout << "Error: Duplicate name. Lifeguard with this name already exists." << endl;
ending();
return;
}
}
// Calculate rounded minute and hour
int roundedMinute = (timeData.minute < 15) ? 0 : (timeData.minute < 45) ? 30 : 0;
int roundedHour = (timeData.minute >= 45) ? timeData.hour + 1 : timeData.hour;
// Create a new lifeguard instance and add it to the rotation
Lifeguard temp(name, roundedHour, roundedMinute);
rot.push_back(temp);
// If there's more than one lifeguard, adjust their times
if (rot.size() > 1) {
for (int i = 0; i < rot.size() - 1; i++) {
rot[rot.size() - 1].addTime(30);
}
}
// Display success message
cout << "Added Lifeguard to the List" << endl;
// Call the ending function
ending();
}
// Add a new lifeguard to the rotation
void AddByName(vector<Lifeguard>& rot, TimeData timeData, string name)
{
// Check for duplicate name
for (const Lifeguard &guard: rot)
{
if (guard.getName() == name)
{
cout << "Error: Duplicate name. Lifeguard with this name already exists." << endl;
ending();
return;
}
}
// Calculate rounded minute and hour
int roundedMinute = (timeData.minute < 15) ? 0 : (timeData.minute < 45) ? 30 : 0;
int roundedHour = (timeData.minute >= 45) ? timeData.hour + 1 : timeData.hour;
// Create a new lifeguard instance and add it to the rotation
Lifeguard temp(name, roundedHour, roundedMinute);
rot.push_back(temp);
// If there's more than one lifeguard, adjust their times
if (rot.size() > 1)
{
for (int i = 0; i < rot.size() - 1; i++)
{
rot[rot.size() - 1].addTime(30);
}
}
}
// Removes lifeguard from the rotation
void remove(vector<Lifeguard>& rot, int numofguards) {
string name;
// Prompt user for lifeguard's name to remove
cout << "Who needs to be removed?" << endl;
cin >> name;
// Iterate through the rotation to find and remove the lifeguard
auto it = rot.begin();
while (it != rot.end()) {
if (it->getName() == name) {
it = rot.erase(it); // Erase the lifeguard from the rotation
cout << "Instance removed successfully." << endl;
break; // Important: Exit the loop after erasing the element
} else {
++it;
}
}
// Determine the time to subtract based on the number of guards
int timeToSubtract = 0;
if (numofguards <= 2) {
timeToSubtract = 30;
} else if (numofguards == 3) {
timeToSubtract = 20;
} else if (numofguards >= 4) {
timeToSubtract = 15;
}
// Iterate through the remaining lifeguards to subtract time
for (; it != rot.end(); ++it) {
it->subtractTime(timeToSubtract); // method in Lifeguard class to subtract time
}
cout << endl;
// Call the ending function
ending();
}
void UpdateList(vector<Lifeguard>& rot, TimeData timeData, int numofguards) {
if (rot.empty()) {
cout << "Rotation is empty." << endl;
return;
}
Lifeguard removedGuard = rot.front(); // Store the first lifeguard
rot.erase(rot.begin()); // Remove the first lifeguard from the rotation
// Determine the time to subtract based on the number of guards
int timeToSubtract = 0;
if (numofguards <= 2) {
timeToSubtract = 30;
} else if (numofguards == 3) {
timeToSubtract = 20;
} else if (numofguards >= 4) {
timeToSubtract = 15;
}
// Subtract time from the remaining lifeguards
for (Lifeguard &guard : rot) {
guard.subtractTime(timeToSubtract); // Subtract time from each remaining guard
}
AddByName(rot, timeData, removedGuard.getName());
cout << "Updated the rotation." << endl;
ending();
}
// Swaps Two guards based on the names
void swap(vector<Lifeguard>& rot) {
string name1, name2;
cout << "Who are we swapping?" << endl;
cin >> name1;
cout << "Who is the other Lifeguard?" << endl;
cin >> name2;
int index1 = -1;
int index2 = -1;
// Find the indices of the lifeguards to be swapped
for (int i = 0; i < rot.size(); i++) {
if ((index1 != -1) && (index2 != -1)) {
break; // Both indices found, no need to continue searching
}
if (rot[i].getName() == name1 && index1 == -1) {
index1 = i;
}
if (rot[i].getName() == name2 && index2 == -1) {
index2 = i;
}
}
// If both lifeguards are found, swap their names
if (index1 != -1 && index2 != -1) {
string tempName = rot[index1].getName();
rot[index1].setName(rot[index2].getName());
rot[index2].setName(tempName);
cout << "Guards Swapped Successfully" << endl;
ending();
} else {
cout << "One or both lifeguards not found." << endl;
}
}
// Use _getch() from conio.h to hold program till you press enter
void ending()
{
cout << "**Press Enter to Continue**";
_getch();
cout << endl;
}
#endif //LIFEGUARD_APP_LIBRARY_CPP