tiffprag

Functions Class

Jan 21st, 2020
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.57 KB | None | 0 0
  1. #ifndef FUNC_H
  2. #define FUNC_H
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. //libraries for importing text files
  8. #include <fstream>    //allow read from text file
  9.  
  10. #include <sstream>    //for string stream class
  11.  
  12. #include <string>     //for npos, find_first_not_of, find_last_not_of, substr
  13.  
  14. #include "ReservationClass.h"   //inherit Reservation class
  15.  
  16. //Array Size/number of reservations allowed
  17. #define MAX 5
  18. Reservation records[MAX];
  19.  
  20. using namespace std;
  21.  
  22. //GLOBAL VARIABLES
  23. int foundTextFile = 0;    //check whether there is any imported text files
  24. int foundRecord = 0;
  25. string fileDirectory; //the .txt file directory
  26.  
  27. class Functions{
  28. public:
  29.   //DISPLAY TABLE COLUMN NAMES
  30.   void displayColumnNames(){
  31.     cout<<setfill('_')<<setw(87)<<"_"<<endl
  32.     <<setfill(' ')<<setw(5)<<"| No. "
  33.     <<left<<setw(30)<<"Name"
  34.     <<setw(5)<<"Phone No."
  35.     <<right<<setw(16)<<"Pax No."
  36.     <<setw(10)<<"Date"
  37.     <<setw(16)<<"Time |"<<endl
  38.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  39.   }
  40.  
  41.   //SET DEFAULT RECORDS
  42.   void defaultRecords(Reservation records[]){
  43.     for(int i = 0; i<MAX; i++){
  44.       records[i].setBookingNo(i+1);
  45.     }
  46.  
  47.     records[0].setName("Tan Phit Huan");
  48.     records[1].setName("Khong Ziv Hale");
  49.     records[2].setName("Rex Luah");
  50.     records[3].setName("Lee Yeu Gor");
  51.     records[4].setName("Tiffany Pragasam");
  52.  
  53.     for(int i = 0; i<MAX; i++){
  54.       records[i].setPhoneNo(123456777+i);
  55.     }
  56.  
  57.     records[0].setPaxNo(2);
  58.     records[1].setPaxNo(5);
  59.     records[2].setPaxNo(7);
  60.     records[3].setPaxNo(4);
  61.     records[4].setPaxNo(10);
  62.  
  63.     records[0].setDate(20191212);
  64.     records[1].setDate(20191111);
  65.     records[2].setDate(20191111);
  66.     records[3].setDate(20191212);
  67.     records[4].setDate(20191211);
  68.  
  69.     for(int i = 0; i<MAX; i++){
  70.       records[i].setTime(1200+(i*100));
  71.     }
  72.   }
  73.  
  74.   //TRIM FUNCTION
  75.   string trim(const string& str){
  76.       size_t first = str.find_first_not_of(' ');
  77.       if (string::npos == first)
  78.       {
  79.           return str;
  80.       }
  81.       size_t last = str.find_last_not_of(' ');
  82.       return str.substr(first, (last - first + 1));
  83.   }
  84.  
  85.   //READ TEXT FILE FUNCTION
  86.   void readTextFile(string fileDirectory, Reservation records[]){
  87.     ifstream myFile(fileDirectory);
  88.     if(!myFile.is_open()){
  89.       cout<<endl<<"ERROR! File failed to open."<<endl;
  90.       return;
  91.     }
  92.  
  93.     foundTextFile = 1;
  94.  
  95.     try{
  96.     int i=0;  //index number
  97.     int checkLinesNo = 0; //check number of lines in the text file
  98.     string tempString,line;
  99.  
  100.     //Reservation Information
  101.     int bkNo, phoneNo, paxNo, day, month, year, date, time;
  102.     string name;
  103.  
  104.     while(getline(myFile, line)){   //reads a line
  105.       checkLinesNo++;
  106.       if(checkLinesNo > MAX+2){     //if text file exceeds 7 lines (5 lines of records + 1 column names and 1 divide line)
  107.         cout<<endl<<"Maximum of "<<MAX<<" reservations can be stored."<<endl
  108.         <<"Some data will be lost."<<endl;
  109.         break;
  110.       }
  111.       stringstream ss(line);        //using stringstream to manipulate the line
  112.       getline(ss, tempString, ' ');  //store the string in
  113.       if(tempString != "|=====================================================================================|"){
  114.         getline(ss, tempString, ' ');
  115.         if(tempString != "No."){
  116.  
  117.           bkNo=stoi(tempString);    //convert tempString to integer
  118.  
  119.           getline(ss, tempString, '|');
  120.  
  121.           //gets substring from character in position 2 onwards, 30 characters(where name column occupies)
  122.           //Then trim to get rid of empty spaces
  123.           name = (trim(tempString.substr(2,30)));
  124.  
  125.           phoneNo = stoi(tempString.substr(32,10));
  126.  
  127.           //need to trim because there can be 1 or 2 digits
  128.           paxNo = stoi(trim(tempString.substr(52,2)));
  129.  
  130.           day = stoi(tempString.substr(60,2));
  131.  
  132.           month = stoi(tempString.substr(63,2));
  133.  
  134.           year = stoi(tempString.substr(66,4));
  135.  
  136.           time = stoi(tempString.substr(77,4));
  137.  
  138.           date = year*10000 + month*100 + day;
  139.  
  140.           //Overriding the default/previous records with the new records
  141.           records[i].setBookingNo(bkNo);
  142.           records[i].setName(name);
  143.           records[i].setPhoneNo(phoneNo);
  144.           records[i].setPaxNo(paxNo);
  145.           records[i].setDate(date);
  146.           records[i].setTime(time);
  147.  
  148.           i++;
  149.         }
  150.       }
  151.  
  152.     }
  153.     }catch(...){
  154.       cout<<endl<<"ERROR! Import process cancelled."<<endl
  155.       <<"Please check whether it is the correct text file or directory."<<endl;
  156.       foundTextFile = 0;
  157.     }
  158.  
  159.     if(foundTextFile == 1){
  160.       cout<<endl<<"Import Successful."<<endl;
  161.     }
  162.     myFile.close();
  163.   }
  164.  
  165.   //BUBBLE SORT FUNCTIONS
  166.   //Sort by Name
  167.   void bubbleSortName(Reservation records[]){
  168.     int i,j;
  169.     Reservation current;
  170.     for(i=0; i<MAX;i++){  
  171.       for (j=0; j<MAX-1; j++){  
  172.         if(records[j].getName()>records[j+1].getName()){              
  173.           current = records[j];              
  174.           records[j] = records[j+1];              
  175.           records[j+1] = current;          
  176.         }        
  177.       }          
  178.     }
  179.   }
  180.  
  181.   //Sort by Date
  182.   void bubbleSortDate(Reservation records[]){
  183.       int i,j;
  184.       Reservation current;
  185.       for(i=0; i<MAX;i++){  
  186.         for (j=0; j<MAX-1; j++){  
  187.           if(records[j].getDate()>records[j+1].getDate()){              
  188.             current = records[j];              
  189.             records[j] = records[j+1];              
  190.             records[j+1] = current;          
  191.           }        
  192.         }          
  193.       }
  194.   }
  195.  
  196.   //Sort by Phone Number
  197.   void bubbleSortPhoneNo(Reservation records[]){
  198.       int i,j;
  199.       Reservation current;
  200.       for(i=0; i<MAX;i++){  
  201.         for (j=0; j<MAX-1; j++){  
  202.           if(records[j].getPhoneNo()>records[j+1].getPhoneNo()){              
  203.             current = records[j];              
  204.             records[j] = records[j+1];              
  205.             records[j+1] = current;          
  206.           }        
  207.         }          
  208.       }
  209.   }
  210.  
  211.   //Sort by number of Pax
  212.   void bubbleSortPaxNo(Reservation records[]){
  213.       int i,j;
  214.       Reservation current;
  215.       for(i=0; i<MAX;i++){  
  216.         for (j=0; j<MAX-1; j++){  
  217.           if(records[j].getPaxNo()>records[j+1].getPaxNo()){              
  218.             current = records[j];              
  219.             records[j] = records[j+1];              
  220.             records[j+1] = current;          
  221.           }        
  222.         }          
  223.       }
  224.   }
  225.  
  226.   //BINARY SEARCH FUNCTIONS
  227.   //Search Phone No Record
  228.   int binarySearchPhoneNo(Reservation records[], int key, int low, int high){    
  229.     int middle; //the middle index number of the array    
  230.     bubbleSortPhoneNo(records);
  231.     while (low <= high ){      
  232.       middle = (low+high)/2;
  233.       if(key==records[middle].getPhoneNo()){
  234.         return middle;
  235.  
  236.       }else if(key<records[middle].getPhoneNo()){
  237.         high = middle-1; //reset high index to left side
  238.  
  239.       }else{
  240.         low = middle+1; //reset low index to right side
  241.  
  242.       }
  243.     }
  244.  
  245.     return -1;   //key not found
  246.   }
  247.  
  248.   //Search Name Record
  249.   int binarySearchName(Reservation records[], string key, int low, int high){    
  250.     int middle; //the middle index number of the array    
  251.     bubbleSortName(records);
  252.     while (low <= high ){      
  253.       middle = (low+high)/2;
  254.       if(key==records[middle].getName()){
  255.         return middle;
  256.  
  257.       }else if(key<records[middle].getName()){
  258.         high = middle-1; //reset high index to left side
  259.  
  260.       }else{
  261.         low = middle+1; //reset low index to right side
  262.  
  263.       }
  264.     }
  265.  
  266.     return -1;   //key not found
  267.   }
  268.  
  269.   //Search Same Date Record
  270.   int binarySearchDate(Reservation records[], int key, int low, int high){    
  271.     int middle; //the middle index number of the array    
  272.     bubbleSortDate(records);
  273.     while (low <= high ){      
  274.       middle = (low+high)/2;
  275.       if(key==records[middle].getDate()){
  276.         return middle;
  277.  
  278.       }else if(key<records[middle].getDate()){
  279.         high = middle-1; //reset high index to left side
  280.  
  281.       }else{
  282.         low = middle+1; //reset low index to right side
  283.  
  284.       }
  285.     }
  286.  
  287.     return -1;   //key not found
  288.   }
  289.  
  290.   //Search Phone No Record
  291.   int binarySearchPaxNo(Reservation records[], int key, int low, int high){    
  292.     int middle; //the middle index number of the array    
  293.     bubbleSortPaxNo(records);
  294.     while (low <= high ){      
  295.       middle = (low+high)/2;
  296.       if(key<=records[middle].getPaxNo()){
  297.         return 1;
  298.       }else{
  299.         low = middle+1; //reset low index to right side
  300.  
  301.       }
  302.     }
  303.  
  304.     return -1;   //key not found
  305.   }
  306.  
  307.   //RECURSION FUNCTIONS
  308.   //Given date, Display all with same date
  309.   void givenDate (Reservation records[], int date, int high){
  310.     if (high < 0){
  311.       return;
  312.     }
  313.     else if (date == records[high].getDate() ){
  314.       records[high].display();
  315.       return givenDate(records, date, high-1);
  316.     }
  317.     else {
  318.       return givenDate(records, date, high-1);
  319.     }
  320.   }
  321.  
  322.  
  323.   //PART 6 FUNCTION
  324.   //Give number of pax, Display all with at least given number of pax
  325.   void givenPaxNo (Reservation records[], int pax, int high){
  326.    
  327.     if(high < 0){
  328.       return;
  329.     }
  330.     else if (pax <= records[high].getPaxNo() ){
  331.       records[high].display();
  332.       return givenPaxNo(records, pax, high-1);
  333.     }
  334.     if(pax>records[high].getPaxNo()){
  335.       return;
  336.     }
  337.     else {
  338.       return givenPaxNo(records, pax, high-1);
  339.     }
  340.   }
  341.  
  342.   //ERROR CHECKING FUNCTIONS
  343.   //for checking whether the name is empty
  344.   bool checkName(string name){
  345.     int counter = 0;
  346.     bool foundTextFile = true;
  347.     //if found any characters other than space
  348.     if(name.find_first_not_of(" ") != string::npos){
  349.       counter++;
  350.     }
  351.  
  352.     //if found any characters other than the alphabets and a space
  353.     if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != string::npos){
  354.       foundTextFile = true;
  355.     }else{
  356.       foundTextFile = false;
  357.     }
  358.  
  359.     //if no input or if got any non alphabetic character
  360.     if(counter == 0 || foundTextFile){
  361.       cout<<"Invalid Input! Need a name!"<<endl;
  362.       return false;
  363.     }else{
  364.       return true;
  365.     }
  366.   }
  367.  
  368.   //Check whether that day exists
  369.   bool checkDay(int day, int month, int year){
  370.     //even months
  371.     if(day <= 0){
  372.       cout<<"Invalid Input! Enter positive numbers!"<<endl;
  373.     }else if(month%2 == 0){
  374.       // if the month is february
  375.       if(month == 2){
  376.         //february in common year that contain 28 days
  377.         if(year%4 != 0 && day>28){
  378.           cout << "Please key in between the day from 1 to 28"<< endl;
  379.         }
  380.         // february in leap year that contain 29 days
  381.         else if(year%4 == 0 && day>29){
  382.           cout << "Please key in between the day from 1 to 29"<< endl;
  383.         }
  384.         else{
  385.           return true;
  386.         }
  387.       }
  388.       // month that are even with 31 days
  389.       else if(month > 7 && day > 31){
  390.         cout << "Please key in between the day from 1 to 31"<< endl;
  391.       }
  392.       // month that are even  with 30 days
  393.       else if(month < 7 && day > 30){
  394.         cout << "Please key in between the day from 1 to 30"<< endl;
  395.       }
  396.       else{
  397.         return true;
  398.       }
  399.  
  400.     // month is odd
  401.     }else if(month%2 != 0){
  402.       // month that are odd with 30 days
  403.       if(month>8 && day > 30){
  404.         cout << "Please key in between the day from 1 to 30"<< endl;
  405.       // month that are odd with 31 days
  406.       }else if(month<8 && day > 31){
  407.         cout << "Please key in between the day from 1 to 31"<< endl;
  408.       }else{
  409.         return true;
  410.       }
  411.     }
  412.     else{
  413.       return true;
  414.     }
  415.     return false;
  416.   }
  417.  
  418.   ///////////////////////////////////////////////////////////////////////////////////
  419.  
  420.   //SELECTION FUNCTIONS//
  421.   //Option 1 : Display All sorted by...
  422.   void displayAll(){
  423.     char choice;
  424.     cout << "-----------------=DISPLAY ALL=------------------" << endl;
  425.     cout << "Display all sorted by..." << endl <<
  426.     "1. Name" << endl <<
  427.     "2. Date" << endl<<
  428.     "3. Back to Main Menu"<< endl;
  429.     cout << "Choice: ";
  430.     cin >> choice;
  431.     cin.clear();
  432.     cin.ignore(1, '\n');
  433.     system("CLS");
  434.  
  435.     switch (choice) {
  436.  
  437.     //Display all sorted by name (PART 1)
  438.     case '1':
  439.       bubbleSortName(records);
  440.       displayColumnNames();
  441.  
  442.       for(int i = 0; i<MAX; i++){
  443.         records[i].display();
  444.       }
  445.       cout<<setfill('-')<<setw(87)<<"-"<<endl;
  446.  
  447.       break;
  448.  
  449.     //Display all sorted by date (PART 2)
  450.     case '2':
  451.       bubbleSortDate(records);
  452.       displayColumnNames();
  453.      
  454.       for(int i = 0; i<MAX; i++){
  455.         records[i].display();
  456.       }
  457.       cout<<setfill('-')<<setw(87)<<"-"<<endl;
  458.  
  459.       break;
  460.  
  461.     case '3':
  462.       return;
  463.  
  464.     default:
  465.       cout << choice << " is not valid choice" << endl<<endl;
  466.       displayAll();
  467.     }
  468.  
  469.     cout <<endl<< "Press enter to continue . . .";
  470.     cin.clear();
  471.     cin.ignore(1000, '\n');
  472.     system("CLS");
  473.     return;
  474.   }
  475.  
  476.   //Option 2 : Search...
  477.   void searchNDisplay(){
  478.     char choice;
  479.     int foundIndex, phoneNo, correct;
  480.     string name;
  481.     cout << "-----------------=SEARCH & DISPLAY=------------------" << endl;
  482.     cout << "Search..." << endl <<
  483.     "1. Phone Number" << endl <<
  484.     "2. Name" << endl<<
  485.     "3. Back to Main Menu"<< endl;
  486.     cout << "Choice: ";
  487.     cin >> choice;
  488.     cin.clear();
  489.     cin.ignore(1, '\n');
  490.     system("CLS");
  491.  
  492.     switch (choice) {
  493.  
  494.     //Search by Phone Number (PART 3)
  495.     case '1':
  496.       cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  497.       cin>>phoneNo;
  498.  
  499.       correct = 1;
  500.       while (correct == 1){
  501.         if (cin.fail() || phoneNo<0){
  502.           cin.clear();
  503.           cin.ignore();
  504.           cout << "Invalid input! Enter positive numbers!" << endl<<endl;
  505.           cout<< "Enter phone number (e.g : 0123456789) : ";
  506.           cin >> phoneNo;
  507.         }else{
  508.           correct = 0;
  509.         }
  510.       }
  511.  
  512.       foundIndex = binarySearchPhoneNo(records, phoneNo, 0, MAX-1);
  513.       //If not found
  514.       if(foundIndex==-1){
  515.         cout<<"No such record with that phone number."<<endl;
  516.       }else{
  517.         //If found
  518.         displayColumnNames();
  519.         records[foundIndex].display();
  520.       }
  521.       cout<<setfill('-')<<setw(87)<<"-"<<endl;
  522.  
  523.       getchar();
  524.       cout <<endl<< "Press enter to continue . . .";
  525.       cin.clear();
  526.       cin.ignore(1000, '\n');
  527.       system("CLS");
  528.       return;
  529.  
  530.     //Search by Name (PART 4)
  531.     case '2':
  532.       do{
  533.         cout<<"Enter name : ";
  534.         getline(cin, name);
  535.       }while(!checkName(name));
  536.  
  537.       foundIndex = binarySearchName(records, name, 0, MAX-1);
  538.       //If not found
  539.       if(foundIndex==-1){
  540.         cout<<"No such record with that name."<<endl;
  541.       }else{
  542.         //If found
  543.         displayColumnNames();
  544.         records[foundIndex].display();
  545.       }
  546.       cout<<setfill('-')<<setw(87)<<"-"<<endl;
  547.  
  548.       cout <<endl<< "Press enter to continue . . .";
  549.       cin.clear();
  550.       cin.ignore(1000, '\n');
  551.       system("CLS");
  552.       return;
  553.  
  554.     case '3':
  555.       return;
  556.  
  557.     default:
  558.       cout << choice << " is not valid choice" << endl<<endl;
  559.       searchNDisplay();
  560.     }
  561.   }
  562.  
  563.   //Option 3 : Given...display all
  564.   void givenDisplayAll(){
  565.     char choice;
  566.     int year, month, day, date, paxNo, correct;
  567.     cout << "-----------------=GIVEN DISPLAY ALL=------------------" << endl;
  568.     cout << "Given...display all records with..." << endl <<
  569.     "1. Date, the same date" << endl <<
  570.     "2. Number of Pax, at least the given number of pax" << endl<<
  571.     "3. Back to Main Menu"<< endl;
  572.     cout << "Choice: ";
  573.     cin >> choice;
  574.     cin.clear();
  575.     cin.ignore(1, '\n');
  576.     system("CLS");
  577.  
  578.     switch (choice) {
  579.  
  580.     //Display by Date (PART 5)
  581.     case '1':
  582.       correct = 1;
  583.       cout<<endl<<"Enter year (e.g : 2019): ";
  584.       cin>>year;
  585.       while (correct == 1){
  586.         if (cin.fail() || year<0){
  587.           cin.clear();
  588.           cin.ignore();
  589.           cout << "Invalid input! Enter positive numbers!" << endl<<endl;
  590.           cout<< "Enter year (e.g : 2019) : ";
  591.           cin >> year;
  592.         }else{
  593.           correct = 0;
  594.         }
  595.       }
  596.  
  597.       correct = 1;
  598.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  599.       cin>>month;
  600.       while (correct == 1){
  601.         if (cin.fail() || month<1 || month>12){
  602.           cin.clear();
  603.           cin.ignore();
  604.           cout << "Invalid input! Only numbers from 1 to 12!" << endl<<endl;
  605.           cout<< "Enter month (e.g : January = 1) : ";
  606.           cin >> month;
  607.         }else{
  608.           correct = 0;
  609.         }
  610.       }
  611.  
  612.       do{
  613.         correct = 1;
  614.         cout<<endl<<"Enter day : ";
  615.         cin>>day;
  616.         while (correct == 1){
  617.           if (cin.fail()){
  618.             cin.clear();
  619.             cin.ignore();
  620.             cout << "Invalid input! Enter numbers!" << endl<<endl;
  621.             cout<< "Enter day : ";
  622.             cin >> day;
  623.           }else{
  624.             correct = 0;
  625.           }
  626.         }
  627.       }while(!checkDay(day, month, year));
  628.  
  629.       date = year*10000 + month*100 + day;
  630.  
  631.       if(binarySearchDate(records, date, 0, MAX-1) != -1){
  632.         displayColumnNames();
  633.         //Call recursion function here
  634.         givenDate(records, date, MAX-1);
  635.         cout<<setfill('-')<<setw(87)<<"-"<<endl;
  636.       }else{
  637.         cout<<endl<<"No such record with that date."<<endl;
  638.       }
  639.       break;
  640.  
  641.     //Display by Pax Number (PART 6)
  642.     case '2':
  643.       correct = 1;
  644.       cout<<endl<<"Enter number of pax : ";
  645.       cin>>paxNo;
  646.  
  647.       while (correct == 1){
  648.         if (cin.fail() || paxNo<=0){
  649.           cin.clear();
  650.           cin.ignore();
  651.           cout << "Invalid input! Enter positive numbers (mininimum : 1)!" << endl<<endl;
  652.           cout<< "Enter number of pax : ";
  653.           cin >> paxNo;
  654.         }else{
  655.           correct = 0;
  656.         }
  657.       }
  658.  
  659.       if(binarySearchPaxNo(records, paxNo, 0, MAX-1) != -1){
  660.         displayColumnNames();
  661.         //Call recursion function here
  662.         givenPaxNo(records, paxNo, MAX-1);
  663.         cout<<setfill('-')<<setw(87)<<"-"<<endl;
  664.       }else{
  665.         cout<<endl<<"No such record with the same number of pax or more."<<endl;
  666.       }
  667.       break;
  668.  
  669.     case '3':
  670.       return;
  671.  
  672.     default:
  673.       cout << choice << " is not valid choice" << endl;
  674.       displayAll();
  675.     }
  676.  
  677.     getchar();
  678.     cout <<endl<< "Press enter to continue . . .";
  679.     cin.clear();
  680.     cin.ignore(1000, '\n');
  681.     system("CLS");
  682.     return;
  683.   }
  684. };
  685.  
  686. #endif
Advertisement
Add Comment
Please, Sign In to add comment