Advertisement
tiffprag

full code(18)

Oct 25th, 2019
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <stdlib.h> //for exit() function
  5. #include <fstream>  //for creating text file
  6. using namespace std;
  7.  
  8. int total = 0;
  9. int bookNum = 0;
  10.  
  11. class Reservation{
  12. private:
  13.   int bookingNo;
  14.   string name;
  15.   int phoneNo;
  16.   int paxNo;
  17.   int day;
  18.   int month;
  19.   int year;
  20.   int time;
  21.   int date;
  22.  
  23. public:
  24.   Reservation(){
  25.     bookingNo = 1;
  26.     name = "Tan Phit Huan";
  27.     phoneNo = 0123456777;
  28.     paxNo = 1;
  29.     day = 1;
  30.     month = 1;
  31.     year = 2019;
  32.     time = 1500;
  33.     date = 20190101;
  34.   }
  35.   Reservation(int bNo, string na, int phNo, int pxNo, int d, int mon, int y, int t, int da){
  36.     bookingNo = bNo;
  37.     name = na;
  38.     phoneNo = phNo;
  39.     paxNo = pxNo;
  40.     day = d;
  41.     month = mon;
  42.     year = y;
  43.     time = t;
  44.     date = da;
  45.   }
  46.   void setBookingNo(int bNo){
  47.     bookingNo = bNo;
  48.   }
  49.   int getBookingNo(){
  50.     return bookingNo;
  51.   }
  52.   void setName(string na){
  53.     name = na;
  54.   }
  55.   string getName(){
  56.     return name;
  57.   }
  58.   void setPhoneNo(int phNo){
  59.     phoneNo = phNo;
  60.   }
  61.   int getPhoneNo(){
  62.     return phoneNo;
  63.   }
  64.   void setPaxNo(int pxNo){
  65.     paxNo = pxNo;
  66.   }
  67.   int getPaxNo(){
  68.     return paxNo;
  69.   }
  70.   void setDay(int d){
  71.     day = d;
  72.   }
  73.   int getDay(){
  74.     return day;
  75.   }
  76.   void setMonth(int m){
  77.     month = m;
  78.   }
  79.   int getMonth(){
  80.     return month;
  81.   }
  82.   void setYear(int y){
  83.       year = y;
  84.   }
  85.   int getYear(){
  86.     return year;
  87.   }
  88.   void setTime(int t){
  89.     time = t;
  90.   }
  91.   int getTime(){
  92.     return time;
  93.   }
  94.   void setDate(long da){
  95.     date = da;
  96.   }
  97.   int getDate(){
  98.     return date;
  99.   }
  100. };
  101.  
  102. class linklist{
  103. private:
  104.   struct node{
  105.     Reservation info;
  106.     node *link;
  107.   }*head;
  108.  
  109. public:
  110.   linklist(){
  111.     head=NULL;
  112.   }
  113.  
  114.   void InsNewNodeByBookNo(int bNo, string na, int phNo, int pxNo, int d, int mon, int y, int t, int da){
  115.     node*newNode = new node;
  116.     node*p,*q = head;
  117.     newNode -> info.setBookingNo(bNo);
  118.     newNode -> info.setName(na);
  119.     newNode -> info.setPhoneNo(phNo);
  120.     newNode -> info.setPaxNo(pxNo);
  121.     newNode -> info.setDay(d);
  122.     newNode -> info.setMonth(mon);
  123.     newNode -> info.setYear(y);
  124.     newNode -> info.setTime(t);
  125.     newNode -> info.setDate(da);
  126.  
  127.     //If no records
  128.     if (head == NULL){
  129.       head = newNode;
  130.       return;
  131.     }
  132.  
  133.     //Insert at the start of list
  134.     if (head -> info.getBookingNo() > newNode -> info.getBookingNo()){
  135.       newNode -> link = head;
  136.       head = newNode;
  137.       return;
  138.     }
  139.  
  140.     while(q->info.getBookingNo() < newNode -> info.getBookingNo() && q->link != NULL){
  141.       p = q;
  142.       q = q->link;
  143.     }
  144.     //Insert at the end of the list
  145.     if(q == NULL){
  146.       q = newNode;
  147.       newNode-> link = NULL;
  148.       return;
  149.     //Insert in the middle of the list
  150.     }else{
  151.       p = q;
  152.       q = q->link;
  153.       p->link = newNode;
  154.       newNode-> link = q;
  155.       return;
  156.     }
  157.   }
  158.  
  159.   void deleteNode(int bNo){
  160.     node *current;
  161.     current = head;
  162.     while (current != NULL){
  163.       if(bNo == current->info.getBookingNo()){
  164.         if(current == head){
  165.           node*temp = head;
  166.           head = head -> link;
  167.           delete temp;
  168.  
  169.         }else if(current->link == NULL){
  170.           node*p, *q = head;
  171.           while(q -> link != NULL){
  172.             p = q;
  173.             q = q -> link;
  174.           }
  175.           p->link = NULL;
  176.           delete q;
  177.  
  178.         }else{
  179.           node*p, *q = head;
  180.           while(q -> info.getBookingNo() != bNo){
  181.             p = q;
  182.             q = q -> link;
  183.           }
  184.           p -> link = q ->link;
  185.           delete q;
  186.         }
  187.       }
  188.       current = current->link;
  189.     }
  190.   }
  191.  
  192.   void PrintList(){
  193.     node *current;
  194.     current = head;
  195.     cout<<setfill(' ')<<setw(5)<<"| No. "
  196.     <<left<<setw(30)<<"Name"
  197.     <<setw(5)<<"Phone No."
  198.     <<right<<setw(16)<<"Pax No."
  199.     <<setw(10)<<"Date"
  200.     <<setw(16)<<"Time |"<<endl
  201.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  202.  
  203.     while (current != NULL){
  204.       cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  205.       <<left<<setw(30)<< current->info.getName()
  206.       <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  207.       <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  208.       <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  209.       <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  210.       current = current->link;
  211.     }
  212.     cout<<endl;
  213.   }
  214.  
  215.   void PrintBookingNoRecords(int bNo){
  216.     node *current;
  217.     current = head;
  218.     cout<<setfill(' ')<<setw(5)<<"| No. "
  219.     <<left<<setw(30)<<"Name"
  220.     <<setw(5)<<"Phone No."
  221.     <<right<<setw(16)<<"Pax No."
  222.     <<setw(10)<<"Date"
  223.     <<setw(16)<<"Time |"<<endl
  224.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  225.  
  226.     while (current != NULL){
  227.       //If there is any reservation with the same name
  228.       if(bNo == current->info.getBookingNo()){
  229.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  230.         <<left<<setw(30)<< current->info.getName()
  231.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  232.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  233.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  234.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  235.       }
  236.       current = current->link;
  237.     }
  238.     cout<<endl;
  239.   }
  240.  
  241.   void PrintNameRecords(string name){
  242.     node *current;
  243.     current = head;
  244.     cout<<setfill(' ')<<setw(5)<<"| No. "
  245.     <<left<<setw(30)<<"Name"
  246.     <<setw(5)<<"Phone No."
  247.     <<right<<setw(16)<<"Pax No."
  248.     <<setw(10)<<"Date"
  249.     <<setw(16)<<"Time |"<<endl
  250.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  251.  
  252.     while (current != NULL){
  253.       //If there is any reservation with the same name
  254.       if(name == current->info.getName()){
  255.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  256.         <<left<<setw(30)<< current->info.getName()
  257.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  258.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  259.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  260.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  261.       }
  262.       current = current->link;
  263.     }
  264.     cout<<endl;
  265.   }
  266.  
  267.   void PrintDateRecords(int date){
  268.     node *current;
  269.     current = head;
  270.     cout<<setfill(' ')<<setw(5)<<"| No. "
  271.     <<left<<setw(30)<<"Name"
  272.     <<setw(5)<<"Phone No."
  273.     <<right<<setw(16)<<"Pax No."
  274.     <<setw(10)<<"Date"
  275.     <<setw(16)<<"Time |"<<endl
  276.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  277.  
  278.     while (current != NULL){
  279.       //If there is any reservation with the same date
  280.       if(date == current->info.getDate()){
  281.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  282.         <<left<<setw(30)<< current->info.getName()
  283.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  284.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  285.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  286.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  287.       }
  288.       current = current->link;
  289.     }
  290.     cout<<endl;
  291.   }
  292.  
  293.   void PrintListTextFile(){
  294.     string fileName;
  295.     cout << "---------------=CREATE TEXT FILE=---------------" << endl;
  296.     //Create file with the file name user entered
  297.     cout << "Enter file name: ";
  298.     cin >> fileName;
  299.     cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  300.     fileName = fileName + ".txt";
  301.  
  302.     //output to .txt file
  303.     ofstream file(fileName.c_str());
  304.  
  305.     if (file.is_open()) {
  306.       //output all the reservation records
  307.       node *current;
  308.       current = head;
  309.       file<<setfill(' ')<<setw(5)<<"| No. "
  310.       <<left<<setw(30)<<"Name"
  311.       <<setw(5)<<"Phone No."
  312.       <<right<<setw(16)<<"Pax No."
  313.       <<setw(10)<<"Date"
  314.       <<setw(16)<<"Time |"<<endl
  315.       <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  316.  
  317.       while (current != NULL){
  318.         file<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  319.         <<left<<setw(30)<< current->info.getName()
  320.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  321.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  322.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  323.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  324.         current = current->link;
  325.       }
  326.       file.close(); //close file
  327.     }        
  328.   }
  329.  
  330.   bool checker(int bNo){
  331.     node *current;
  332.     current = head;
  333.     while(current != NULL){
  334.       if(bNo == current->info.getBookingNo()){
  335.         return true;
  336.       }
  337.       current = current->link;
  338.     }
  339.     return false;
  340.   }
  341. };
  342.  
  343. //linklist of reservations sorted by booking number
  344. linklist RBList;
  345. void addRecord();
  346. void updateRecord();
  347. void deleteRecord();
  348. void viewRecord();
  349. void createTextFile();
  350. bool checkName(string name);
  351. int checkNoOfDigits(int phoneNo);
  352. bool checkYear(int year);
  353. bool checkMonth(int month, int year);
  354. bool checkDay(int day, int month, int year);
  355. bool checkTime(int time, int day, int month, int year);
  356.  
  357. int main() {
  358.   char choice;
  359.   cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
  360.   cout << "Please enter your selection" << endl <<
  361.     "1. Add Reservation Records" << endl <<
  362.     "2. Update Reservation Records" << endl <<
  363.     "3. Delete Reservation Records" << endl <<
  364.     "4. View Reservation Records  " << endl <<
  365.     "5. Create text file" << endl <<
  366.     "6. Exit" << endl;
  367.  
  368.   cout << "Choice: ";
  369.   cin >> choice;
  370.   cin.clear();
  371.   cin.ignore(1, '\n');
  372.   system("CLS");
  373.  
  374.   switch (choice) {
  375.   //Add Records
  376.   case '1':
  377.     addRecord();
  378.     break;
  379.  
  380.   //Update Records
  381.   case '2':
  382.     if (total == 0) {
  383.       cout << "No reservation records available." << endl;
  384.       main();
  385.     }
  386.     updateRecord();
  387.  
  388.     break;
  389.  
  390.   //Delete Records
  391.   case '3':
  392.     if (total == 0) {
  393.       cout << "No reservation records available." << endl;
  394.       main();
  395.     }
  396.     deleteRecord();
  397.  
  398.     break;
  399.  
  400.   //View Reservation Records
  401.   case '4':
  402.     if (total == 0) {
  403.       cout << "No reservation records available." << endl;
  404.       main();
  405.     }
  406.     viewRecord();
  407.     break;
  408.  
  409.   //Create Text File
  410.   case '5':
  411.     if (total == 0) {
  412.       cout << "No reservation records available." << endl;
  413.       main();
  414.     }
  415.     createTextFile();
  416.     break;
  417.  
  418.   case '6':
  419.     exit(1);
  420.     break;
  421.  
  422.   default:
  423.     cout << choice << " is not valid choice" << endl;
  424.     main();
  425.   }
  426.  
  427.   return 0;
  428. }
  429.  
  430. //OPTION 1 : Add Reservation Records
  431. void addRecord(){
  432.   string name;
  433.   int phoneNo, paxNo, day, month, year, time, date;
  434.   int correct = 1;
  435.   cout << "-----------------=ADD RECORD=-------------------" << endl;
  436.   cout<<"Enter 0 to return to main menu."<<endl;
  437.   do{
  438.     cout<<"Enter name : ";
  439.     getline(cin, name);
  440.   }while(!checkName(name));
  441.  
  442.   if(name=="0"){
  443.     cout << "Press enter to continue . . .";
  444.     getchar();
  445.     system("CLS");
  446.     main();
  447.   }
  448.  
  449.   cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  450.   cin>>phoneNo;
  451.  
  452.   correct = 1;
  453.   while (correct == 1){
  454.     if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  455.       cin.clear();
  456.       cin.ignore();
  457.       cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  458.       cout<< "Enter phone number (e.g : 0123456789) : ";
  459.       cin >> phoneNo;
  460.     }else{
  461.       correct = 0;
  462.     }
  463.   }
  464.  
  465.   correct = 1;
  466.   cout<<endl<<"Enter number of pax (maximum : 50) : ";
  467.   cin>>paxNo;
  468.  
  469.   while (correct == 1){
  470.     if (cin.fail() || paxNo<=0 || paxNo > 50){
  471.       cin.clear();
  472.       cin.ignore();
  473.       cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  474.       cout<< "Enter number of pax (maximum : 50) : ";
  475.       cin >> paxNo;
  476.     }else{
  477.       correct = 0;
  478.     }
  479.   }
  480.  
  481.  
  482.   do{
  483.     correct = 1;
  484.     cout<<endl<<"Enter year (e.g : 2019): ";
  485.     cin>>year;
  486.     while (correct == 1){
  487.       if (cin.fail() || year > 2021){
  488.         cin.clear();
  489.         cin.ignore();
  490.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  491.         cout<< "Enter year (e.g : 2019) : ";
  492.         cin >> year;
  493.         //reset if a year before 2019 was entered then 2022 or later is entered
  494.       }else{
  495.         correct = 0;
  496.       }
  497.     }
  498.   }while(!checkYear(year));
  499.  
  500.   do{
  501.     correct = 1;
  502.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  503.     cin>>month;
  504.     while (correct == 1){
  505.       if (cin.fail()){
  506.         cin.clear();
  507.         cin.ignore();
  508.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  509.         cout<< "Enter month (e.g : January = 1) : ";
  510.         cin >> month;
  511.       }else{
  512.         correct = 0;
  513.       }
  514.     }
  515.   }while(!checkMonth(month, year));
  516.  
  517.   do{
  518.     correct = 1;
  519.     cout<<endl<<"Enter day : ";
  520.     cin>>day;
  521.     while (correct == 1){
  522.       if (cin.fail()){
  523.         cin.clear();
  524.         cin.ignore();
  525.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  526.         cout<< "Enter day : ";
  527.         cin >> day;
  528.       }else{
  529.         correct = 0;
  530.       }
  531.     }
  532.   }while(!checkDay(day, month, year));
  533.  
  534.   do{
  535.     correct = 1;
  536.     cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  537.     cin>>time;
  538.     while (correct == 1){
  539.       if (cin.fail()){
  540.         cin.clear();
  541.         cin.ignore();
  542.         cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  543.         cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  544.         cin >> time;
  545.       }else{
  546.         correct = 0;
  547.       }
  548.     }
  549.   }while(!checkTime(time, day, month, year));
  550.  
  551.   bookNum++;
  552.   cout<<endl<<"Booking Number : "<<bookNum<<endl;
  553.  
  554.   date = year*10000 + month*100 + day;
  555.  
  556.   RBList.InsNewNodeByBookNo(bookNum, name, phoneNo, paxNo, day, month, year, time, date);
  557.   total++;
  558.   cin.clear();
  559.   cin.ignore(1000, '\n');
  560.   cout << "Press enter to continue . . .";
  561.   getchar();
  562.   system("CLS");
  563.   main();
  564. }
  565.  
  566. //OPTION 2 : Update Reservation Records
  567. void updateRecord(){
  568.   string name;
  569.   int bNo, phoneNo, paxNo, day, month, year, time, date;
  570.   int correct = 1;
  571.   cout << "----------------=UPDATE RECORD=-----------------" << endl;
  572.   cout<<"Enter 0 to return to main menu."<<endl;
  573.   cout << "Enter Booking Number : ";
  574.   cin >> bNo;
  575.   if(bNo == 0){
  576.     cout << "Press enter to continue . . .";
  577.     getchar();
  578.     system("CLS");
  579.     main();
  580.   }
  581.  
  582.   if(RBList.checker(bNo)){
  583.     RBList.deleteNode(bNo);
  584.     cin.clear();
  585.     cin.ignore(1000, '\n');
  586.  
  587.     cout<<endl;
  588.     cout<<"Enter name : ";
  589.     getline(cin, name);
  590.  
  591.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  592.     cin>>phoneNo;
  593.  
  594.     correct = 1;
  595.     while (correct == 1){
  596.       if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  597.         cin.clear();
  598.         cin.ignore();
  599.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  600.         cout<< "Enter phone number (e.g : 0123456789) : ";
  601.         cin >> phoneNo;
  602.       }else{
  603.         correct = 0;
  604.       }
  605.     }
  606.  
  607.     correct = 1;
  608.     cout<<endl<<"Enter number of pax (maximum : 50) : ";
  609.     cin>>paxNo;
  610.  
  611.     while (correct == 1){
  612.       if (cin.fail() || paxNo<=0 || paxNo > 50){
  613.         cin.clear();
  614.         cin.ignore();
  615.         cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  616.         cout<< "Enter number of pax (maximum : 50) : ";
  617.         cin >> paxNo;
  618.       }else{
  619.         correct = 0;
  620.       }
  621.     }
  622.  
  623.     do{
  624.       correct = 1;
  625.       cout<<endl<<"Enter year (e.g : 2019): ";
  626.       cin>>year;
  627.       while (correct == 1){
  628.         if (cin.fail() || year > 2021){
  629.           cin.clear();
  630.           cin.ignore();
  631.           cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  632.           cout<< "Enter year (e.g : 2019) : ";
  633.           cin >> year;
  634.           //reset if a year before 2019 was entered then 2022 or later is entered
  635.         }else{
  636.           correct = 0;
  637.         }
  638.       }
  639.     }while(!checkYear(year));
  640.  
  641.     do{
  642.       correct = 1;
  643.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  644.       cin>>month;
  645.       while (correct == 1){
  646.         if (cin.fail()){
  647.           cin.clear();
  648.           cin.ignore();
  649.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  650.           cout<< "Enter month (e.g : January = 1) : ";
  651.           cin >> month;
  652.         }else{
  653.           correct = 0;
  654.         }
  655.       }
  656.     }while(!checkMonth(month, year));
  657.  
  658.     do{
  659.       correct = 1;
  660.       cout<<endl<<"Enter day : ";
  661.       cin>>day;
  662.       while (correct == 1){
  663.         if (cin.fail()){
  664.           cin.clear();
  665.           cin.ignore();
  666.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  667.           cout<< "Enter day : ";
  668.           cin >> day;
  669.         }else{
  670.           correct = 0;
  671.         }
  672.       }
  673.     }while(!checkDay(day, month, year));
  674.  
  675.     do{
  676.       correct = 1;
  677.       cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  678.       cin>>time;
  679.       while (correct == 1){
  680.         if (cin.fail()){
  681.           cin.clear();
  682.           cin.ignore();
  683.           cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  684.           cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  685.           cin >> time;
  686.         }else{
  687.           correct = 0;
  688.         }
  689.       }
  690.     }while(!checkTime(time, day, month, year));
  691.  
  692.     //for finding the reservation by date
  693.     date = year*10000 + month*100 + day;
  694.  
  695.     RBList.InsNewNodeByBookNo(bNo, name, phoneNo, paxNo, day, month, year, time, date);
  696.     cout<<"Update process complete."<<endl;
  697.   }else{
  698.     cout<<"No existing record with that booking number."<<endl
  699.     <<"Update process cancelled."<<endl;
  700.   }
  701.  
  702.   cin.clear();
  703.   cin.ignore(1000, '\n');
  704.   cout << "Press enter to continue . . .";
  705.   getchar();
  706.   system("CLS");
  707.   main();
  708. }
  709.  
  710. //OPTION 3 : Delete Reservation Records
  711. void deleteRecord(){
  712.   int  bNo;
  713.   char confirm;
  714.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  715.   cout<<"Enter 0 to return to main menu."<<endl;
  716.   cout<<"Enter Booking Number : ";
  717.   cin>>bNo;
  718.   if(bNo == 0){
  719.     cout << "Press enter to continue . . .";
  720.     getchar();
  721.     system("CLS");
  722.     main();
  723.   }
  724.   if(RBList.checker(bNo)){
  725.     RBList.PrintBookingNoRecords(bNo);
  726.     cout<<"Confirm?(y/n) : ";
  727.     cin>>confirm;
  728.  
  729.     switch (confirm) {
  730.       case 'y':
  731.         RBList.deleteNode(bNo);
  732.         total--;
  733.         cout<<"Deletion process complete."<<endl;
  734.         break;
  735.       default:
  736.         cout<<"Deletion process cancelled."<<endl;
  737.         main();
  738.     }
  739.   }else{
  740.     cout<<"No existing record with that booking number."<<endl;
  741.   }
  742.  
  743.   cin.clear();
  744.   cin.ignore(1000, '\n');
  745.   cout << "Press enter to continue . . .";
  746.   getchar();
  747.   system("CLS");
  748.   main();
  749. }
  750.  
  751. //OPTION 4 : View Reservation Records
  752. void viewRecord(){
  753.   char choice;
  754.   string name;
  755.   int bNo, year, month, day, date, correct;
  756.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  757.   cout << "Please enter your selection" << endl <<
  758.   "1. View All Reservations(Sorted by Booking Number)" << endl <<
  759.   "2. Search by Name" << endl<<
  760.   "3. Search by Date"<< endl <<
  761.   "4. Back to Main Menu"<< endl;
  762.   cout << "Choice: ";
  763.   cin >> choice;
  764.   cin.clear();
  765.   cin.ignore(1, '\n');
  766.   system("CLS");
  767.  
  768.   switch (choice) {
  769.   //View All Reservations
  770.   case '1':
  771.     RBList.PrintList();
  772.     break;
  773.  
  774.   //Search by Name
  775.   case '2':
  776.     do{
  777.       cout<<"Enter name : ";
  778.       getline(cin, name);
  779.     }while(!checkName(name));
  780.  
  781.     RBList.PrintNameRecords(name);
  782.     break;
  783.  
  784.   case '3':
  785.  
  786.     cout<<endl<<"Enter year (e.g : 2019): ";
  787.     cin>>year;
  788.     while (correct == 1){
  789.       if (cin.fail() || year > 2021){
  790.         cin.clear();
  791.         cin.ignore();
  792.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  793.         cout<< "Enter year (e.g : 2019) : ";
  794.         cin >> year;
  795.       }else{
  796.           correct = 0;
  797.       }
  798.     }
  799.  
  800.     correct = 1;
  801.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  802.     cin>>month;
  803.     while (correct == 1){
  804.       if (cin.fail()){
  805.         cin.clear();
  806.         cin.ignore();
  807.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  808.         cout<< "Enter month (e.g : January = 1) : ";
  809.         cin >> month;
  810.       }else{
  811.           correct = 0;
  812.       }
  813.     }
  814.  
  815.     cout<<endl<<"Enter day : ";
  816.     cin>>day;
  817.     while (correct == 1){
  818.       if (cin.fail()){
  819.         cin.clear();
  820.         cin.ignore();
  821.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  822.         cout<< "Enter day : ";
  823.         cin >> day;
  824.       }else{
  825.           correct = 0;
  826.       }
  827.     }
  828.  
  829.     date = year*10000 + month*100 + day;
  830.  
  831.     RBList.PrintDateRecords(date);
  832.     cin.clear();
  833.     cin.ignore(1000, '\n');
  834.     break;
  835.  
  836.   case '4':
  837.     main();
  838.     break;
  839.  
  840.   default:
  841.     cout << choice << " is not valid choice" << endl;
  842.     viewRecord();
  843.   }
  844.  
  845.   cout << "Press enter to continue . . .";
  846.   cin.clear();
  847.   cin.ignore(1000, '\n');
  848.   system("CLS");
  849.   main();
  850. }
  851.  
  852. //OPTION 5 : Create Text File
  853. void createTextFile(){
  854.   char choice;
  855.   cout << "------------------=Create Txt File=------------------" << endl;
  856.   cout << "Please enter your selection" << endl <<
  857.   "1. Create Sort By Booking Number." << endl <<
  858.   "2. Back to Main Menu"<< endl;
  859.   cout << "Choice: ";
  860.   cin >> choice;
  861.  
  862.   switch (choice) {
  863.   //View All Reservations
  864.   case '1':
  865.     RBList.PrintListTextFile();
  866.     break;
  867.  
  868.   case '2':
  869.     main();
  870.     break;
  871.  
  872.   default:
  873.     cout << choice << " is not valid choice" << endl;
  874.     viewRecord();
  875.   }
  876.  
  877.   getchar();
  878.   cout << "-----------------------------------------------------" << endl;
  879.  
  880.   cout << "Press enter to continue . . .";
  881.   cin.clear();
  882.   cin.ignore(1000, '\n');
  883.   system("CLS");
  884.   main();
  885. }
  886.  
  887. //for checking whether the name is empty
  888. bool checkName(string name){
  889.   int counter = 0;
  890.   bool found = true;
  891.   if(name.find_first_not_of(" ") != std::string::npos){
  892.     counter++;
  893.   }
  894.  
  895.   if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0") != std::string::npos){
  896.     found = false;
  897.   }else{
  898.     found = true;
  899.   }
  900.  
  901.   if(counter == 0 || !found){
  902.     cout<<"Invalid Input! Need a name!"<<endl;
  903.     return false;
  904.   }else{
  905.     return true;
  906.   }
  907. }
  908.  
  909. int checkNoOfDigits(int phoneNo){
  910.   if (phoneNo == 0){
  911.     return 0;
  912.   }else{
  913.     return 1 + checkNoOfDigits(phoneNo / 10);
  914.   }
  915. }
  916.  
  917. //for getting the current time
  918. time_t now = time(0);
  919. tm *ltm = localtime(&now);
  920.  
  921. int currentYear = 1900+ltm->tm_year;
  922. int currentMonth = 1 + ltm->tm_mon;
  923. int currentDay = ltm->tm_mday;
  924. int currentHour = ltm->tm_hour;
  925. int currentMinute = ltm->tm_min;
  926.  
  927. bool checkYear(int year){
  928.   if(year < currentYear){
  929.     cout<<"Can't go back in time!"<<endl;
  930.     return false;
  931.   }else{
  932.     return true;
  933.   }
  934. }
  935.  
  936. bool checkMonth(int month, int year){
  937.   if(month < currentMonth && year == currentYear){
  938.     cout<<"Can't go back in time!"<<endl;
  939.     return false;
  940.   }else if(month > 12){
  941.     cout<<"There are only 12 months in a year!"<<endl;
  942.     return false;
  943.   }else{
  944.     return true;
  945.   }
  946. }
  947.  
  948. bool checkDay(int day, int month, int year){
  949.   if(day < currentDay && month == currentMonth && year == currentYear){
  950.     cout<<"Can't go back in time!"<<endl;
  951.   //even months
  952.   }else if(day <= 0){
  953.     cout<<"Can't go back in time!"<<endl;
  954.   }else if(month%2 == 0){
  955.     // if the month is february
  956.     if(month == 2){
  957.       //february in common year that contain 28 days
  958.       if(year%4 != 0 && day>28){
  959.         cout << "Please key in between the day from 1 to 28"<< endl;
  960.       }
  961.       // february in leap year that contain 29 days
  962.       else if(year%4 == 0 && day>29){
  963.         cout << "Please key in between the day from 1 to 29"<< endl;
  964.       }
  965.       else{
  966.         return true;
  967.       }
  968.     }
  969.     // month that are even with 31 days
  970.     else if(month > 7 && day > 31){
  971.       cout << "Please key in between the day from 1 to 31"<< endl;
  972.     }
  973.     // month that are even  with 30 days
  974.     else if(month < 7 && day > 30){
  975.       cout << "Please key in between the day from 1 to 30"<< endl;
  976.     }
  977.     else{
  978.       return true;
  979.     }
  980.  
  981.   // month is odd
  982.   }else if(month%2 != 0){
  983.     // month that are odd with 30 days
  984.     if(month>8 && day > 30){
  985.       cout << "Please key in between the day from 1 to 30"<< endl;
  986.     // month that are odd with 31 days
  987.     }else if(month<8 && day > 31){
  988.       cout << "Please key in between the day from 1 to 31"<< endl;
  989.     }else{
  990.       return true;
  991.     }
  992.   }
  993.   else{
  994.     return true;
  995.   }
  996.   return false;
  997. }
  998.  
  999. bool checkTime(int time, int day, int month, int year){
  1000.   int hour = time/100;
  1001.   int minute = time - hour*100;
  1002.  
  1003.   if(hour > 24){
  1004.     cout<<"Only 0 to 24 hours."<<endl;
  1005.     return false;
  1006.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  1007.     cout<<"Can't go back in time!"<<endl;
  1008.     return false;
  1009.   }else if(minute > 59){
  1010.     cout<<"Only 0 to 59 minutes."<<endl;
  1011.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  1012.     cout<<"Can't go back in time!"<<endl;
  1013.   }else{
  1014.     return true;
  1015.   }
  1016.   return false;
  1017. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement