Advertisement
tiffprag

Asgn 2(2.1)

Nov 12th, 2019
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.59 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. //Option 2 : Search...
  244. void searchNDisplay(){
  245.   char choice;
  246.   int foundIndex, phoneNo, correct;
  247.   string name;
  248.   cout << "-----------------=SEARCH & DISPLAY=------------------" << endl;
  249.   cout << "Search..." << endl <<
  250.   "1. Phone Number" << endl <<
  251.   "2. Name" << endl<<
  252.   "3. Back to Main Menu"<< endl;
  253.   cout << "Choice: ";
  254.   cin >> choice;
  255.   cin.clear();
  256.   cin.ignore(1, '\n');
  257.   system("CLS");
  258.  
  259.   switch (choice) {
  260.  
  261.   //Search by Phone Number (PART 3)
  262.   case '1':
  263.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  264.     cin>>phoneNo;
  265.  
  266.     correct = 1;
  267.     while (correct == 1){
  268.       if (cin.fail() || phoneNo<0){
  269.         cin.clear();
  270.         cin.ignore();
  271.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  272.         cout<< "Enter phone number (e.g : 0123456789) : ";
  273.         cin >> phoneNo;
  274.       }else{
  275.         correct = 0;
  276.       }
  277.     }
  278.     foundIndex = binarySearchPhoneNo(records, phoneNo, 0, MAX-1);
  279.     if(foundIndex==-1){
  280.       cout<<"No such record with that phone number."<<endl;
  281.     }else{
  282.       displayColumnNames();
  283.       records[foundIndex].display();
  284.     }
  285.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  286.  
  287.     getchar();
  288.     cout << "Press enter to continue . . .";
  289.     cin.clear();
  290.     cin.ignore(1000, '\n');
  291.     system("CLS");
  292.     main();
  293.  
  294.   //Search by Name (PART 4)
  295.   case '2':
  296.     do{
  297.       cout<<"Enter name : ";
  298.       getline(cin, name);
  299.     }while(!checkName(name));
  300.  
  301.     foundIndex = binarySearchName(records, name, 0, MAX-1);
  302.     if(foundIndex==-1){
  303.       cout<<"No such record with that name."<<endl;
  304.     }else{
  305.       displayColumnNames();
  306.       records[foundIndex].display();
  307.     }
  308.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  309.  
  310.     cout << "Press enter to continue . . .";
  311.     cin.clear();
  312.     cin.ignore(1000, '\n');
  313.     system("CLS");
  314.     main();
  315.  
  316.   case '3':
  317.     main();
  318.  
  319.   default:
  320.     cout << choice << " is not valid choice" << endl<<endl;
  321.     searchNDisplay();
  322.   }
  323. }
  324.  
  325. //Option 3 : Given...display all
  326. void givenDisplayAll(){
  327.   char choice;
  328.   int year, month, day, date, paxNo;
  329.   cout << "-----------------=GIVEN DISPLAY ALL=------------------" << endl;
  330.   cout << "Given...display all records with..." << endl <<
  331.   "1. Date, the same date" << endl <<
  332.   "2. Number of Pax, at least the given number of pax" << endl<<
  333.   "3. Back to Main Menu"<< endl;
  334.   cout << "Choice: ";
  335.   cin >> choice;
  336.   cin.clear();
  337.   cin.ignore(1, '\n');
  338.   system("CLS");
  339.  
  340.   switch (choice) {
  341.  
  342.   //Display by Date (PART 5)
  343.   case '1':
  344.     cout<<"Enter year : ";
  345.     cin>>year;
  346.     cout<<"Enter month : ";
  347.     cin>>month;
  348.     cout<<"Enter day : ";
  349.     cin>>day;
  350.     date = year*10000 + month*100 + day;
  351.     displayColumnNames();
  352.     //Call recursion function here
  353.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  354.     break;
  355.  
  356.   //Display by Pax Number (PART 6)
  357.   case '2':
  358.     cout<<"Enter number of pax : ";
  359.     cin>>paxNo;
  360.     displayColumnNames();
  361.     //Call recursion function here
  362.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  363.     break;
  364.  
  365.   case '3':
  366.     main();
  367.  
  368.   default:
  369.     cout << choice << " is not valid choice" << endl;
  370.     displayAll();
  371.   }
  372.  
  373.   getchar();
  374.   cout << "Press enter to continue . . .";
  375.   cin.clear();
  376.   cin.ignore(1000, '\n');
  377.   system("CLS");
  378.   main();
  379. }
  380.  
  381. //PART 1 FUNCTION
  382. //Bubble sort by Name
  383. void bubbleSortName(Reservation A[]){
  384.     int i,j;
  385.     Reservation current;
  386.     for(i=0; i<MAX;i++){  
  387.       for (j=0; j<MAX-1; j++){  
  388.         if(A[j].getName()>A[j+1].getName()){              
  389.           current = A[j];              
  390.           A[j] = A[j+1];              
  391.           A[j+1] = current;          
  392.         }        
  393.       }          
  394.     }
  395. }
  396.  
  397. //PART 2 FUNCTION
  398. //Bubble sort by Date
  399. void bubbleSortDate(Reservation A[]){
  400.     int i,j;
  401.     Reservation current;
  402.     for(i=0; i<MAX;i++){  
  403.       for (j=0; j<MAX-1; j++){  
  404.         if(A[j].getDate()>A[j+1].getDate()){              
  405.           current = A[j];              
  406.           A[j] = A[j+1];              
  407.           A[j+1] = current;          
  408.         }        
  409.       }          
  410.     }
  411. }
  412.  
  413. //PART 3 FUNCTIONS
  414. //Bubble sort by Phone Number
  415. void bubbleSortPhoneNo(Reservation A[]){
  416.     int i,j;
  417.     Reservation current;
  418.     for(i=0; i<MAX;i++){  
  419.       for (j=0; j<MAX-1; j++){  
  420.         if(A[j].getPhoneNo()>A[j+1].getPhoneNo()){              
  421.           current = A[j];              
  422.           A[j] = A[j+1];              
  423.           A[j+1] = current;          
  424.         }        
  425.       }          
  426.     }
  427. }
  428.  
  429. //Search Phone No Record
  430. int binarySearchPhoneNo(Reservation A[], int key, int low, int high){    
  431.   int middle; //the middle index number of the array    
  432.   bubbleSortPhoneNo(A);
  433.   while (low <= high ){      
  434.     middle = (low+high)/2;
  435.     if(key==A[middle].getPhoneNo()){
  436.       return middle;
  437.  
  438.     }else if(key<A[middle].getPhoneNo()){
  439.       high = middle-1; //reset high index to left side
  440.  
  441.     }else{
  442.       low = middle+1; //reset low index to right side
  443.  
  444.     }
  445.   }
  446.  
  447.   return -1;   //key not found
  448. }
  449.  
  450. //PART 4 FUNCTION
  451. //Search Name Record
  452. int binarySearchName(Reservation A[], string key, int low, int high){    
  453.   int middle; //the middle index number of the array    
  454.   bubbleSortPhoneNo(A);
  455.   while (low <= high ){      
  456.     middle = (low+high)/2;
  457.     if(key==A[middle].getName()){
  458.       return middle;
  459.  
  460.     }else if(key<A[middle].getName()){
  461.       high = middle-1; //reset high index to left side
  462.  
  463.     }else{
  464.       low = middle+1; //reset low index to right side
  465.  
  466.     }
  467.   }
  468.  
  469.   return -1;   //key not found
  470. }
  471.  
  472. //PART 5 FUNCTION
  473. //Given date, Display all with same date
  474.  
  475.  
  476. //PART 6 FUNCTION
  477. //Give number of pax, Display all with at least given number of pax
  478.  
  479.  
  480. //ERROR CHECKING FUNCTIONS
  481. //for checking whether the name is empty
  482. bool checkName(string name){
  483.   int counter = 0;
  484.   bool found = true;
  485.   //if found any characters other than space
  486.   if(name.find_first_not_of(" ") != std::string::npos){
  487.     counter++;
  488.   }
  489.  
  490.   //if found any characters other than the alphabets and a space
  491.   if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV") != std::string::npos){
  492.     found = true;
  493.   }else{
  494.     found = false;
  495.   }
  496.  
  497.   //if no input or if got any non alphabetic character
  498.   if(counter == 0 || found){
  499.     cout<<"Invalid Input! Need a name!"<<endl;
  500.     return false;
  501.   }else{
  502.     return true;
  503.   }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement