tiffprag

Asgn 2(2)

Nov 12th, 2019
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 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 1: Search & Display...
  90. void searchNDisplay();
  91.  
  92. int main(){
  93.   for(int i = 0; i<MAX; i++){
  94.     records[i].setBookingNo(i+1);
  95.   }
  96.  
  97.   records[0].setName("Tan Phit Huan");
  98.   records[1].setName("Khong Ziv Hale");
  99.   records[2].setName("Rex Luah");
  100.   records[3].setName("Lee Yeu Gor");
  101.   records[4].setName("Tiffany Pragasam");
  102.  
  103.   for(int i = 0; i<MAX; i++){
  104.     records[i].setPhoneNo(123456777+i);
  105.   }
  106.  
  107.   records[0].setPaxNo(2);
  108.   records[1].setPaxNo(5);
  109.   records[2].setPaxNo(7);
  110.   records[3].setPaxNo(4);
  111.   records[4].setPaxNo(10);
  112.  
  113.   records[0].setDate(20191212);
  114.   records[1].setDate(20191111);
  115.   records[2].setDate(20191111);
  116.   records[3].setDate(20191212);
  117.   records[4].setDate(20191211);
  118.  
  119.   for(int i = 0; i<MAX; i++){
  120.     records[i].setTime(1200+(i*100));
  121.   }
  122.  
  123.   char choice;
  124.   cout << "===============RESTAURANT RESERVATION SYSTEM II===============" << endl;
  125.   cout << "Please enter your selection" << endl <<
  126.     "1. Display All Sorted by..." << endl <<
  127.     "2. Search & Display..." << endl <<
  128.     "3. Given...display all..." << endl <<
  129.     "4. Exit" << endl;
  130.  
  131.   cout << "Choice: ";
  132.   cin >> choice;
  133.   cin.clear();
  134.   cin.ignore(1, '\n');
  135.   system("CLS");
  136.  
  137.   switch (choice) {
  138.  
  139.   //Display All Sorted by...
  140.   case '1':
  141.     displayAll();
  142.  
  143.   //Search...
  144.   case '2':
  145.     searchNDisplay();
  146.  
  147.   //Given...display all
  148.   case '3':
  149.  
  150.  
  151.   case '4':
  152.     exit(1);
  153.  
  154.   default:
  155.     cout << choice << " is not valid choice" << endl;
  156.     main();
  157.   }
  158.  
  159.   return 0;
  160. }
  161.  
  162. //Used to display the top of the table
  163. void displayColumnNames();
  164. //Bubble sort by Name
  165. void bubbleSortName(Reservation A[]);
  166. //Bubble sort by Date
  167. void bubbleSortDate(Reservation A[]);
  168.  
  169. //Option 1 : Display All sorted by...
  170. void displayAll(){
  171.   char choice;
  172.   cout << "-----------------=DISPLAY ALL=------------------" << endl;
  173.   cout << "Display all sorted by..." << endl <<
  174.   "1. Name" << endl <<
  175.   "2. Date" << endl<<
  176.   "3. Back to Main Menu"<< endl;
  177.   cout << "Choice: ";
  178.   cin >> choice;
  179.   cin.clear();
  180.   cin.ignore(1, '\n');
  181.   system("CLS");
  182.  
  183.   switch (choice) {
  184.  
  185.   //Display all sorted by name
  186.   case '1':
  187.     bubbleSortName(records);
  188.     displayColumnNames();
  189.  
  190.     for(int i = 0; i<MAX; i++){
  191.       records[i].display();
  192.     }
  193.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  194.  
  195.     break;
  196.  
  197.   //Display all sorted by date
  198.   case '2':
  199.     bubbleSortDate(records);
  200.     displayColumnNames();
  201.    
  202.     for(int i = 0; i<MAX; i++){
  203.       records[i].display();
  204.     }
  205.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  206.  
  207.     break;
  208.  
  209.   case '3':
  210.     main();
  211.  
  212.   default:
  213.     cout << choice << " is not valid choice" << endl<<endl;
  214.     displayAll();
  215.   }
  216.  
  217.   cout << "Press enter to continue . . .";
  218.   cin.clear();
  219.   cin.ignore(1000, '\n');
  220.   system("CLS");
  221.   main();
  222. }
  223.  
  224. void displayColumnNames(){
  225.   cout<<setfill('_')<<setw(87)<<"_"<<endl
  226.   <<setfill(' ')<<setw(5)<<"| No. "
  227.   <<left<<setw(30)<<"Name"
  228.   <<setw(5)<<"Phone No."
  229.   <<right<<setw(16)<<"Pax No."
  230.   <<setw(10)<<"Date"
  231.   <<setw(16)<<"Time |"<<endl
  232.   <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  233. }
  234.  
  235. //Search Phone No Record
  236. int binarySearchPhoneNo(Reservation A[], int key, int low, int high);
  237. ////Search Name Record
  238. int binarySearchName(Reservation A[], string key, int low, int high);
  239. //Option 2 : Search...
  240. void searchNDisplay(){
  241.   char choice;
  242.   int foundIndex, phoneNo;
  243.   string name;
  244.   cout << "-----------------=SEARCH & DISPLAY=------------------" << endl;
  245.   cout << "Search..." << endl <<
  246.   "1. Phone Number" << endl <<
  247.   "2. Name" << endl<<
  248.   "3. Back to Main Menu"<< endl;
  249.   cout << "Choice: ";
  250.   cin >> choice;
  251.   cin.clear();
  252.   cin.ignore(1, '\n');
  253.   system("CLS");
  254.  
  255.   switch (choice) {
  256.  
  257.   //Search by Phone Number
  258.   case '1':
  259.     cout<<"Enter phone number : ";
  260.     cin>>phoneNo;
  261.     foundIndex = binarySearchPhoneNo(records, phoneNo, 0, MAX-1);
  262.     if(foundIndex==-1){
  263.       cout<<"No such record with that phone number."<<endl;
  264.     }else{
  265.       displayColumnNames();
  266.       records[foundIndex].display();
  267.     }
  268.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  269.  
  270.     getchar();
  271.     cout << "Press enter to continue . . .";
  272.     cin.clear();
  273.     cin.ignore(1000, '\n');
  274.     system("CLS");
  275.     main();
  276.  
  277.   //Search by Name
  278.   case '2':
  279.     cout<<"Enter name : ";
  280.     getline(cin, name);
  281.     foundIndex = binarySearchName(records, name, 0, MAX-1);
  282.     if(foundIndex==-1){
  283.       cout<<"No such record with that name."<<endl;
  284.     }else{
  285.       displayColumnNames();
  286.       records[foundIndex].display();
  287.     }
  288.     cout<<setfill('-')<<setw(87)<<"-"<<endl;
  289.  
  290.     cout << "Press enter to continue . . .";
  291.     cin.clear();
  292.     cin.ignore(1000, '\n');
  293.     system("CLS");
  294.     main();
  295.  
  296.   case '3':
  297.     main();
  298.  
  299.   default:
  300.     cout << choice << " is not valid choice" << endl<<endl;
  301.     searchNDisplay();
  302.   }
  303. }
  304.  
  305. //Option 3 : Given...display all
  306. void givenDisplayAll(){
  307.   char choice;
  308.   int year, month, day, date;
  309.   string name;
  310.   cout << "-----------------=GIVEN DISPLAY ALL=------------------" << endl;
  311.   cout << "Given...display all records with..." << endl <<
  312.   "1. Date, the same date" << endl <<
  313.   "2. Number of Pax, at least the given number of pax" << endl<<
  314.   "3. Back to Main Menu"<< endl;
  315.   cout << "Choice: ";
  316.   cin >> choice;
  317.   cin.clear();
  318.   cin.ignore(1, '\n');
  319.   system("CLS");
  320.  
  321.   switch (choice) {
  322.  
  323.   //Search by Phone Number
  324.   case '1':
  325.     cout<<"Enter year : ";
  326.     cin>>year;
  327.     cout<<"Enter month : ";
  328.     cin>>month;
  329.     cout<<"Enter day : ";
  330.     cin>>day;
  331.     date = year*10000 + month*100 + day;
  332.  
  333.  
  334.  
  335.   //Search by Name
  336.   case '2':
  337.     cout<<"Enter name : ";
  338.     getline(cin, name);
  339.    
  340.  
  341.  
  342.   case '3':
  343.     main();
  344.  
  345.   default:
  346.     cout << choice << " is not valid choice" << endl;
  347.     displayAll();
  348.   }
  349.  
  350.   cout << "Press enter to continue . . .";
  351.   cin.clear();
  352.   cin.ignore(1000, '\n');
  353.   system("CLS");
  354.   main();
  355. }
  356.  
  357. //Bubble sort by Name
  358. void bubbleSortName(Reservation A[]){
  359.     int i,j;
  360.     Reservation current;
  361.     for(i=0; i<MAX;i++){  
  362.       for (j=0; j<MAX-1; j++){  
  363.         if(A[j].getName()>A[j+1].getName()){              
  364.           current = A[j];              
  365.           A[j] = A[j+1];              
  366.           A[j+1] = current;          
  367.         }        
  368.       }          
  369.     }
  370. }
  371.  
  372. //Bubble sort by Date
  373. void bubbleSortDate(Reservation A[]){
  374.     int i,j;
  375.     Reservation current;
  376.     for(i=0; i<MAX;i++){  
  377.       for (j=0; j<MAX-1; j++){  
  378.         if(A[j].getDate()>A[j+1].getDate()){              
  379.           current = A[j];              
  380.           A[j] = A[j+1];              
  381.           A[j+1] = current;          
  382.         }        
  383.       }          
  384.     }
  385. }
  386.  
  387. //Bubble sort by Phone Number
  388. void bubbleSortPhoneNo(Reservation A[]){
  389.     int i,j;
  390.     Reservation current;
  391.     for(i=0; i<MAX;i++){  
  392.       for (j=0; j<MAX-1; j++){  
  393.         if(A[j].getPhoneNo()>A[j+1].getPhoneNo()){              
  394.           current = A[j];              
  395.           A[j] = A[j+1];              
  396.           A[j+1] = current;          
  397.         }        
  398.       }          
  399.     }
  400. }
  401.  
  402. //Search Phone No Record
  403. int binarySearchPhoneNo(Reservation A[], int key, int low, int high){    
  404.   int middle; //the middle index number of the array    
  405.   bubbleSortPhoneNo(A);
  406.   while (low <= high ){      
  407.     middle = (low+high)/2;
  408.     if(key==A[middle].getPhoneNo()){
  409.       return middle;
  410.  
  411.     }else if(key<A[middle].getPhoneNo()){
  412.       high = middle-1; //reset high index to left side
  413.  
  414.     }else{
  415.       low = middle+1; //reset low index to right side
  416.  
  417.     }
  418.   }
  419.  
  420.   return -1;   //key not found
  421. }
  422.  
  423. //Search Name Record
  424. int binarySearchName(Reservation A[], string key, int low, int high){    
  425.   int middle; //the middle index number of the array    
  426.   bubbleSortPhoneNo(A);
  427.   while (low <= high ){      
  428.     middle = (low+high)/2;
  429.     if(key==A[middle].getName()){
  430.       return middle;
  431.  
  432.     }else if(key<A[middle].getName()){
  433.       high = middle-1; //reset high index to left side
  434.  
  435.     }else{
  436.       low = middle+1; //reset low index to right side
  437.  
  438.     }
  439.   }
  440.  
  441.   return -1;   //key not found
  442. }
Advertisement
Add Comment
Please, Sign In to add comment