G2A Many GEOs
SHARE
TWEET

Untitled

ericcheah575 Apr 6th, 2020 205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6. #include <windows.h>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. void validc(string &, int);
  12. void find_bmr(int , float &);
  13. void valid(int, double &, int, int);
  14. void rangeValid(int &, int &, int, int);
  15. int extractInt(string);
  16. int checkID(string[], string);
  17. void set_data_search(int &, int &, int);
  18. void set_data_gender(string &);
  19. bool digits(string s);
  20.  
  21. class health : public personal_info {
  22.     private:
  23.         double bmi;
  24.         double bmr;
  25.         double rmr;
  26.         double age;
  27.         string weightc;
  28.         fitness_info wh;
  29.         DoB date;
  30.        
  31.     public:
  32.         void setHealth(string, string, string, double, double, double, double, double);
  33.         void setPersonal(string, string, string);
  34.         void setFitness(string, double, double, double, double, double);
  35.         void setAge(double a) {
  36.             age = a;
  37.         }
  38.         double getAge() const {
  39.             return age;
  40.         }
  41.         void calc_data();
  42.         double getBMI() {
  43.             return bmi;
  44.         }
  45.         double getWeight2() {
  46.             return wh.getWeight();
  47.         }
  48.         double getHeight2() {
  49.             return wh.getHeight();
  50.         }
  51.         double getBMR() {
  52.             return bmr;
  53.         }
  54.         double getRMR() {
  55.             return rmr;
  56.         }
  57.         string getWeightC() {
  58.             return weightc;
  59.         }
  60.         double getDay2() {
  61.             return date.getDay();
  62.         }
  63.         double getMonth2() {
  64.             return date.getMonth();
  65.         }
  66.         double getYear2() {
  67.             return date.getYear();
  68.         }
  69.         void display();
  70.         health();
  71.         ~health();
  72.         friend void calc_calorie(health &);
  73.         friend void set_new_data(health &, string []);
  74.         friend void mod_data(health &, int, int);
  75.         friend void read_data(health [], int &, int &);
  76.         friend void display_stats(health [], int);
  77.         friend void del_data(health [], int &, int);
  78.         friend void selection(health [], int [], int);
  79.         friend void search(health[], int, int);
  80.         friend void sortData(health[], int);
  81. };
  82.  
  83. health::health() {
  84.     personal_info();
  85.     fitness_info();
  86.     DoB();
  87.     bmi = 0;
  88.     rmr = 0;
  89.     bmr = 0;
  90.     age = 0;
  91.     weightc = " ";
  92. }
  93.  
  94. health::~health() {
  95.     personal_info();
  96.     fitness_info();
  97.     DoB();
  98.     bmi = 0;
  99.     rmr = 0;
  100.     bmr = 0;
  101.     age = 0;
  102.     weightc = " ";
  103. }
  104.  
  105. void health::setHealth(string n, string s, string g, double w, double h, double d, double m, double y) {
  106.     setData(n, s, g);
  107.     wh.setFitness(s, w, h);
  108.     date.setDoB(d, m, y);
  109. }
  110.  
  111. void health::setPersonal(string n, string s, string g) {
  112.     setData(n, s, g);
  113. }
  114.  
  115. void health::setFitness(string s, double w, double h, double d, double m, double y) {
  116.     wh.setFitness(s, w, h);
  117.     date.setDoB(d, m, y);
  118. }
  119.  
  120. void health::calc_data() {
  121.     /* formula for BMI */
  122.     bmi = wh.getWeight() / (wh.getHeight()/100 * wh.getHeight()/100);
  123.    
  124.     /* formula for BMR and RMR */
  125.     /* both gender of same weight and height have diffrent BMR and RMR */
  126.     if (getGender() == "Male") {
  127.         bmr = 10 * wh.getWeight() + 6.25 * wh.getHeight() - 5 * getAge() + 5;
  128.         rmr = 88.362 + (13.397 * wh.getWeight()) + (4.799 * wh.getHeight()) - (5.677 * getAge());
  129.     }
  130.    
  131.     else if (getGender() == "Female") {
  132.         bmr = 10 * wh.getWeight() + 6.25 * wh.getHeight() - 5 * getAge() - 161;
  133.         rmr = 447.593 + (9.247 * wh.getWeight()) + (3.098 * wh.getHeight()) - (4.330 * getAge());
  134.     }
  135.    
  136.     /* determine weight category by BMI */
  137.     if (bmi >= 30)
  138.         weightc = "Obese";
  139.     else if (bmi < 30 && bmi >= 25)
  140.         weightc = "Overweight";
  141.     else if (bmi < 25 && bmi >= 20)
  142.         weightc = "Normal Weight";
  143.     else if (bmi < 20)
  144.         weightc = "Underweight";
  145. }
  146.  
  147. void set_new_data(health &ob, string data[]) {
  148.     string n1, s1, g1, line = "___________________________________________________________________________________";
  149.     double a1, w1, h1, d, m, y, temp;
  150.     int index = 0;
  151.    
  152.     /* data input by user */
  153.     cout << line << endl;
  154.     cout << "|           Name           |  ";
  155.     cin.ignore();      
  156.     getline(cin, n1);
  157.    
  158.     /*prevent name containing number */
  159.     while(digits(n1)) {
  160.         cout << "Invalid input. Name contains number."
  161.              << "\nName: ";
  162.         cin.clear();
  163.         cin.ignore();
  164.         getline(cin, n1);
  165.     }
  166.    
  167.     cout << "|         Staff ID         |  ";
  168.     cin >> s1;
  169.  
  170.     /* check for existing staff ID and input validation to ensure staff ID is of 12 digits and number only */
  171.     while(index >= 0 || s1.size() != 12 || !digits(s1)) {
  172.         index = checkID(data, s1);                     
  173.         if (index >= 0 || s1.size() != 12 || !digits(s1)) {
  174.             cout << "The Staff ID already existed or does not contain 12 digits! Please enter a new ID."
  175.                  << "\nStaff ID: ";
  176.             cin.clear();
  177.             cin.ignore();
  178.             cin >> s1;
  179.         }
  180.     }
  181.    
  182.     cout << "| Gender (Male or Female)  |  ";
  183.     cin >> g1;
  184.     /* input validation for gender to ensure it is either male or female */
  185.     validc(g1, 2);
  186.    
  187.     /* extracting first six digits from staff id and converting string to int data type */
  188.     /* DoB is calculated automatically */
  189.     y = extractInt(s1.substr(0, 2));
  190.     d = extractInt(s1.substr(4, 2));
  191.     m = extractInt(s1.substr(2, 2));
  192.    
  193.     /* age cannot be calculated automatically as the birthday of some person have not passed */
  194.     /* for example, those who are born in 1999 may be 20 or 21 years old */
  195.     cout << "|            Age           |  ";
  196.     cin >> a1;
  197.  
  198.     /* people born before and after or on 2000 have different age calculations */
  199.     if (y > 20) {
  200.         temp = 120 - y;
  201.         y += 1900;
  202.     }
  203.    
  204.     else {
  205.         temp = 20 - y;
  206.         y += 1900;
  207.     }
  208.    
  209.     /* input validation for age that age is a double variable within the range of staff ID */
  210.     valid(3, a1, (temp - 1), temp);
  211.     ob.setAge(a1);
  212.    
  213.     cout << "|         Weight (kg)      |  ";
  214.     cin >> w1;
  215.     /* input validation for weight that weight is a double variable within 0 to 500 kg */
  216.     valid(1, w1, 0, 500);
  217.  
  218.     cout << "|         Height (cm)      |  ";
  219.     cin >> h1;
  220.     /* input validation for height that height is a double variable within 0 to 250 cm */
  221.     valid(2, h1, 0, 250);
  222.    
  223.     ob.setHealth(n1, s1, g1, w1, h1, d, m, y); 
  224.     cout << line << endl << endl;  
  225. }
  226.  
  227. void health::display() {
  228.     cout << endl << endl;
  229.     int a = age;
  230.     personal_info::display_p();
  231.     date.display_DoB();
  232.     cout << "|             Age              |  " << a       << " years old" << endl;
  233.     wh.display_fi();
  234.     cout << "|       Body Mass Index        |  " << bmi     << endl
  235.          << "|        Weight Category       |  " << weightc << endl
  236.          << "|     Basal Metabolic Rate     |  " << bmr     << " kJ" << endl
  237.          << "|    Resting Metabolic Rate    |  " << rmr     << " kJ" << endl
  238.          << "..........................................................." << endl << endl;
  239. }
  240.  
  241. void calc_calorie(health &h) {
  242.     /* determining the calorie needed by the staff */
  243.     cout << "\n\n\t\t\t Your weight category is " << h.weightc << "!" << endl;
  244.     int freq = 0;
  245.     float temp = 0;
  246.    
  247.     /*calculate calorie consume per day to gain weight */
  248.     if (h.weightc == "Underweight")
  249.         cout << "\n\t\t\t Please eat more to replenish your health."
  250.              << "\n\t\t\t You need to eat more than " << (h.bmr + h.rmr)/2 << " kJ." << endl << endl;
  251.    
  252.     /*calculate calorie consume per day to maintain weight */
  253.     else if (h.weightc == "Normal Weight"){
  254.         cout << "\n\t\t\t You are healthy."
  255.              << "\n\n\t\t\t How often do you exercise per week?"
  256.              << "\n\t\t\t [1 - Sedentary, 2 - Slightly, 3 - Moderately, 4 - Actively]"
  257.              << "\n\n\n\t\t\t Choice: ";
  258.         cin >> freq;
  259.         temp = h.bmr;
  260.         find_bmr(freq, temp);
  261.         h.bmr = temp;
  262.         cout << "\n\n\t\t\t To maintain the weight, you must consume " << (h.bmr + h.rmr)/2 << " kJ per day.\n\n" ;
  263.     }
  264.    
  265.     /*calculate calorie consume per day to lose weight */
  266.     else if (h.weightc == "OverWeight" || h.weightc == "Obese" ){
  267.         float new_weight = 25 * (h.wh.getHeight()/100 * h.wh.getHeight()/100);
  268.         cout << "\n\t\t\t You are unhealthy!"
  269.              << "\n\t\t\t You need to be at least " << new_weight << " kg!"
  270.              << "\n\n\t\t\t How often do you exercise per week?"
  271.              << "\n\t\t\t [1 - Sedentary, 2 - Slightly, 3 - Moderately, 4 - Actively]"
  272.              << "\n\n\n\t\t\t Choice: ";
  273.         cin >> freq;
  274.         temp = h.bmr;
  275.         find_bmr(freq, temp);
  276.         h.bmr = temp;
  277.         cout << "\n\n\t\t\t To maintain the weight, you must consume " << (h.bmr + h.rmr)/2 << " kJ per day.\n\n"  //calorie to consume to maintain weight
  278.              << "\n\t\t\t However, you must lose 2 kg per month!"
  279.              << "\n\t\t\t So, You must consume " << (h.bmr + h.rmr)/2 - 500 << " kJ per day.\n\n"; //calorie consume to lose weight
  280.     }
  281. }
  282.  
  283. void mod_data(health &h, int choice2, int index) {
  284.     string temp_string;
  285.     double temp_double;
  286.     int x;
  287.     stringstream oss;
  288.    
  289.     switch (choice2) {
  290.         case 1: {
  291.             cout << "\t\t\t Please enter the new name."
  292.                  << "\n\t\t\t Name: ";
  293.             cin.ignore();
  294.             getline(cin, temp_string);
  295.            
  296.             /*prevent name containing number */
  297.             while(digits(temp_string)) {
  298.                 cout << "Invalid input! Name contains number."
  299.                      << "\nName: ";
  300.                 cin.clear();
  301.                 cin.ignore();
  302.                 getline(cin, temp_string);
  303.             }
  304.             h.setName(temp_string);
  305.             break;
  306.         }
  307.                            
  308.         case 2: {
  309.             cout << "\t\t\t Please enter the new age."
  310.                  << "\n\t\t\t Note: If you modify the age, the staff ID and date of birth will be automatically modified."
  311.                  << "\n\t\t\t Age: ";
  312.             cin >> temp_double;
  313.             valid(3, temp_double, 20, 65);
  314.             h.setAge(temp_double);
  315.             /* to save the remaining part of staff ID apart from the year, for example, 99xxxx-xx-xxxx */
  316.             /* as the age is modified, the year part should be modified as well */ 
  317.             temp_string = h.getStaff().substr(2, 10);
  318.            
  319.             /* getting the year of birth from age */
  320.             if(h.getAge() > 20)
  321.                 x = 120 - h.getAge();
  322.             else
  323.                 x = 20 - h.getAge();
  324.            
  325.             /* ensuring the staff ID always contains 12 digits */
  326.             if(x < 10)
  327.                 oss << "0" << x << temp_string;
  328.             else
  329.                 oss << x << temp_string;
  330.             h.setStaff(oss.str());
  331.            
  332.             h.date.setYear(x);
  333.             break;
  334.         }
  335.                            
  336.         case 3: {
  337.             cout << "\t\t\t Please enter the new gender."
  338.                  << "\n\t\t\t Gender: ";
  339.             cin >> temp_string;
  340.             validc(temp_string, 2);
  341.             h.setGender(temp_string);
  342.             break;
  343.         }
  344.                            
  345.         case 4: {
  346.             cout << "\t\t\t Please enter the new weight."
  347.                  << "\n\t\t\t [Weight must be within 0 to 500 kg]"
  348.                  << "\n\t\t\t Weight: ";
  349.             cin >> temp_double;
  350.             valid(1, temp_double, 0, 500);
  351.             h.wh.setWeight(temp_double);
  352.             break;
  353.         }
  354.        
  355.         case 5: {
  356.             cout << "\t\t\t Please enter the new height."
  357.                  << "\n\t\t\t [Height must be within 0 to 250 cm]"
  358.                  << "\n\t\t\t Height: ";
  359.             cin >> temp_double;
  360.             valid(2, temp_double, 0, 250);
  361.             h.wh.setHeight(temp_double);
  362.             break;
  363.         }
  364.        
  365.         default: {
  366.             cout << "Invalid Input!";
  367.             break;
  368.         }
  369.     }
  370. }
  371.  
  372. void read_data(health data_obj[], int &count, int &rub_data) {
  373.     string temp_staff2[200], temp_txt1, temp_txt2, temp_txt3, temp, corr_data[200], corr_data2[200];
  374.     double age = 0, day = 0, month = 0, year = 0, temp_weight[200], temp_height[200];
  375.     int temp_y = 0, index = 0, num_CD = 0, count2 = 0, num_CD2 = 0;
  376.     bool contain_number, found = false, name_no_ID = false, ID_no_name = false;
  377.     ifstream inData;
  378.    
  379.     rub_data = 0;
  380.     cout << "\n\n\n\t\tWelcome to Let's Get Fit!"
  381.          << "\n\t\tData is now being read from both 'PersonalInfo.txt' and 'FitnessInfo.txt'.\n\n\n";
  382.    
  383.     inData.open("PersonalInfo.txt", ios::app);
  384.     for (int a = 0; !inData.eof(); a++) {
  385.         /* check whether if temp is empty */
  386.         if (temp != "")
  387.             temp_txt1 = temp;
  388.         else if (temp == "")
  389.             inData >> temp_txt1;
  390.        
  391.         /* reset temp */
  392.         temp = ""; 
  393.        
  394.         /* validate first data read is staff ID */
  395.         contain_number = digits(temp_txt1);
  396.         /* test for valid staff ID */
  397.         if (!contain_number || temp_txt1.length() != 12) {
  398.             /* first part of data not staff ID (may be name or gender) */
  399.             if (temp_txt1 == "Male" || temp_txt1 == "Female") {
  400.                 /* first part of data is gender indicates staff ID and name are missing from txt file */
  401.                 rub_data++;
  402.                 found = true;
  403.                 continue;
  404.             }
  405.            
  406.             else if (!contain_number && (temp_txt1 != "Male" || temp_txt1 != "Female"))
  407.                 /* first part of data is name indicates staff ID is missing from txt file */
  408.                 temp = temp_txt1;
  409.                
  410.             temp_txt1 = "TBD"; 
  411.             inData.ignore();
  412.            
  413.         /* to get identification of the data that is missing staff ID */
  414.             /* getting data from the whole line using getline with delimiter
  415.             of '\n' because unsure of how many data are left in txt file */
  416.             getline(inData, temp_txt2, '\n');
  417.             /* extract gender within temp_txt2, '\t' separates name with gender in txt file */
  418.             temp_txt3 = temp_txt2.substr(temp_txt2.find_last_of("\t ", temp_txt2.length()));
  419.             /* remove other data from temp_txt3 (some data may be mixed inside temp_txt3) */
  420.             temp_txt3 = temp_txt3.erase(0, temp_txt3.find_first_not_of("\t "));
  421.             /* exrtact remaining name data within temp_txt2 */
  422.             temp_txt2 = temp_txt2.substr(0, temp_txt2.find_last_of("\t "));
  423.             /* erase data */
  424.             temp_txt2 = temp_txt2.erase(temp_txt2.find_last_not_of("\t ") +1, temp_txt2.length());
  425.            
  426.             if (temp != "" && !digits(temp))
  427.                 /* name data may be read into temp (from the earlier part when staff ID is missing in txt file) */
  428.                 temp_txt2 = temp + " " + temp_txt2;
  429.                
  430.             if (temp_txt3 != "Male" && temp_txt3 !="Female") {
  431.                 /* third part of the data is not of gender type */
  432.                 temp_txt2 += " " + temp_txt3;
  433.                 temp_txt3 = "TBD";
  434.             }
  435.            
  436.             /* record data with name */
  437.             corr_data2[num_CD2] = temp_txt2;
  438.             num_CD2++;
  439.             name_no_ID = true;
  440.             found = true;
  441.            
  442.             data_obj[count].setPersonal(temp_txt2, temp_txt1, temp_txt3);
  443.             count++;
  444.             temp = "";
  445.         }
  446.        
  447.         else if(contain_number && temp_txt1.length() == 12) {
  448.             /* staff ID (maybe) not missing from txt file */
  449.             inData.ignore();
  450.             inData >> temp_txt2;
  451.            
  452.             /* getting second part of data using temp_txt2 to test it */
  453.             if(temp_txt2 == "Male" || temp_txt2 == "Female") {
  454.                 /* second part of data is gender indicates name is missing */
  455.                 temp_txt2 = "TBD";
  456.                 temp = "";
  457.                 /* record corrupted data */
  458.                 rub_data++;
  459.                 found = true;
  460.             }
  461.            
  462.             else if(digits(temp_txt2)) {
  463.                 /* second part of data contains digits indicates name and gender is missing from text file */
  464.                 temp_txt2 = "TBD";
  465.                 temp_txt3 = "TBD";
  466.                 /* record corrupted data */
  467.                 rub_data++;
  468.                 found = true;
  469.             }
  470.            
  471.             else {     
  472.                 /* getting data from the whole line using getline with delimiter
  473.                 of '\n' because unsure of how many data are left in txt file */
  474.                 getline(inData, temp, '\n');
  475.                
  476.                 /* extract gender within temp_txt2, '\t' separates name with gender in txt file */
  477.                 temp_txt3 = temp.substr(temp.find_last_of("\t ",temp.length()));
  478.                 /* remove other data from temp_txt3 (some data may be mixed inside temp_txt3) */
  479.                 temp_txt3 = temp_txt3.erase(0, temp_txt3.find_first_not_of("\t "));
  480.                 /* exrtact remaining name data within temp_txt2 */
  481.                 temp = temp.substr(0, temp.find_last_of("\t "));
  482.                 /* construct the name data fully */
  483.                 temp_txt2 = temp_txt2 + temp;
  484.                 /* erase data */
  485.                 temp_txt2 = temp_txt2.erase(temp_txt2.find_last_not_of("\t ") +1, temp_txt2.length());
  486.                
  487.                 if(temp_txt3 != "Male" && temp_txt3 !="Female") {
  488.                     /* third part of data still contains name (gender is missing from txt file) */
  489.                     temp_txt2 += " " + temp_txt3;
  490.                     temp_txt3 = "TBD";
  491.                     corr_data[num_CD] = temp_txt1;
  492.                     num_CD++;
  493.                     found = true;
  494.                 }
  495.                 temp = "";
  496.             }
  497.            
  498.             data_obj[count].setPersonal(temp_txt2, temp_txt1, temp_txt3);
  499.             count++;
  500.         }
  501.     }
  502.    
  503.     if(found) {
  504.         /* prompt user if txt file contain corrupted data */
  505.         cout << "\n\t\tThese is/are the data that has corrupted data from 'PersonalInfo.txt' that are detected: " << endl
  506.              << "\t\tNote: Data that can be identified are missing Gender information." << endl;
  507.            
  508.         for(int i = 0; i < count; i++) {
  509.             for(int a = 0; a < num_CD; a++) {
  510.                 if(data_obj[i].getStaff() == corr_data[a])
  511.                     /* indentification of corrupted data with staff ID and name */
  512.                     cout << "\n\t\t\tName: " << data_obj[i].getName()
  513.                          << "\n\t\t\tStaff ID: " << corr_data[a];
  514.             }
  515.         }
  516.        
  517.         if(name_no_ID) {
  518.             for(int i = 0; i < num_CD2; i++)
  519.                 cout << "\n\t\t\tName: " << corr_data2[i]
  520.                      << "\n\t\t\tStaff ID: TBD";
  521.         }
  522.        
  523.         cout << endl << endl << "\t\t\tThere is/are " << rub_data << " corrupted in 'PersonalInfo.txt' without any identification." << endl << endl;
  524.     }
  525.    
  526.     else if(!found)
  527.         /* prompt user if txt file does not contain corrupted data */
  528.         cout << "\n\t\tThe data read from 'PersonalInfo.txt' are all valid." << endl << endl;
  529.    
  530.     inData.close();
  531.        
  532.     inData.open("FitnessInfo.txt", ios::app);
  533.    
  534.     /* reset counter and flag */
  535.     num_CD = 0;
  536.     rub_data = 0;
  537.     found = false; 
  538.    
  539.     for(int a = 0; !inData.eof(); a++) {
  540.        
  541.         /* using string as temporary text so that program will not crash after reading data
  542.         and store into respective data type after done */
  543.         if(temp != "")
  544.             temp_txt1 = temp;
  545.         else if(temp == "")
  546.             inData >> temp_txt1;
  547.            
  548.         /* reset temp */
  549.         temp = "";
  550.        
  551.         /* validate first data read is staff ID */
  552.         contain_number = digits(temp_txt1);
  553.         /* test for valid staff ID */
  554.         if(!contain_number && temp_txt1.length() != 12) {
  555.             /* first part of data is not staff ID (also not weight or height)
  556.             also indicates that staff ID may be missing in txt file */
  557.             inData >> temp_txt2;
  558.             contain_number = digits(temp_txt2);
  559.            
  560.             /* test for valid weight and second part of data is not staff ID */
  561.             if(contain_number && temp_txt2.length() != 12) {
  562.                 inData >> temp_txt3;
  563.                 contain_number = digits(temp_txt3);
  564.                    
  565.                 /* test for valid height */
  566.                 if(contain_number && temp_txt3.length() == 12)
  567.                     /* third part of data is staff ID */
  568.                     /* record which data is corrupted */
  569.                     corr_data[num_CD] = temp_txt3;
  570.                
  571.                 else
  572.                     /* cannot identify data as staff ID is missing */
  573.                     rub_data++;
  574.             }
  575.            
  576.             else if(contain_number && temp_txt2.length() == 12) {
  577.                 /* second part of the data is staff ID (indicates weight is missing in txt file) */
  578.                 /* record which data is corrupted */
  579.                 corr_data[num_CD] = temp_txt2;
  580.                 num_CD++;
  581.             }
  582.            
  583.             else if(contain_number && extractInt(temp_txt2) < 0)
  584.                 /* if second part of the data is invalid (out of range) */
  585.                 rub_data++;
  586.                
  587.             else {
  588.                 inData >> temp_txt3;
  589.                 if(contain_number && temp_txt3.length() == 12) {
  590.                     /* third part of data is staff ID (indicates height is missing in txt file) */
  591.                     /* record which data is corrupted */
  592.                     corr_data[num_CD] = temp_txt3;
  593.                     num_CD++;
  594.                 }
  595.             }
  596.            
  597.             found = true;
  598.             /* statement will be skipped regardlessly */
  599.             continue;
  600.         }
  601.        
  602.         else if(contain_number && temp_txt1.length() != 12) {
  603.             /* if first part of data is not staff ID (indicates staff ID is missing from txt file) */
  604.             /* it may be weight or corrupted data */
  605.             inData >> temp_txt3;
  606.             contain_number = digits(temp_txt3);
  607.            
  608.             if(contain_number && temp_txt3.length() == 12)
  609.                 /* third part of data is staff ID */
  610.                 temp = temp_txt3;
  611.            
  612.             rub_data++;
  613.             found = true;
  614.             continue;
  615.         }
  616.        
  617.         else {
  618.             /* the first part of data is staff ID */
  619.             inData >> temp_txt2;
  620.             contain_number = digits(temp_txt2);
  621.            
  622.             if(contain_number && extractInt(temp_txt2) > 0 && extractInt(temp_txt2) <= 150 && temp_txt2.length() != 12) {
  623.                 /* when second part of the data is weight */
  624.                 inData >> temp_txt3;
  625.                 contain_number = digits(temp_txt3);
  626.                
  627.                 if(contain_number && extractInt(temp_txt3) > 140 && temp_txt3.length() != 12)
  628.                     /* when third part of data is height */
  629.                     temp = "";
  630.                    
  631.                 else if((contain_number && temp_txt3.length() == 12) || (contain_number && extractInt(temp_txt3) < 140)) {
  632.                     /* third part of data is staff ID */
  633.                     temp = temp_txt3;
  634.                     temp_txt3 = "0";
  635.                     /* record which data is corrupted */
  636.                     corr_data[num_CD] = temp_txt1;
  637.                     num_CD++;
  638.                     found = true;
  639.                 }  
  640.             }
  641.            
  642.             else if(contain_number && temp_txt2.length() == 12) {
  643.                 /* second part of data is staff ID */
  644.                 /* record which data is corrupted */
  645.                 corr_data[num_CD] = temp_txt2;
  646.                 num_CD++;
  647.                 found = true;
  648.             }
  649.            
  650.             else if(extractInt(temp_txt2) > 140) {
  651.                 /* second part of data is height */
  652.                 temp = "";
  653.                 temp_txt3 = temp_txt2;
  654.                 temp_txt2 = "0";
  655.                 /* record which data is corrupted */
  656.                 corr_data[num_CD] = temp_txt1;
  657.                 num_CD++;
  658.                 found = true;
  659.             }
  660.            
  661.             else {
  662.                 /* weight is not valid */
  663.                 temp_txt2 = "0";
  664.                 inData >> temp_txt3;
  665.                 contain_number = digits(temp_txt3);
  666.                    
  667.                 if(contain_number && extractInt(temp_txt3) > 140)
  668.                     temp = "";
  669.                
  670.                 else {
  671.                     /* variable of different data type */
  672.                     temp_txt3 = "0";
  673.                     temp = "";
  674.                 }
  675.                
  676.                 /* record which data is corrupted */
  677.                 corr_data[num_CD] = temp_txt1;
  678.                 num_CD++;
  679.                 found = true;
  680.             }
  681.         }
  682.         count2++;
  683.         temp_staff2[a] = temp_txt1;
  684.         temp_weight[a] = extractInt(temp_txt2);
  685.         temp_height[a] = extractInt(temp_txt3);
  686.     }
  687.    
  688.     if(found) {
  689.         /* prompt user if file contains corrupted data */
  690.         cout << "\n\t\tThese are the data that has corrupted data from 'FitnessInfo.txt' that are detected: " << endl
  691.              << "\t\tNote: Data that can be identified is/are missing Weight or Height information." << endl;
  692.            
  693.         for(int i = 0; i < count; i++) {
  694.             for(int a = 0; a < num_CD; a++) {
  695.                 if(data_obj[i].getStaff() == corr_data[a])
  696.                     cout << "\n\t\t\tName: " << data_obj[i].getName()
  697.                          << "\n\t\t\tStaff ID: " << corr_data[a];
  698.             }
  699.         }
  700.         cout << endl << endl << "\t\t\tThere is/are " << rub_data << " corrupted in 'FitnessInfo.txt' without any identification." << endl << endl;
  701.         cout << endl << endl;
  702.     }
  703.    
  704.     else if(!found)
  705.         /* prompt user if file does not contain corrupted data */
  706.         cout << "\n\t\tThe data read from 'FitnessInfo.txt' are all valid." << endl << endl;
  707.    
  708.     inData.close();
  709.    
  710.     system("pause");
  711.     system("cls");
  712.    
  713.     /* rearrange number of data */
  714.     if(count > count2)
  715.         count = count2;
  716.        
  717.     for (int a = 0; a < count; a++) {
  718.    
  719.         temp_txt1 = data_obj[a].getStaff();
  720.            
  721.         /* prevent duplication of data of first ID */
  722.         for (int c = a + 1; c <= count; c++) {
  723.             /* + 1 to prevent reading the same data */
  724.             if(data_obj[a].getStaff() == data_obj[c].getStaff())
  725.                 data_obj[c] = data_obj[c + 1];
  726.             continue;
  727.         }
  728.        
  729.         /* prevent duplication of data of second ID */
  730.         for (int c = a + 1; c <= count; c++) {
  731.             /* + 1 to prevent reading the same data */
  732.             if(temp_staff2[a] == temp_staff2[c]) {
  733.                 temp_staff2[c] = temp_staff2[c + 1];
  734.                 temp_weight[c] = temp_weight[c + 1];
  735.                 temp_height[c] = temp_height[c + 1];
  736.             }
  737.             continue;
  738.         }
  739.            
  740.         /* matching staff ID */
  741.         index = checkID(temp_staff2, temp_txt1);
  742.            
  743.         /* removing data with no matches or rubbish data */
  744.         if(index < 0 || data_obj[a].getStaff() == "TBD" || data_obj[a].getName() == "TBD" || data_obj[a].getGender() == "TBD" ) {
  745.             data_obj[a] = data_obj[a + 1];
  746.             count--;
  747.             continue;
  748.         }
  749.            
  750.         /* extracting first six digits from staff id and converting string to int data type */
  751.         temp_y = extractInt(data_obj[a].getStaff().substr(0, 2));
  752.  
  753.         /* showing year in the form of yyyy */
  754.         if (temp_y > 20) {
  755.             age = 120 - temp_y;
  756.             temp_y += 1900;
  757.         }
  758.         else {
  759.             age = 20 - temp_y;
  760.             temp_y += 2000;
  761.         }
  762.        
  763.         /* extract data part from staff ID */
  764.         day = extractInt(data_obj[a].getStaff().substr(4, 2));
  765.         month = extractInt(data_obj[a].getStaff().substr(2, 2));
  766.         year = temp_y;
  767.        
  768.         data_obj[a].setFitness(data_obj[a].getStaff(), temp_weight[index], temp_height[index], day, month, year);
  769.         data_obj[a].setAge(age);
  770.        
  771.         if(data_obj[a].getWeight2() == 0 || data_obj[a].getHeight2() == 0)
  772.             /* remove corrupted data */
  773.             data_obj[a] = data_obj[a + 1];
  774.     }
  775. }
  776.  
  777. void display_stats(health h[], int size) {
  778.     string line = "+-----------------+-------------+";
  779.     string mainline = "+-------------------------------+";
  780.     double countM = 0, countF = 0, count20 = 0, count30 = 0, count40 = 0, count50 = 0;
  781.     double count_under = 0, count_norm = 0, count_over = 0, count_obese = 0;
  782.     double perc_genderM, perc_genderF, perc_age20, perc_age30, perc_age40, perc_age50, perc_under, perc_norm, perc_over, perc_obese;
  783.    
  784.     for (int k = 0; k < size; k++) {
  785.         if (h[k].getGender() == "Male")
  786.             countM++;
  787.         else if (h[k].getGender() == "Female")
  788.             countF++;
  789.         else
  790.             size--;
  791.     }
  792.    
  793.     /* calculate percentage of data */
  794.     perc_genderM = (countM * 100) / size;
  795.     perc_genderF = (countF  * 100) / size;
  796.    
  797.     for (int k = 0; k < size; k++) {
  798.         if (h[k].getAge() >= 50)
  799.             count50++;
  800.         else if (h[k].getAge() >= 40 && h[k].getAge() < 50)
  801.             count40++;
  802.         else if (h[k].getAge() >= 30 && h[k].getAge() < 40)
  803.             count30++;
  804.         else if (h[k].getAge() >= 20 && h[k].getAge() < 30)
  805.             count20++;
  806.         else
  807.             continue;
  808.     }
  809.    
  810.     /* calculate percentage of data */
  811.     perc_age20 = (count20 * 100) / size;
  812.     perc_age30 = (count30 * 100) / size;
  813.     perc_age40 = (count40 * 100) / size;
  814.     perc_age50 = (count50 * 100) / size;
  815.    
  816.     for (int k = 0; k <= size; k++) {
  817.         if (h[k].getWeightC() == "Underweight")
  818.             count_under++;
  819.         else if (h[k].getWeightC() == "Normal Weight")
  820.             count_norm++;
  821.         else if (h[k].getWeightC() == "Overweight")
  822.             count_over++;
  823.         else if (h[k].getWeightC() == "Obese")
  824.             count_obese++;
  825.         else
  826.             continue;
  827.     }
  828.    
  829.     /* calculate percentage of data */
  830.     perc_under = (count_under * 100) / size;
  831.     perc_norm = (count_norm * 100) / size;
  832.     perc_over = (count_over * 100) / size;
  833.     perc_obese = (count_obese * 100) / size;
  834.    
  835.     cout << "\n\t\t      Statistics of Staffs" << endl << endl;
  836.     cout << endl << "\t\t" << mainline << endl
  837.     /* displaying statistics of gender */
  838.          << "\t\t" << "|\t     Gender     \t|\n" << "\t\t" << line << endl
  839.          << "\t\t" << "|       Male      |" << "    " << perc_genderM << "%\t|" << endl << "\t\t" << line << endl
  840.          << "\t\t" << "|      Female     |" << "    " << perc_genderF << "%\t|" << endl << "\t\t" << line << endl << endl;
  841.          
  842.     cout << "\t\t" << mainline << endl
  843.     /* displaying statistics of age */
  844.          << "\t\t" << "|\t          Age       \t|\n" << "\t\t" << line << endl
  845.          << "\t\t" << "|     20 - 30     |" << "    " << perc_age20 << "%\t|" << endl << "\t\t" << line << endl
  846.          << "\t\t" << "|     30 - 40     |" << "    " << perc_age30 << "%\t|" << endl << "\t\t" << line << endl
  847.          << "\t\t" << "|     40 - 50     |" << "    " << perc_age40 << "%\t|" << endl << "\t\t" << line << endl
  848.          << "\t\t" << "|    Above  50    |" << "    " << perc_age50 << "%\t|" << endl << "\t\t" << line << endl << endl;
  849.          
  850.     cout << "\t\t" << mainline << endl
  851.     /* displaying statistics of weight category */
  852.          << "\t\t" << "|\t Weight  Category \t|\n" << "\t\t" << line << endl
  853.          << "\t\t" << "|   Underweight   |" << "    " << perc_under << "%\t|" << endl << "\t\t" << line << endl
  854.          << "\t\t" << "|  Normal Weight  |" << "    " << perc_norm << "%\t|" << endl << "\t\t" << line << endl
  855.          << "\t\t" << "|   Overweight    |" << "    " << perc_over << "%\t|" << endl << "\t\t" << line << endl
  856.          << "\t\t" << "|      Obese      |" << "    " << perc_obese << "%\t|" << endl << "\t\t" << line << endl << endl;
  857. }
  858.  
  859. void del_data(health data_obj[], int &count, int index) {
  860.     for (int j = index; j < count; j++)
  861.         /* delete by shifting every data forward */
  862.         data_obj[j] = data_obj[j + 1];
  863. }
  864.  
  865. void selection(health h[], int choice[], int count) {
  866.     /* multiple selection function */
  867.     string gender;
  868.     int min = 0, max = 0, min1 = 0, max1 = 0;
  869.     if ((choice[0] == 1 && choice[1] == 2) ||(choice[0] == 2 && choice[1] == 1)) {
  870.         set_data_gender(gender);
  871.         set_data_search(min, max, 1);
  872.         for (int k = 0; k < count; k++) {
  873.             if (gender == "Male" && h[k].getGender() == "Male" && h[k].getAge() >= min && h[k].getAge() <= max)
  874.                 h[k].display();
  875.             else if (gender == "Female" && h[k].getGender() == "Female" && h[k].getAge() >= min && h[k].getAge() <= max)
  876.                 h[k].display();
  877.         }
  878.     }
  879.                    
  880.     else if ((choice[0] == 1 && choice[1] == 3) || (choice[0] == 3 && choice[1] == 1)) {
  881.         set_data_gender(gender);
  882.         set_data_search(min, max, 2);
  883.         for (int k = 0; k < count; k++) {
  884.             if (gender == "Male" && h[k].getGender() == "Male" && h[k].getWeight2() >= min && h[k].getWeight2() <= max)
  885.                 h[k].display();
  886.             else if (gender == "Female" && h[k].getGender() == "Female" && h[k].getWeight2() >= min && h[k].getWeight2() <= max)
  887.                 h[k].display();
  888.         }
  889.     }
  890.                    
  891.     else if ((choice[0] == 1 && choice[1] == 4) || (choice[0] == 4 && choice[1] == 1)) {
  892.         set_data_gender(gender);
  893.         set_data_search(min, max, 3);
  894.         for (int k = 0; k < count; k++) {
  895.             if (gender == "Male" && h[k].getGender() == "Male" && h[k].getHeight2() >= min && h[k].getHeight2() <= max)
  896.                 h[k].display();
  897.             else if (gender == "Female" && h[k].getGender() == "Female" && h[k].getHeight2() >= min && h[k].getHeight2() <= max)
  898.                 h[k].display();
  899.         }
  900.     }
  901.                    
  902.     else if ((choice[0] == 1 && choice[1] == 5) || (choice[0] == 5 && choice[1] == 1)) {
  903.         set_data_gender(gender);
  904.         set_data_search(min, max, 4);
  905.         for (int k = 0; k < count; k++) {
  906.             if (gender == "Male" && h[k].getGender() == "Male" && h[k].getBMI() >= min && h[k].getBMI() <= max)
  907.                 h[k].display();
  908.             else if (gender == "Female" && h[k].getGender() == "Female" && h[k].getBMI() >= min && h[k].getBMI() <= max)
  909.                 h[k].display();
  910.         }
  911.     }
  912.                    
  913.     else if ((choice[0] == 2 && choice[1] == 3) || (choice[0] == 3 && choice[1] == 2)) {
  914.         set_data_search(min, max, 1);
  915.         set_data_search(min1, max1, 2);
  916.         for (int k = 0; k < count; k++) {
  917.             if (h[k].getAge() >= min && h[k].getAge() <= max && h[k].getWeight2() >= min1 && h[k].getWeight2() <= max1)
  918.                 h[k].display();
  919.         }
  920.     }
  921.                    
  922.     else if ((choice[0] == 2 && choice[1] == 4) || (choice[0] == 4 && choice[1] == 2)) {
  923.         set_data_search(min, max, 1);
  924.         set_data_search(min1, max1, 3);
  925.         for (int k = 0; k < count; k++) {
  926.             if (h[k].getAge() >= min && h[k].getAge() <= max && h[k].getHeight2() >= min1 && h[k].getHeight2() <= max1)
  927.                 h[k].display();
  928.         }
  929.     }
  930.                    
  931.     else if ((choice[0] == 2 && choice[1] == 5) || (choice[0] == 5 && choice[1] == 2)) {
  932.         set_data_search(min, max, 1);
  933.         set_data_search(min1, max1, 4);
  934.         for (int k = 0; k < count; k++) {
  935.             if (h[k].getAge() >= min && h[k].getAge() <= max && h[k].getBMI() >= min1 && h[k].getBMI() <= max1)
  936.                 h[k].display();
  937.         }
  938.     }
  939.                    
  940.     else if ((choice[0] == 3 && choice[1] == 4) || (choice[0] == 4 && choice[1] == 3)) {
  941.         set_data_search(min, max, 2);
  942.         set_data_search(min1, max1, 3);
  943.         for (int k = 0; k < count; k++) {
  944.             if (h[k].getWeight2() >= min && h[k].getWeight2() <= max && h[k].getHeight2() >= min1 && h[k].getHeight2() <= max1)
  945.                 h[k].display();
  946.         }
  947.     }
  948.                    
  949.     else if ((choice[0] == 3 && choice[1] == 5) || (choice[0] == 5 && choice[1] == 3)) {
  950.         set_data_search(min, max, 2);
  951.         set_data_search(min1, max1, 4);
  952.         for (int k = 0; k < count; k++) {
  953.             if (h[k].getWeight2() >= min && h[k].getWeight2() <= max && h[k].getBMI() >= min1 && h[k].getBMI() <= max1)
  954.                 h[k].display();
  955.         }
  956.     }
  957.                    
  958.     else if ((choice[0] == 4 && choice[1] == 5) || (choice[0] == 5 && choice[1] == 4)) {
  959.         set_data_search(min, max, 3);
  960.         set_data_search(min1, max1, 4);
  961.         for (int k = 0; k < count; k++) {
  962.             if (h[k].getHeight2() >= min && h[k].getHeight2() <= max && h[k].getBMI() >= min1 && h[k].getBMI() <= max1)
  963.                 h[k].display();
  964.         }
  965.     }
  966.                    
  967.     else
  968.         cout << "\n\n\n\t\t\t Invalid Input!" << endl << endl;
  969. }
  970.  
  971. void search(health data[], int choice, int count) {
  972.     string temp[200], text;
  973.     int index = -1, min, max;
  974.    
  975.     if(choice == 1) {
  976.         for(int a = 0; a < count; a++)
  977.             temp[a] = data[a].personal_info::getStaff();
  978.        
  979.         cout << "\t\t\t Please enter the ID number of the staff: ";
  980.         cin >> text;
  981.         index = checkID(temp, text);
  982.        
  983.         if(index >= 0)
  984.             data[index].display();
  985.                    
  986.         /* promt user if data not found */
  987.         else
  988.             cout << "\t\t\t Staff I.D not found!";
  989.     }
  990.    
  991.     else if(choice == 2) {
  992.         for(int a = 0; a < count; a++)
  993.             temp[a] = data[a].personal_info::getName();
  994.        
  995.         cin.ignore();
  996.         cout << "\t\t\t Please enter the name of the staff: ";
  997.         getline(cin, text);
  998.         index = checkID(temp, text);
  999.        
  1000.         if(index >= 0)
  1001.             data[index].display();
  1002.                    
  1003.         /* promt user if data not found */
  1004.         else
  1005.             cout << "\t\t\t Staff name not found!";
  1006.     }
  1007.    
  1008.     else if(choice == 3) {
  1009.         set_data_search(min, max, 2);
  1010.         for(int a = 0; a < count; a++) {
  1011.             if(data[a].getWeight2() >= min && data[a].getWeight2() <= max)
  1012.                 data[a].display();
  1013.         }
  1014.     }
  1015.    
  1016.     else if(choice == 4) {
  1017.         set_data_search(min, max, 3);
  1018.         for(int a = 0; a < count; a++) {
  1019.             if(data[a].getHeight2() >= min && data[a].getHeight2() <= max)
  1020.                 data[a].display();
  1021.         }
  1022.     }
  1023.    
  1024.     else if(choice == 5) {
  1025.         set_data_search(min, max, 5);
  1026.         for(int a = 0; a < count; a++) {
  1027.             if(data[a].getBMI() >= min && data[a].getBMI() <= max)
  1028.                 data[a].display();
  1029.         }
  1030.     }
  1031.    
  1032.     else if(choice == 6) {
  1033.         set_data_search(min, max, 1);
  1034.         for(int a = 0; a < count; a++) {
  1035.             if(data[a].getAge() >= min && data[a].getAge() <= max)
  1036.                 data[a].display();
  1037.         }
  1038.     }
  1039.    
  1040.     else
  1041.         cout << "\t\t\t Invalid Input!";
  1042. }
  1043.  
  1044. void sortData(health data_obj[], int count) {
  1045.     string temp_name1, temp_name2, temp_ID, temp_gender;
  1046.     double temp_weight = 0, temp_height = 0, temp_day = 0, temp_month = 0, temp_year = 0, temp_age = 0;
  1047.    
  1048.     for(int i = 1; i < count; i++) {
  1049.         temp_name1 = data_obj[i - 1].getName();
  1050.         temp_name2 = data_obj[i].getName();
  1051.        
  1052.         /*reset value of j for every checks */
  1053.         int j = 0;
  1054.         while(temp_name1[j] == temp_name2[j])
  1055.             /* advance checking to next alphabet of the same string if first alphabet is the same */
  1056.             j++;
  1057.        
  1058.         /*compare the alphabets precendence */
  1059.         if(temp_name1[j] > temp_name2[j]) {
  1060.             /* storing data into temp data */
  1061.             temp_ID = data_obj[i - 1].getStaff();
  1062.             temp_gender = data_obj[i - 1].getGender();
  1063.             temp_weight = data_obj[i - 1].getWeight2();
  1064.             temp_height = data_obj[i - 1].getHeight2();
  1065.             temp_day = data_obj[i - 1].getDay2();
  1066.             temp_month = data_obj[i - 1].getMonth2();
  1067.             temp_year = data_obj[i - 1].getYear2();
  1068.             temp_age = data_obj[i - 1].getAge();
  1069.            
  1070.             /* swapping of data */
  1071.             /*set data of next array into previous array */
  1072.             data_obj[i - 1].setHealth(temp_name2, data_obj[i].getStaff(), data_obj[i].getGender(), data_obj[i].getWeight2(), data_obj[i].getHeight2(),
  1073.                                        data_obj[i].getDay2(), data_obj[i].getMonth2(), data_obj[i].getYear2());
  1074.             data_obj[i - 1].setAge(data_obj[i].getAge());
  1075.             data_obj[i - 1].calc_data();
  1076.            
  1077.             /*set data of previous array into next array */
  1078.             data_obj[i].setHealth(temp_name1, temp_ID, temp_gender, temp_weight, temp_height, temp_day, temp_month, temp_year);
  1079.             data_obj[i].setAge(temp_age);
  1080.             data_obj[i].calc_data();
  1081.            
  1082.             /* reset count to check again */
  1083.             i = 0;
  1084.         }
  1085.     }
  1086. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top