Advertisement
Guest User

intMain(9.1)

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