Advertisement
tiffprag

Asgn 2(3)

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