Last active
August 22, 2016 14:33
-
-
Save tkaczenko/d44cc74e0f1382bd5157f47f65ebcb9f to your computer and use it in GitHub Desktop.
Serialization with Qt. This class calculates several personal norms for healthy lifestyle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Profile.h" | |
Profile::Profile() | |
{ | |
} | |
Profile::Profile(float w, short h, short age, QString g, float a, int calorie) | |
{ | |
setWeigth(w); | |
setHeigth(h); | |
setAge(age); | |
setGender(g); | |
setActivity(a); | |
setCalorie(calorie); | |
} | |
Profile::~Profile() | |
{ | |
} | |
void Profile::calculateNorms() | |
{ | |
double bmi = Profile::calculateBMI(); | |
Profile::setBMI(bmi); | |
QString conclusion = Profile::conclusionBMI(bmi); | |
Profile::setConclusion(conclusion); | |
double minw, maxw; | |
Profile::calculateOptimalWeight(minw, maxw); | |
Profile::setOptimalWeightMin(minw); | |
Profile::setOptimalWeightMax(maxw); | |
double calorie = Profile::calculateBMR(); | |
Profile::setBMR(calorie); | |
double water = Profile::calculateWater(); | |
Profile::setWater(water); | |
} | |
double Profile::calculateBMI() | |
{ | |
double h = static_cast<double>(height) / 100; | |
return static_cast<double>(weight) / (h * h); | |
} | |
QString Profile::conclusionBMI(double res) | |
{ | |
QString conclusions[7] = { tr("Выраженный дефицит массы тела"), | |
tr("Недостаточная масса тела"), | |
tr("Норма"), | |
tr("Избыточная масса тела"), | |
tr("Ожирение первой степени"), | |
tr("Ожирение второй степени"), | |
tr("Ожирение третьей степени") }; | |
QString temp; | |
res = std::floor(res * 100 + 0.5) / 100; | |
if (res < 16) | |
temp = conclusions[0]; | |
if (res >= 16 && res < 18.5) | |
temp = conclusions[1]; | |
if (res >= 18.5 && res < 24.99) | |
temp = conclusions[2]; | |
if (res >= 25 && res < 30) | |
temp = conclusions[3]; | |
if (res >= 30 && res < 35) | |
temp = conclusions[4]; | |
if (res >= 35 && res < 40) | |
temp = conclusions[5]; | |
if (res >= 40) | |
temp = conclusions[6]; | |
return temp; | |
} | |
void Profile::calculateOptimalWeight(double &min, double &max) | |
{ | |
float h = (float) height / 100; | |
min = (double) 18.5 * h * h; | |
max = (double) 25 * h * h; | |
} | |
double Profile::calculateBMR() | |
{ | |
double res; | |
if (gender == "male") | |
res = 9.99 * weight + 6.25 * height - 4.92 * age + 5; | |
if (gender == "female") | |
res = 9.99 * weight + 6.25 * height - 4.92 * age - 161; | |
return res * activity; | |
} | |
double Profile::calculateWater() | |
{ | |
double res; | |
if (gender == "male") | |
res = 35 * weight; | |
if (gender == "female") | |
res = 31 * weight; | |
return res / 1000; | |
} | |
void Profile::setWeigth(float w) | |
{ | |
weight = w; | |
} | |
float Profile::getWeigth() const | |
{ | |
return weight; | |
} | |
void Profile::setHeigth(short h) | |
{ | |
height = h; | |
} | |
short Profile::getHeigth() const | |
{ | |
return height; | |
} | |
void Profile::setAge(short age) | |
{ | |
this->age = age; | |
} | |
short Profile::getAge() const | |
{ | |
return age; | |
} | |
void Profile::setGender(QString g) | |
{ | |
gender = g; | |
} | |
QString Profile::getGender() const | |
{ | |
return gender; | |
} | |
void Profile::setActivity(float w) | |
{ | |
activity = w; | |
} | |
float Profile::getActivity() const | |
{ | |
return activity; | |
} | |
void Profile::setCalorie(int calorie) | |
{ | |
this->calorie = calorie; | |
} | |
int Profile::getCalorie() const | |
{ | |
return calorie; | |
} | |
void Profile::setBMI(double temp) | |
{ | |
bmi = temp; | |
} | |
double Profile::getBMI() const | |
{ | |
return bmi; | |
} | |
void Profile::setConclusion(QString temp) | |
{ | |
conclusion = temp; | |
} | |
QString Profile::getConclusion() const | |
{ | |
return conclusion; | |
} | |
void Profile::setOptimalWeightMin(double temp) | |
{ | |
minWeight = temp; | |
} | |
double Profile::getOptimalWeightMin() const | |
{ | |
return minWeight; | |
} | |
void Profile::setOptimalWeightMax(double temp) | |
{ | |
maxWeight = temp; | |
} | |
double Profile::getOptimalWeightMax() const | |
{ | |
return maxWeight; | |
} | |
void Profile::setBMR(double temp) | |
{ | |
bmr = temp; | |
} | |
double Profile::getBMR() const | |
{ | |
return bmr; | |
} | |
void Profile::setWater(double temp) | |
{ | |
water = temp; | |
} | |
double Profile::getWater() const | |
{ | |
return water; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PROFILE_H | |
#define PROFILE_H | |
#include <QCoreApplication> | |
#include <QString> | |
#include <QDataStream> | |
/*! | |
* \brief Serialized class for user's profile and calculating personal norms. | |
* | |
* This class calculates the all personal norms. | |
* A list of norms: | |
* - BMI; | |
* - BMR; | |
* - Optimal weight; | |
* - Daily water norms; | |
* | |
* \author tkaczenko (Andrii Tkachenko) | |
*/ | |
class Profile | |
{ | |
Q_DECLARE_TR_FUNCTIONS(Profile) | |
public: | |
/// Create an empty Profile | |
Profile(); | |
/*! | |
* \brief Create profile with basic information | |
* \param[in] w User's weight | |
* \param[in] h User's height | |
* \param[in] age User's age | |
* \param[in] g Gender type | |
* \param[in] a User's activity coefficient | |
* \param[in] calorie Targeted number of daily calories | |
*/ | |
Profile(float w, short h, short age, QString g, float a, int calorie); | |
~Profile(); | |
// ostream, << overloading | |
friend QDataStream &operator<<(QDataStream &out, const Profile &p) | |
{ | |
out << p.getWeigth() << p.getHeigth() << p.getAge() << p.getGender() | |
<< p.getActivity() << p.getCalorie() << p.getBMI() | |
<< p.getConclusion() << p.getOptimalWeightMin() << p.getOptimalWeightMax() | |
<< p.getBMR() << p.getWater(); | |
return out; | |
} | |
// istream, >> overloading | |
friend QDataStream &operator>>(QDataStream &in, Profile &p) | |
{ | |
float weight; | |
short height; | |
short age; | |
QString gender; | |
float activity; | |
int calorie; | |
double bmi; | |
QString conclusion; | |
double minWeight; | |
double maxWeight; | |
double bmr; | |
double water; | |
in >> weight >> height >> age >> gender >> activity >> calorie >> bmi | |
>> conclusion >> minWeight >> maxWeight >> bmr >> water; | |
p = Profile(weight,height,age,gender,activity, calorie); | |
p.setBMI(bmi); | |
p.setConclusion(conclusion); | |
p.setOptimalWeightMin(minWeight); | |
p.setOptimalWeightMax(maxWeight); | |
p.setBMR(bmr); | |
p.setWater(water); | |
return in; | |
} | |
/// Calculate the all profile norms | |
void calculateNorms(); | |
void setWeigth(float w); | |
float getWeigth() const; | |
void setHeigth(short w); | |
short getHeigth() const; | |
void setAge(short age); | |
short getAge() const; | |
void setGender(QString g); | |
QString getGender() const; | |
void setActivity(float a); | |
float getActivity() const; | |
void setCalorie(int calorie); | |
int getCalorie() const; | |
void setBMI(double temp); | |
double getBMI() const; | |
void setConclusion(QString temp); | |
QString getConclusion() const; | |
void setOptimalWeightMin(double min); | |
double getOptimalWeightMin() const; | |
void setOptimalWeightMax(double max); | |
double getOptimalWeightMax() const; | |
void setBMR(double bmr); | |
double getBMR() const; | |
void setWater(double water); | |
double getWater() const; | |
private: | |
/*! | |
* \brief Calculate BMI | |
* \see https://en.wikipedia.org/wiki/Body_mass_index | |
* \return BMI number | |
*/ | |
double calculateBMI(); | |
/*! | |
* \brief Get conclusion by BMI number | |
* \param[in] res is the BMI number | |
* \return Conlcusion | |
*/ | |
QString conclusionBMI(double res); | |
/*! | |
* \brief Calculate optimal weight by BMI | |
* \param[in,out] min killograms of minimum optimal weight | |
* \param[in,out] max killograms of maximum optimal weight | |
*/ | |
void calculateOptimalWeight(double &min, double &max); | |
/*! | |
* \brief Calculate BMR | |
* \see https://en.wikipedia.org/wiki/Basal_metabolic_rate | |
* \return BMR number | |
*/ | |
double calculateBMR(); | |
/*! | |
* \brief Calculate optimal daily water norm. | |
* \return Liters of optimal daily number of water | |
*/ | |
double calculateWater(); | |
private: | |
float weight; /*!< User's weight(kg) */ | |
short height; /*!< User's height(cm) */ | |
short age; /*!< User's age */ | |
QString gender; /*!< User's gender */ | |
float activity; /*!< Coefficient of user's activity */ | |
int calorie; /*!< Targeted number of daily calories */ | |
double bmi; | |
QString conclusion; /*!< Conclusion by BMI */ | |
double minWeight; /*!< Minimum of optimal weight */ | |
double maxWeight; /*!< Maximum of optimal weigth */ | |
double bmr; /*!< BMR number */ | |
double water; /*!< Liters of optimal daily number of water */ | |
}; | |
#endif // PROFILE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment