tiffprag

Asgn 2(5)

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