Advertisement
Guest User

intMain(6)

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