-
Notifications
You must be signed in to change notification settings - Fork 0
/
Person.cpp
430 lines (375 loc) · 12.1 KB
/
Person.cpp
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <iostream>
#include <cmath>
#include "Person.h"
#include "VariadicTable.h"
#include <vector>
std::vector<Person> records;
static size_t ID{0};
// prototypes
Person *findPersonById(int);
//
void increase_ID()
{
ID++;
}
void decrease_ID()
{
ID--;
}
// prototypes
int update_selection_handler();
template <typename T>
void input_check(std::istream &stream, T &variable)
{
while (!(stream))
{
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "\nWrong type please reenter : " << std::endl;
std::cin >> variable;
}
}
size_t gender_check(size_t &variable)
{
std::cin >> variable;
while (variable != 1 && variable != 2)
{
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Wrong choice , please enter 1 or 2 : ";
std::cin >> variable;
}
return variable;
}
void create_person()
{
std::string fullname{};
size_t age{};
size_t gender{};
double height{};
double weight{};
double neck{};
double waist{};
double hip{};
std::cin.get();
std::cout << "Please enter the Fullname : " << std::endl;
std::getline(std::cin, fullname);
std::cout << "Please enter your gender (Male=1,Female=2): " << std::endl;
gender = gender_check(gender);
std::cout << "Please enter the age : " << std::endl;
input_check(std::cin >> age, age);
std::cout << "Please enter the height (cm): " << std::endl;
input_check(std::cin >> height, height);
std::cout << "Please enter the weight (kg): " << std::endl;
input_check(std::cin >> weight, weight);
std::cout << "Please enter the neck circumference ( Widest - cm ) : " << std::endl;
input_check(std::cin >> neck, neck);
std::cout << "Please enter the waist circumference (Thinnest - cm ): " << std::endl;
input_check(std::cin >> waist, waist);
std::cout << "Please enter the hip circumference ( Widest - cm ): " << std::endl;
input_check(std::cin >> hip, hip);
// Person create
Person newPerson{fullname, gender, age, height, weight, neck, waist, hip};
newPerson.set_id(ID);
increase_ID();
records.push_back(newPerson);
std::cout << "\nPerson created" << std::endl;
std::cout << "\n---------Person Info----------" << std::endl;
std::cout << "Fullname : " << newPerson.get_fullname() << std::endl;
std::cout << "Gender : " << newPerson.get_gender() << std::endl;
std::cout << "Age : " << newPerson.get_age() << std::endl;
std::cout << "Height : " << newPerson.get_height() << std::endl;
std::cout << "Weight : " << newPerson.get_weight() << std::endl;
std::cout << "Neck : " << newPerson.get_neck() << std::endl;
std::cout << "Waist : " << newPerson.get_waist() << std::endl;
std::cout << "Hip : " << newPerson.get_hip() << std::endl;
std::cout << "\n";
}
void view_person(Person &p)
{
VariadicTable<size_t, std::string, std::string, int, double, double, double, double, double> vt({"ID", "Name", "Gender",
"Age",
"Height", "Weight", "Neck", "Waist", "Hip"},
10);
vt.addRow(p.get_id(), p.get_fullname(), p.get_gender(), p.get_age(), p.get_height(), p.get_weight(), p.get_neck(), p.get_waist(), p.get_hip());
vt.print(std::cout);
std::cout << "\n";
}
void display_calorie_need(double bmr)
{
std::cout << "-- Daily Calorie (kcal) Need by Activity Level -- \n";
// Daily Calorie Need by Activity Level
VariadicTable<double, double, double, double, double> daily_calorie({"Sedentary", "Lightly (1-3 Days/Week)", "Moderately (3-5 Days/ Week)", "Very Active (6-7 Days/Week)", "Extra Active (Hard Exercise & Job)"}, 10);
daily_calorie.addRow((bmr * 1.2), (bmr * 1.1375), (bmr * 1.55), (bmr * 1.725), (bmr * 1.9));
daily_calorie.print(std::cout);
std::cout << "\n";
}
void view_calculations_for_person(Person &p)
{
std::cout << "\n"
<< std::endl;
double bmr{p.get_bmr()};
VariadicTable<size_t, std::string, double, double, double>
vt({"ID", "Name", "BMR (Basal Metabolic Rate)", "BMI (Body Mass Index)", "Fat Percentage %"}, 10);
vt.addRow(p.get_id(), p.get_fullname(), bmr, p.get_bmi(), p.get_fat_percentage());
vt.print(std::cout);
std::cout << "\n\n";
display_calorie_need(bmr);
}
void view_person_list()
{
VariadicTable<size_t, std::string, std::string, int, double, double, double, double, double> vt({"ID", "Name", "Gender", "Age", "Height", "Weight", "Neck", "Waist", "Hip"}, 10);
if (records.size())
{
for (auto p : records)
{
vt.addRow(p.get_id(), p.get_fullname(), p.get_gender(), p.get_age(), p.get_height(), p.get_weight(), p.get_neck(), p.get_waist(), p.get_hip());
}
vt.print(std::cout);
std::cout << "\n";
}
else
{
std::cout << "\n--- List is empty ---\n"
<< std::endl;
}
}
void view_calculations()
{
view_person_list();
if (records.size() > 0)
{
size_t id{};
std::cout << "\nPlease Enter the id number of the person you want to see calculations : " << std::endl;
input_check(std::cin >> id, id);
Person *thisPerson = findPersonById(id);
if (!thisPerson->get_fullname().empty())
{
view_calculations_for_person(*thisPerson);
}
else
{
delete thisPerson;
}
}
}
Person *findPersonById(int id)
{
bool is_find{false};
size_t index{};
for (auto it = records.begin(); it != records.end(); ++it)
{
if (it->get_id() == id)
{
index = it - records.begin(); // it-records.begin() gives us a index of that element.
is_find = true;
}
}
if (!is_find)
{
std::cout << "\nPerson with ID " << id << " not found.\n"
<< std::endl;
Person *emptyPerson = new Person(); // Boş bir Person nesnesi oluşturulur
return emptyPerson;
}
else
{
return &(records.at(index));
}
}
void update_field(Person &thisPerson, int selection)
{
switch (selection)
{
case 1:
{
std::string fullname{};
std::cout << "\nEnter the new value of fullname :" << std::endl;
std::cin.clear();
std::cin.ignore(10000, '\n');
std::getline(std::cin, fullname);
thisPerson.set_fullname(fullname);
update_field(thisPerson, update_selection_handler());
break;
}
case 2:
{
size_t gender{};
std::cout << "\nEnter the new value of gender (Male=1, Female=2) :" << std::endl;
thisPerson.set_gender(gender_check(gender));
update_field(thisPerson, update_selection_handler());
break;
}
case 3:
{
size_t age{};
std::cout << "\nEnter the new value of age :" << std::endl;
input_check(std::cin >> age, age);
thisPerson.set_age(age);
update_field(thisPerson, update_selection_handler());
break;
}
case 4:
{
double height{};
std::cout << "\nEnter the new value of height :" << std::endl;
input_check(std::cin >> height, height);
thisPerson.set_height(height);
update_field(thisPerson, update_selection_handler());
break;
}
case 5:
{
double weight{};
std::cout << "\nEnter the new value of weight :" << std::endl;
input_check(std::cin >> weight, weight);
thisPerson.set_weight(weight);
update_field(thisPerson, update_selection_handler());
break;
}
case 6:
{
double neck{};
std::cout << "\nEnter the new value of neck :" << std::endl;
input_check(std::cin >> neck, neck);
thisPerson.set_neck(neck);
update_field(thisPerson, update_selection_handler());
break;
}
case 7:
{
double waist{};
std::cout << "\nEnter the new value of waist :" << std::endl;
input_check(std::cin >> waist, waist);
thisPerson.set_waist(waist);
update_field(thisPerson, update_selection_handler());
break;
}
case 8:
{
double hip{};
std::cout << "\nEnter the new value of hip :" << std::endl;
input_check(std::cin >> hip, hip);
thisPerson.set_hip(hip);
update_field(thisPerson, update_selection_handler());
break;
}
case 9:
{
break;
}
default:
std::cout << "\nSorry. Wrong selection. Try again !\n"
<< std::endl;
}
}
int update_selection_handler()
{
int selection{};
std::cout << "\n1 - Edit Fullname " << std::endl;
std::cout << "2 - Edit Gender " << std::endl;
std::cout << "3 - Edit Age " << std::endl;
std::cout << "4 - Edit Height " << std::endl;
std::cout << "5 - Edit Weight " << std::endl;
std::cout << "6 - Edit Neck " << std::endl;
std::cout << "7 - Edit Waist " << std::endl;
std::cout << "8 - Edit Hip " << std::endl;
std::cout << "9- Exit Update Menu " << std::endl;
std::cout << "\nPlease select the field you want to change : " << std::endl;
input_check(std::cin >> selection, selection);
return selection;
}
void update_person()
{
view_person_list();
if (records.size() > 0)
{
size_t id{};
std::cout << "\nPlease Enter the id number of the person you want to update : " << std::endl;
input_check(std::cin >> id, id);
Person *thisPerson = findPersonById(id);
if (!thisPerson->get_fullname().empty())
{
view_person(*thisPerson);
int selection = update_selection_handler();
update_field(*thisPerson, selection);
}
else
{
delete thisPerson;
}
}
}
bool deletePersonByID(size_t &id)
{
for (auto it = records.begin(); it != records.end(); ++it)
{
if (it->get_id() == id)
{
std::cout << "\n ---> "
<< it->get_fullname() << " <--- deleted.\n"
<< std::endl;
records.erase(it);
return true;
}
}
std::cout << "\nPerson with ID " << id << " not found. Nothing removed.\n"
<< std::endl;
return false;
}
void delete_person()
{
view_person_list();
if (records.size() > 0)
{
size_t id{};
std::cout << "\nPlease Enter the id number of the person you want to delete : " << std::endl;
input_check(std::cin >> id, id);
deletePersonByID(id);
}
}
const double Person::bmr_calculation(size_t gender, size_t age, size_t height, size_t weight)
{
if (gender == 1)
{
return 66.5 + (13.75 * weight) + (5.003 * height) - (6.75 * age);
}
else if (gender == 2)
{
return 655.1 + (9.563 * weight) + (1.850 * height) - (4.676 * age);
}
else
{
return 0.0;
}
}
double set_digits_after_decimal_point(double value)
{
double multiplier = 100.0;
// multiplier and static_cast operation for set the value as a 2 digits after decimal point
return static_cast<double>(static_cast<int>(value * multiplier)) / multiplier;
}
const double Person::bmi_calculation(double height, double weight)
{
double value = (weight / ((height / 100) * (height / 100)));
return set_digits_after_decimal_point(value);
}
const double Person::fat_percentage_calculation(size_t gender, double height, double neck, double waist, double hip)
{
double value{0.0};
// us navy fat percentage method
if (gender == 1) // if male
{
value = 495 / (1.0324 - 0.19077 * log10(waist - neck) + 0.15456 * log10(height)) - 450;
return set_digits_after_decimal_point(value);
}
else if (gender == 2) // if female
{
value = 495 / (1.29579 - 0.35004 * log10(waist + hip - neck) + 0.22100 * log10(height)) - 450;
return set_digits_after_decimal_point(value);
}
else
{
return 0.0;
}
}