Advertisement
Guest User

intMain(5)

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