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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#include <iostream>
#include <variant>
#include <vector>
#include "Cage.hh"
#include "Mammal.hh"
#include "Zoo.hh"
namespace zoo {
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
} // namespace zoo
int main() {
zoo::Zoo zoo;
std::vector<zoo::Cage> cages;
std::vector<std::variant<zoo::ZooAnimal, zoo::Mammal>> zooAnimals;
// Prompt the user to enter up to ten exhibits at the Zoo
std::cout << "enter up to ten exhibit names (negative number to finish):"
<< std::endl;
for (int i = 0; i < 10; ++i) {
std::string exhibit;
std::cout << "exhibit name #" << i + 1 << ": ";
std::getline(std::cin, exhibit);
if (exhibit.starts_with('-')) {
break;
}
zoo.setExhibit(exhibit);
}
// Prompt the user to enter up to ten cages at the zoo
std::cout << "\nenter up to ten cage numbers (negative number to finish):"
<< std::endl;
for (int i = 0; i < 10; ++i) {
int cageNumber;
zoo::Cage cage(0, 0, "", &zoo);
std::cout << "cage number #" << i + 1 << ": ";
std::cin >> cageNumber;
if (cageNumber < 0) {
break;
}
zoo.setCage(cageNumber);
cage.setCageNumber(cageNumber);
cages.emplace_back(cage);
}
// Prompt the user to add information to each of the cages
if (zoo.getNumCages() > 0) {
std::cout << "\nadd information to each of the cages:" << std::endl;
for (;;) {
int cageNumber;
std::string cageLocation;
int cageSquareFootage;
std::cout << "available cages numbers: ";
for (std::size_t i = 0; i < static_cast<std::size_t>(zoo.getNumCages());
++i) {
std::cout << *zoo.getCage(i) << " ";
}
// Various mechanisms have been put in place to ensure that the user's
// choice of cage to edit is a valid cage number which exists in the zoo
// before it can be added as a cage.
std::cout
<< "\ncage number to edit (negative number to finish, -1 to go one "
"by one): ";
std::cin >> cageNumber;
if (cageNumber == -1) {
for (std::size_t i = 0; i < static_cast<std::size_t>(zoo.getNumCages());
++i) {
std::cout << "cage #" << *zoo.getCage(i) << "'s location: ";
std::cin.ignore();
std::getline(std::cin, cageLocation);
std::cout << "cage #" << *zoo.getCage(i) << "'s square footage: ";
std::cin >> cageSquareFootage;
// Make sure the square footage of the cage is within the 2 to 100000
// range
if (cageSquareFootage < 2 || cageSquareFootage > 100000) {
std::cout << "invalid cage squared footage: must be in range of "
"2-100000, try again."
<< std::endl;
i -= 1;
continue;
}
cages.at(i).setCageLocation(cageLocation);
cages.at(i).setCageSqFt(cageSquareFootage);
}
break;
}
for (std::size_t i = 0; i < static_cast<std::size_t>(zoo.getNumCages());
++i) {
if (*zoo.getCage(i) == cageNumber) {
std::cout << "cage #" << *zoo.getCage(i) << "'s location: ";
std::cin.ignore();
std::getline(std::cin, cageLocation);
std::cout << "cage #" << *zoo.getCage(i) << "'s square footage: ";
std::cin >> cageSquareFootage;
cages.at(i).setCageLocation(cageLocation);
cages.at(i).setCageSqFt(cageSquareFootage);
}
}
if (cageNumber < 0) {
break;
}
}
}
// Prompt the user to add up to twenty animals to the zoo
std::cout << "\nadd up to twenty animals and/ or mammals to the zoo "
"(negative number to finish):"
<< std::endl;
for (int i = 0; i < 20; ++i) {
int idNumber;
int cageNumber;
int dateAcquired;
std::string species;
bool isMammal;
std::string exhibit;
std::string name;
int cageSizeMin;
std::cout << "animal/ mammal #" << i + 1 << " id number: ";
std::cin >> idNumber;
if (idNumber < 0) {
break;
}
std::cout << "animal/ mammal #" << i + 1 << " cage number: ";
std::cin >> cageNumber;
std::cout << "animal/ mammal #" << i + 1 << " date acquired: ";
std::cin >> dateAcquired;
std::cout << "animal/ mammal #" << i + 1 << " species: ";
std::cin.ignore();
std::getline(std::cin, species);
std::cout << "is this animal a mammal? (0 = no, 1 = yes): ";
std::cin >> isMammal;
if (isMammal) {
zoo::Mammal mammal = zoo::Mammal("", "", 0, idNumber, cageNumber,
dateAcquired, species, &zoo);
std::cout << "mammal #" << i + 1 << " exhibit: ";
std::cin.ignore();
std::getline(std::cin, exhibit);
std::cout << "mammal #" << i + 1 << " name: ";
std::getline(std::cin, name);
std::cout << "mammal #" << i + 1 << " minimum cage size: ";
std::cin >> cageSizeMin;
mammal.setExhibit(exhibit);
mammal.setName(name);
mammal.setCageSizeMin(cageSizeMin);
zooAnimals.emplace_back(mammal);
} else {
zoo::ZooAnimal zooAnimal =
zoo::ZooAnimal(idNumber, cageNumber, dateAcquired, species, &zoo);
zooAnimal.setIDNum(idNumber);
zooAnimal.setCageNum(cageNumber);
zooAnimal.setDateAcq(dateAcquired);
zooAnimal.setSpecies(species);
zooAnimals.emplace_back(zooAnimal);
}
}
// Display all the information about the zoo, cage, zoo animals, and mammals
// owned by the zoo
std::cout << "\nzoo:" << std::endl;
if (zoo.getNumExhibits() > 0) {
std::cout << " exhibits:" << std::endl;
for (std::size_t i = 0; i < static_cast<std::size_t>(zoo.getNumExhibits());
++i) {
std::cout << " exhibit #" << i + 1 << " name: " << *zoo.getExhibit(i)
<< std::endl;
}
std::cout << std::endl;
} else {
std::cout << " there were no exhibits at the zoo\n" << std::endl;
}
if (zoo.getNumCages() > 0) {
std::cout << " cages:" << std::endl;
for (std::size_t i = 0; i < static_cast<std::size_t>(zoo.getNumCages());
++i) {
std::cout << " cage #" << i + 1
<< ":\n number: " << cages.at(i).getCageNumber()
<< "\n square footage: " << cages.at(i).getCageSqFt()
<< "\n location: " << cages.at(i).getCageLocation()
<< std::endl;
}
std::cout << std::endl;
} else {
std::cout << " there were no cages at the zoo\n" << std::endl;
}
if (!zooAnimals.empty()) {
std::cout << " animals and/ or mammals:" << std::endl;
for (std::size_t i = 0; i < zooAnimals.size(); ++i) {
std::visit(zoo::overloaded{
[&](zoo::Mammal &animal) {
std::cout
<< " mammal #" << i + 1
<< ":\n id number: " << animal.getIDNum()
<< "\n cage number: " << animal.getCageNum()
<< "\n date acquired: " << animal.getDateAcq()
<< "\n species: " << animal.getSpecies()
<< "\n exhibit: " << animal.getExhibit()
<< "\n name: " << animal.getName()
<< "\n minimum cage size: "
<< animal.getCageSizeMin();
},
[&](zoo::ZooAnimal &animal) {
std::cout
<< " animal #" << i + 1
<< ":\n id number: " << animal.getIDNum()
<< "\n cage number: " << animal.getCageNum()
<< "\n date acquired: " << animal.getDateAcq()
<< "\n species: " << animal.getSpecies();
}},
zooAnimals.at(i));
std::cout << std::endl;
}
}
if (!zooAnimals.empty()) {
// Let the user choose to see all information about a specific animal, by
// identification number
std::cout << "\nanimal ids:";
for (auto &zooAnimal : zooAnimals) {
std::visit(
[](auto &&animal) { std::cout << "\n " << animal.getIDNum(); },
zooAnimal);
}
std::cout << std::endl;
for (;;) {
int idNumber;
std::cout << "id number to view (negative number to finish): ";
std::cin >> idNumber;
if (idNumber < 0) {
break;
}
for (auto &zooAnimal : zooAnimals) {
std::visit(zoo::overloaded{
[&](zoo::Mammal &animal) {
if (animal.getIDNum() == idNumber) {
std::cout
<< "mammal #" << idNumber
<< ":\n id number: " << animal.getIDNum()
<< "\n cage number: " << animal.getCageNum()
<< "\n date acquired: " << animal.getDateAcq()
<< "\n species: " << animal.getSpecies()
<< "\n exhibit: " << animal.getExhibit()
<< "\n name: " << animal.getName()
<< "\n minimum cage size: "
<< animal.getCageSizeMin() << std::endl;
}
},
[&](zoo::ZooAnimal &animal) {
if (animal.getIDNum() == idNumber) {
std::cout
<< "animal #" << idNumber
<< ":\n id number: " << animal.getIDNum()
<< "\n cage number: " << animal.getCageNum()
<< "\n date acquired: " << animal.getDateAcq()
<< "\n species: " << animal.getSpecies()
<< std::endl;
}
}},
zooAnimal);
}
}
} else {
std::cout << "there were no animals at the zoo" << std::endl;
}
if (!zooAnimals.empty()) {
std::size_t mammalCount = 0;
std::cout << "\nmammals names:";
for (auto &zooAnimal : zooAnimals) {
std::visit(
[&mammalCount](auto &&animal) {
if constexpr (std::is_same_v<std::decay_t<decltype(animal)>,
zoo::Mammal>) {
std::cout << "\n " << animal.getName();
mammalCount += 1;
}
},
zooAnimal);
}
if (mammalCount == 0) {
std::cout << " there were no mammals at the zoo";
}
std::cout << std::endl;
}
return 0;
}
|