Advertisement
tiffprag

full code(15)

Oct 25th, 2019
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.91 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 bookingNo = 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.     total--;
  191.   }
  192.  
  193.   void PrintList(){
  194.     node *current;
  195.     current = head;
  196.     cout<<setfill(' ')<<setw(5)<<"| No. "
  197.     <<left<<setw(30)<<"Name"
  198.     <<setw(5)<<"Phone No."
  199.     <<right<<setw(16)<<"Pax No."
  200.     <<setw(10)<<"Date"
  201.     <<setw(16)<<"Time |"<<endl
  202.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  203.  
  204.     while (current != NULL){
  205.       cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  206.       <<left<<setw(30)<< current->info.getName()
  207.       <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  208.       <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  209.       <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  210.       <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  211.       current = current->link;
  212.     }
  213.     cout<<endl;
  214.   }
  215.  
  216.   void PrintBookingNoRecords(int bNo){
  217.     node *current;
  218.     current = head;
  219.     cout<<setfill(' ')<<setw(5)<<"| No. "
  220.     <<left<<setw(30)<<"Name"
  221.     <<setw(5)<<"Phone No."
  222.     <<right<<setw(16)<<"Pax No."
  223.     <<setw(10)<<"Date"
  224.     <<setw(16)<<"Time |"<<endl
  225.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  226.  
  227.     while (current != NULL){
  228.       //If there is any reservation with the same name
  229.       if(bNo == current->info.getBookingNo()){
  230.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  231.         <<left<<setw(30)<< current->info.getName()
  232.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  233.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  234.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  235.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  236.       }
  237.       current = current->link;
  238.     }
  239.     cout<<endl;
  240.   }
  241.  
  242.   void PrintNameRecords(string name){
  243.     node *current;
  244.     current = head;
  245.     cout<<setfill(' ')<<setw(5)<<"| No. "
  246.     <<left<<setw(30)<<"Name"
  247.     <<setw(5)<<"Phone No."
  248.     <<right<<setw(16)<<"Pax No."
  249.     <<setw(10)<<"Date"
  250.     <<setw(16)<<"Time |"<<endl
  251.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  252.  
  253.     while (current != NULL){
  254.       //If there is any reservation with the same name
  255.       if(name == current->info.getName()){
  256.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  257.         <<left<<setw(30)<< current->info.getName()
  258.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  259.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  260.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  261.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  262.       }
  263.       current = current->link;
  264.     }
  265.     cout<<endl;
  266.   }
  267.  
  268.   void PrintDateRecords(int date){
  269.     node *current;
  270.     current = head;
  271.     cout<<setfill(' ')<<setw(5)<<"| No. "
  272.     <<left<<setw(30)<<"Name"
  273.     <<setw(5)<<"Phone No."
  274.     <<right<<setw(16)<<"Pax No."
  275.     <<setw(10)<<"Date"
  276.     <<setw(16)<<"Time |"<<endl
  277.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  278.  
  279.     while (current != NULL){
  280.       //If there is any reservation with the same date
  281.       if(date == current->info.getDate()){
  282.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  283.         <<left<<setw(30)<< current->info.getName()
  284.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  285.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  286.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  287.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  288.       }
  289.       current = current->link;
  290.     }
  291.     cout<<endl;
  292.   }
  293.  
  294.   void PrintListTextFile(){
  295.     string fileName;
  296.     cout << "---------------=CREATE TEXT FILE=---------------" << endl;
  297.     //Create file with the file name user entered
  298.     cout << "Enter file name: ";
  299.     cin >> fileName;
  300.     cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  301.     fileName = fileName + ".txt";
  302.  
  303.     //output to .txt file
  304.     ofstream file(fileName.c_str());
  305.  
  306.     if (file.is_open()) {
  307.       //output all the reservation records
  308.       node *current;
  309.       current = head;
  310.       file<<setfill(' ')<<setw(5)<<"| No. "
  311.       <<left<<setw(30)<<"Name"
  312.       <<setw(5)<<"Phone No."
  313.       <<right<<setw(16)<<"Pax No."
  314.       <<setw(10)<<"Date"
  315.       <<setw(16)<<"Time |"<<endl
  316.       <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  317.  
  318.       while (current != NULL){
  319.         file<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  320.         <<left<<setw(30)<< current->info.getName()
  321.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  322.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  323.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  324.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  325.         current = current->link;
  326.       }
  327.       file.close(); //close file
  328.     }        
  329.   }
  330.  
  331.   bool checker(int bNo){
  332.     node *current;
  333.     current = head;
  334.     while(current != NULL){
  335.       if(bNo == current->info.getBookingNo()){
  336.         return true;
  337.       }
  338.       current = current->link;
  339.     }
  340.     return false;
  341.   }
  342. };
  343.  
  344. //linklist of reservations sorted by booking number
  345. linklist RBList;
  346. void addRecord();
  347. void updateRecord();
  348. void deleteRecord();
  349. void viewRecord();
  350. void createTextFile();
  351. bool checkName(string name);
  352. int checkNoOfDigits(int phoneNo);
  353. bool checkYear(int year);
  354. bool checkMonth(int month, int year);
  355. bool checkDay(int day, int month, int year);
  356. bool checkTime(int time, int day, int month, int year);
  357.  
  358. int main() {
  359.   char choice;
  360.   cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
  361.   cout << "Please enter your selection" << endl <<
  362.     "1. Add Reservation Records" << endl <<
  363.     "2. Update Reservation Records" << endl <<
  364.     "3. Delete Reservation Records" << endl <<
  365.     "4. View Reservation Records  " << endl <<
  366.     "5. Create text file" << endl <<
  367.     "6. Exit" << endl;
  368.  
  369.   cout << "Choice: ";
  370.   cin >> choice;
  371.   cin.clear();
  372.   cin.ignore(1, '\n');
  373.   system("CLS");
  374.  
  375.   switch (choice) {
  376.   //Add Records
  377.   case '1':
  378.     addRecord();
  379.     break;
  380.  
  381.   //Update Records
  382.   case '2':
  383.     if (total == 0) {
  384.       cout << "No reservation records available." << endl;
  385.       main();
  386.     }
  387.     updateRecord();
  388.  
  389.     break;
  390.  
  391.   //Delete Records
  392.   case '3':
  393.     if (total == 0) {
  394.       cout << "No reservation records available." << endl;
  395.       main();
  396.     }
  397.     deleteRecord();
  398.  
  399.     break;
  400.  
  401.   //View Reservation Records
  402.   case '4':
  403.     if (total == 0) {
  404.       cout << "No reservation records available." << endl;
  405.       main();
  406.     }
  407.     viewRecord();
  408.     break;
  409.  
  410.   //Create Text File
  411.   case '5':
  412.     if (total == 0) {
  413.       cout << "No reservation records available." << endl;
  414.       main();
  415.     }
  416.     createTextFile();
  417.     break;
  418.  
  419.   case '6':
  420.     exit(1);
  421.     break;
  422.  
  423.   default:
  424.     cout << choice << " is not valid choice" << endl;
  425.     main();
  426.   }
  427.  
  428.   return 0;
  429. }
  430.  
  431. //OPTION 1 : Add Reservation Records
  432. void addRecord(){
  433.   string name;
  434.   int phoneNo, paxNo, day, month, year, time, date;
  435.   int correct = 1;
  436.   cout << "-----------------=ADD RECORD=-------------------" << endl;
  437.   cout<<"Enter 0 to return to main menu."<<endl;
  438.   do{
  439.     cout<<"Enter name : ";
  440.     getline(cin, name);
  441.   }while(!checkName(name));
  442.  
  443.   if(name=="0"){
  444.     cout << "Press enter to continue . . .";
  445.     getchar();
  446.     system("CLS");
  447.     main();
  448.   }
  449.  
  450.   cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  451.   cin>>phoneNo;
  452.  
  453.   correct = 1;
  454.   while (correct == 1){
  455.     if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  456.       cin.clear();
  457.       cin.ignore();
  458.       cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  459.       cout<< "Enter phone number (e.g : 0123456789) : ";
  460.       cin >> phoneNo;
  461.     }else{
  462.       correct = 0;
  463.     }
  464.   }
  465.  
  466.   correct = 1;
  467.   cout<<endl<<"Enter number of pax (maximum : 50) : ";
  468.   cin>>paxNo;
  469.  
  470.   while (correct == 1){
  471.     if (cin.fail() || paxNo<=0 || paxNo > 50){
  472.       cin.clear();
  473.       cin.ignore();
  474.       cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  475.       cout<< "Enter number of pax (maximum : 50) : ";
  476.       cin >> paxNo;
  477.     }else{
  478.       correct = 0;
  479.     }
  480.   }
  481.  
  482.  
  483.   do{
  484.     correct = 1;
  485.     cout<<endl<<"Enter year (e.g : 2019): ";
  486.     cin>>year;
  487.     while (correct == 1){
  488.       if (cin.fail() || year > 2021){
  489.         cin.clear();
  490.         cin.ignore();
  491.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  492.         cout<< "Enter year (e.g : 2019) : ";
  493.         cin >> year;
  494.         //reset if a year before 2019 was entered then 2022 or later is entered
  495.       }else{
  496.         correct = 0;
  497.       }
  498.     }
  499.   }while(!checkYear(year));
  500.  
  501.   do{
  502.     correct = 1;
  503.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  504.     cin>>month;
  505.     while (correct == 1){
  506.       if (cin.fail()){
  507.         cin.clear();
  508.         cin.ignore();
  509.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  510.         cout<< "Enter month (e.g : January = 1) : ";
  511.         cin >> month;
  512.       }else{
  513.         correct = 0;
  514.       }
  515.     }
  516.   }while(!checkMonth(month, year));
  517.  
  518.   do{
  519.     correct = 1;
  520.     cout<<endl<<"Enter day : ";
  521.     cin>>day;
  522.     while (correct == 1){
  523.       if (cin.fail()){
  524.         cin.clear();
  525.         cin.ignore();
  526.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  527.         cout<< "Enter day : ";
  528.         cin >> day;
  529.       }else{
  530.         correct = 0;
  531.       }
  532.     }
  533.   }while(!checkDay(day, month, year));
  534.  
  535.   do{
  536.     correct = 1;
  537.     cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  538.     cin>>time;
  539.     while (correct == 1){
  540.       if (cin.fail()){
  541.         cin.clear();
  542.         cin.ignore();
  543.         cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  544.         cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  545.         cin >> time;
  546.       }else{
  547.         correct = 0;
  548.       }
  549.     }
  550.   }while(!checkTime(time, day, month, year));
  551.  
  552.   bookingNo++;
  553.   cout<<endl<<"Booking Number : "<<bookingNo<<endl;
  554.  
  555.   date = year*10000 + month*100 + day;
  556.  
  557.   RBList.InsNewNodeByBookNo(bookingNo, name, phoneNo, paxNo, day, month, year, time, date);
  558.   total++;
  559.   cin.clear();
  560.   cin.ignore(1000, '\n');
  561.   cout << "Press enter to continue . . .";
  562.   getchar();
  563.   system("CLS");
  564.   main();
  565. }
  566.  
  567. //OPTION 2 : Update Reservation Records
  568. void updateRecord(){
  569.   string name;
  570.   int bNo, phoneNo, paxNo, day, month, year, time, date;
  571.   int correct = 1;
  572.   cout << "----------------=UPDATE RECORD=-----------------" << endl;
  573.   cout<<"Enter 0 to return to main menu."<<endl;
  574.   cout << "Enter Booking Number : ";
  575.   cin >> bNo;
  576.   if(bNo == 0){
  577.     cout << "Press enter to continue . . .";
  578.     getchar();
  579.     system("CLS");
  580.     main();
  581.   }
  582.  
  583.   if(RBList.checker(bNo)){
  584.     RBList.deleteNode(bNo);
  585.     cin.clear();
  586.     cin.ignore(1000, '\n');
  587.  
  588.     cout<<endl;
  589.     cout<<"Enter name : ";
  590.     getline(cin, name);
  591.  
  592.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  593.     cin>>phoneNo;
  594.  
  595.     correct = 1;
  596.     while (correct == 1){
  597.       if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  598.         cin.clear();
  599.         cin.ignore();
  600.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  601.         cout<< "Enter phone number (e.g : 0123456789) : ";
  602.         cin >> phoneNo;
  603.       }else{
  604.         correct = 0;
  605.       }
  606.     }
  607.  
  608.     correct = 1;
  609.     cout<<endl<<"Enter number of pax (maximum : 50) : ";
  610.     cin>>paxNo;
  611.  
  612.     while (correct == 1){
  613.       if (cin.fail() || paxNo<=0 || paxNo > 50){
  614.         cin.clear();
  615.         cin.ignore();
  616.         cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  617.         cout<< "Enter number of pax (maximum : 50) : ";
  618.         cin >> paxNo;
  619.       }else{
  620.         correct = 0;
  621.       }
  622.     }
  623.  
  624.     do{
  625.       correct = 1;
  626.       cout<<endl<<"Enter year (e.g : 2019): ";
  627.       cin>>year;
  628.       while (correct == 1){
  629.         if (cin.fail() || year > 2021){
  630.           cin.clear();
  631.           cin.ignore();
  632.           cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  633.           cout<< "Enter year (e.g : 2019) : ";
  634.           cin >> year;
  635.           //reset if a year before 2019 was entered then 2022 or later is entered
  636.         }else{
  637.           correct = 0;
  638.         }
  639.       }
  640.     }while(!checkYear(year));
  641.  
  642.     do{
  643.       correct = 1;
  644.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  645.       cin>>month;
  646.       while (correct == 1){
  647.         if (cin.fail()){
  648.           cin.clear();
  649.           cin.ignore();
  650.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  651.           cout<< "Enter month (e.g : January = 1) : ";
  652.           cin >> month;
  653.         }else{
  654.           correct = 0;
  655.         }
  656.       }
  657.     }while(!checkMonth(month, year));
  658.  
  659.     do{
  660.       correct = 1;
  661.       cout<<endl<<"Enter day : ";
  662.       cin>>day;
  663.       while (correct == 1){
  664.         if (cin.fail()){
  665.           cin.clear();
  666.           cin.ignore();
  667.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  668.           cout<< "Enter day : ";
  669.           cin >> day;
  670.         }else{
  671.           correct = 0;
  672.         }
  673.       }
  674.     }while(!checkDay(day, month, year));
  675.  
  676.     do{
  677.       correct = 1;
  678.       cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  679.       cin>>time;
  680.       while (correct == 1){
  681.         if (cin.fail()){
  682.           cin.clear();
  683.           cin.ignore();
  684.           cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  685.           cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  686.           cin >> time;
  687.         }else{
  688.           correct = 0;
  689.         }
  690.       }
  691.     }while(!checkTime(time, day, month, year));
  692.  
  693.     //for finding the reservation by date
  694.     date = year*10000 + month*100 + day;
  695.  
  696.     RBList.InsNewNodeByBookNo(bookingNo, name, phoneNo, paxNo, day, month, year, time, date);
  697.     cout<<"Update process complete."<<endl;
  698.   }else{
  699.     cout<<"No existing record with that booking number."<<endl
  700.     <<"Update process cancelled."<<endl;
  701.   }
  702.  
  703.   cin.clear();
  704.   cin.ignore(1000, '\n');
  705.   cout << "Press enter to continue . . .";
  706.   getchar();
  707.   system("CLS");
  708.   main();
  709. }
  710.  
  711. //OPTION 3 : Delete Reservation Records
  712. void deleteRecord(){
  713.   int  bNo;
  714.   char confirm;
  715.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  716.   cout<<"Enter 0 to return to main menu."<<endl;
  717.   cout<<"Enter Booking Number : ";
  718.   cin>>bNo;
  719.   if(bNo == 0){
  720.     cout << "Press enter to continue . . .";
  721.     getchar();
  722.     system("CLS");
  723.     main();
  724.   }
  725.   if(RBList.checker(bNo)){
  726.     RBList.PrintBookingNoRecords(bNo);
  727.     cout<<"Confirm?(y/n) : ";
  728.     cin>>confirm;
  729.  
  730.     switch (confirm) {
  731.       case 'y':
  732.         RBList.deleteNode(bNo);
  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.   }
  898.  
  899.   if(counter == 0 || !found){
  900.     cout<<"Invalid Input! Need a name!"<<endl;
  901.     return false;
  902.   }else{
  903.     return true;
  904.   }
  905. }
  906.  
  907. int checkNoOfDigits(int phoneNo){
  908.   if (phoneNo == 0){
  909.     return 0;
  910.   }else{
  911.     return 1 + checkNoOfDigits(phoneNo / 10);
  912.   }
  913. }
  914.  
  915. //for getting the current time
  916. time_t now = time(0);
  917. tm *ltm = localtime(&now);
  918.  
  919. int currentYear = 1900+ltm->tm_year;
  920. int currentMonth = 1 + ltm->tm_mon;
  921. int currentDay = ltm->tm_mday;
  922. int currentHour = ltm->tm_hour;
  923. int currentMinute = ltm->tm_min;
  924.  
  925. bool checkYear(int year){
  926.   if(year < currentYear){
  927.     cout<<"Can't go back in time!"<<endl;
  928.     return false;
  929.   }else{
  930.     return true;
  931.   }
  932. }
  933.  
  934. bool checkMonth(int month, int year){
  935.   if(month < currentMonth && year == currentYear){
  936.     cout<<"Can't go back in time!"<<endl;
  937.     return false;
  938.   }else if(month > 12){
  939.     cout<<"There are only 12 months in a year!"<<endl;
  940.     return false;
  941.   }else{
  942.     return true;
  943.   }
  944. }
  945.  
  946. bool checkDay(int day, int month, int year){
  947.   if(day < currentDay && month == currentMonth && year == currentYear){
  948.     cout<<"Can't go back in time!"<<endl;
  949.   //even months
  950.   }else if(day <= 0){
  951.     cout<<"Can't go back in time!"<<endl;
  952.   }else if(month%2 == 0){
  953.     // if the month is february
  954.     if(month == 2){
  955.       //february in common year that contain 28 days
  956.       if(year%4 != 0 && day>28){
  957.         cout << "Please key in between the day from 1 to 28"<< endl;
  958.       }
  959.       // february in leap year that contain 29 days
  960.       else if(year%4 == 0 && day>29){
  961.         cout << "Please key in between the day from 1 to 29"<< endl;
  962.       }
  963.       else{
  964.         return true;
  965.       }
  966.     }
  967.     // month that are even with 31 days
  968.     else if(month > 7 && day > 31){
  969.       cout << "Please key in between the day from 1 to 31"<< endl;
  970.     }
  971.     // month that are even  with 30 days
  972.     else if(month < 7 && day > 30){
  973.       cout << "Please key in between the day from 1 to 30"<< endl;
  974.     }
  975.     else{
  976.       return true;
  977.     }
  978.  
  979.   // month is odd
  980.   }else if(month%2 != 0){
  981.     // month that are odd with 30 days
  982.     if(month>8 && day > 30){
  983.       cout << "Please key in between the day from 1 to 30"<< endl;
  984.     // month that are odd with 31 days
  985.     }else if(month<8 && day > 31){
  986.       cout << "Please key in between the day from 1 to 31"<< endl;
  987.     }else{
  988.       return true;
  989.     }
  990.   }
  991.   else{
  992.     return true;
  993.   }
  994.   return false;
  995. }
  996.  
  997. bool checkTime(int time, int day, int month, int year){
  998.   int hour = time/100;
  999.   int minute = time - hour*100;
  1000.  
  1001.   if(hour > 24){
  1002.     cout<<"Only 0 to 24 hours."<<endl;
  1003.     return false;
  1004.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  1005.     cout<<"Can't go back in time!"<<endl;
  1006.     return false;
  1007.   }else if(minute > 59){
  1008.     cout<<"Only 0 to 59 minutes."<<endl;
  1009.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  1010.     cout<<"Can't go back in time!"<<endl;
  1011.   }else{
  1012.     return true;
  1013.   }
  1014.   return false;
  1015. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement