Advertisement
Guest User

intMain(5)

a guest
Oct 23rd, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.16 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.  
  453.   do{
  454.     cout<<"Enter name : ";
  455.     getline(cin, name);
  456.   }while(!checkName(name));
  457.  
  458.   cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  459.   cin>>phoneNo;
  460.  
  461.   correct = 1;
  462.   while (correct == 1){
  463.     length = to_string(phoneNo).length();
  464.     if (cin.fail() || phoneNo<0 || length>11){
  465.       cin.clear();
  466.       cin.ignore();
  467.       cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  468.       cout<< "Enter phone number (e.g : 0123456789) : ";
  469.       cin >> phoneNo;
  470.     }else{
  471.         correct = 0;
  472.     }
  473.   }
  474.  
  475.   correct = 1;
  476.   cout<<endl<<"Enter number of pax (maximum : 50) : ";
  477.   cin>>paxNo;
  478.  
  479.   while (correct == 1){
  480.     if (cin.fail() || paxNo<0 || paxNo > 50){
  481.       cin.clear();
  482.       cin.ignore();
  483.       cout << "Invalid input! Enter positive numbers (maximum : 50)!" << endl<<endl;
  484.       cout<< "Enter number of pax (maximum : 50) : ";
  485.       cin >> paxNo;
  486.     }else{
  487.         correct = 0;
  488.     }
  489.   }
  490.  
  491.   correct = 1;
  492.   do{
  493.     cout<<endl<<"Enter year (e.g : 2019): ";
  494.     cin>>year;
  495.     while (correct == 1){
  496.       if (cin.fail() || year > 2021){
  497.         cin.clear();
  498.         cin.ignore();
  499.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  500.         cout<< "Enter year (e.g : 2019) : ";
  501.         cin >> year;
  502.       }else{
  503.           correct = 0;
  504.       }
  505.     }
  506.   }while(!checkYear(year));
  507.  
  508.   correct = 1;
  509.   do{
  510.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  511.     cin>>month;
  512.     while (correct == 1){
  513.       if (cin.fail()){
  514.         cin.clear();
  515.         cin.ignore();
  516.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  517.         cout<< "Enter month (e.g : January = 1) : ";
  518.         cin >> month;
  519.       }else{
  520.           correct = 0;
  521.       }
  522.     }
  523.   }while(!checkMonth(month, year));
  524.  
  525.   correct = 1;
  526.   do{
  527.     cout<<endl<<"Enter day : ";
  528.     cin>>day;
  529.     while (correct == 1){
  530.       if (cin.fail()){
  531.         cin.clear();
  532.         cin.ignore();
  533.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  534.         cout<< "Enter day : ";
  535.         cin >> day;
  536.       }else{
  537.           correct = 0;
  538.       }
  539.     }
  540.   }while(!checkDay(day, month, year));
  541.  
  542.   do{
  543.     cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  544.     cin>>time;
  545.     while (correct == 1){
  546.       if (cin.fail()){
  547.         cin.clear();
  548.         cin.ignore();
  549.         cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  550.         cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  551.         cin >> time;
  552.       }else{
  553.           correct = 0;
  554.       }
  555.     }
  556.   }while(!checkTime(time, day, month, year));
  557.  
  558.   bookingNo++;
  559.   cout<<endl<<"Booking Number : "<<bookingNo<<endl;
  560.  
  561.   //for inserting the node in order according to date and time
  562.   dateAndTime = year*100000000 + month*1000000 + day*10000 + time;
  563.  
  564.   RList.InsNewNode(bookingNo, name, phoneNo, paxNo, day, month, year, time, dateAndTime);
  565.   total++;
  566.   cin.clear();
  567.   cin.ignore(1000, '\n');
  568.   cout << "Press enter to continue . . .";
  569.   getchar();
  570.   system("CLS");
  571.   main();
  572. }
  573.  
  574. //OPTION 2 : Update Reservation Records
  575. void updateRecord(){
  576.   string name;
  577.   int bNo, phoneNo, paxNo, day, month, year, time;
  578.   long dateAndTime;
  579.   int length;
  580.   int correct = 1;
  581.   cout << "----------------=UPDATE RECORD=-----------------" << endl;
  582.   cout<<"Enter 0 to return to main menu."<<endl;
  583.   cout << "Enter Booking Number : ";
  584.   cin >> bNo;
  585.   if(bNo == 0){
  586.     main();
  587.   }
  588.  
  589.   if(RList.checker(bNo)){
  590.     cin.clear();
  591.     cin.ignore(1000, '\n');
  592.     cout<<"Enter name : ";
  593.     getline(cin, name);
  594.  
  595.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  596.     cin>>phoneNo;
  597.  
  598.     correct = 1;
  599.     while (correct == 1){
  600.       length = to_string(phoneNo).length();
  601.       if (cin.fail() || phoneNo<0 || length>11){
  602.         cin.clear();
  603.         cin.ignore();
  604.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  605.         cout<< "Enter phone number (e.g : 0123456789) : ";
  606.         cin >> phoneNo;
  607.       }else{
  608.           correct = 0;
  609.       }
  610.     }
  611.  
  612.     correct = 1;
  613.     cout<<endl<<"Enter number of pax (maximum : 50) : ";
  614.     cin>>paxNo;
  615.  
  616.     while (correct == 1){
  617.       if (cin.fail() || paxNo<0 || paxNo > 50){
  618.         cin.clear();
  619.         cin.ignore();
  620.         cout << "Invalid input! Enter positive numbers (maximum : 50)!" << endl<<endl;
  621.         cout<< "Enter number of pax (maximum : 50) : ";
  622.         cin >> paxNo;
  623.       }else{
  624.           correct = 0;
  625.       }
  626.     }
  627.  
  628.     correct = 1;
  629.     do{
  630.       cout<<endl<<"Enter year (e.g : 2019): ";
  631.       cin>>year;
  632.       while (correct == 1){
  633.         if (cin.fail() || year > 2021){
  634.           cin.clear();
  635.           cin.ignore();
  636.           cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  637.           cout<< "Enter year (e.g : 2019) : ";
  638.           cin >> year;
  639.         }else{
  640.             correct = 0;
  641.         }
  642.       }
  643.     }while(!checkYear(year));
  644.  
  645.     correct = 1;
  646.     do{
  647.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  648.       cin>>month;
  649.       while (correct == 1){
  650.         if (cin.fail()){
  651.           cin.clear();
  652.           cin.ignore();
  653.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  654.           cout<< "Enter month (e.g : January = 1) : ";
  655.           cin >> month;
  656.         }else{
  657.             correct = 0;
  658.         }
  659.       }
  660.     }while(!checkMonth(month, year));
  661.  
  662.     correct = 1;
  663.     do{
  664.       cout<<endl<<"Enter day : ";
  665.       cin>>day;
  666.       while (correct == 1){
  667.         if (cin.fail()){
  668.           cin.clear();
  669.           cin.ignore();
  670.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  671.           cout<< "Enter day : ";
  672.           cin >> day;
  673.         }else{
  674.             correct = 0;
  675.         }
  676.       }
  677.     }while(!checkDay(day, month, year));
  678.  
  679.     do{
  680.       cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  681.       cin>>time;
  682.       while (correct == 1){
  683.         if (cin.fail()){
  684.           cin.clear();
  685.           cin.ignore();
  686.           cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  687.           cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  688.           cin >> time;
  689.         }else{
  690.             correct = 0;
  691.         }
  692.       }
  693.     }while(!checkTime(time, day, month, year));
  694.     //for inserting the node in order
  695.     dateAndTime = year*100000000 + month*1000000 + day*10000 + time;
  696.  
  697.     RList.InsNewNode(bNo, name, phoneNo, paxNo, day, month, year, time, dateAndTime);
  698.     cout<<"Update process complete."<<endl;
  699.   }else{
  700.     cout<<"No existing record with that booking number."<<endl
  701.     <<"Update process cancelled."<<endl;
  702.   }
  703.  
  704.   cin.clear();
  705.   cin.ignore(1000, '\n');
  706.   cout << "Press enter to continue . . .";
  707.   getchar();
  708.   system("CLS");
  709.   main();
  710. }
  711.  
  712. //OPTION 3 : Delete Reservation Records
  713. void deleteRecord(){
  714.   int  bNo;
  715.   char confirm;
  716.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  717.   cout<<"Enter 0 to return to main menu."<<endl;
  718.   cout<<"Enter Booking Number : ";
  719.   cin>>bNo;
  720.   if(bNo == 0){
  721.     main();
  722.   }
  723.   if(RList.checker(bNo)){
  724.     RList.PrintBookingNoRecords(bNo);
  725.     cout<<"Confirm?(y/n) : ";
  726.     cin>>confirm;
  727.  
  728.     switch (confirm) {
  729.       case 'y':
  730.         RList.deleteNode(bNo);
  731.         cout<<"Deletion process complete."<<endl;
  732.         break;
  733.       default:
  734.         cout<<"Deletion process cancelled."<<endl;
  735.         main();
  736.     }
  737.   }else{
  738.     cout<<"No existing record with that booking number."<<endl;
  739.   }
  740.  
  741.   cin.clear();
  742.   cin.ignore(1000, '\n');
  743.   cout << "Press enter to continue . . .";
  744.   getchar();
  745.   system("CLS");
  746.   main();
  747. }
  748.  
  749. //OPTION 4 : View Reservation Records
  750. void viewRecord(){
  751.   char choice;
  752.   string name;
  753.   int bNo, year, month, day, date;
  754.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  755.   cout << "Please enter your selection" << endl <<
  756.   "1. View All Reservations" << endl <<
  757.   "2. Search by Booking Number"<< endl <<
  758.   "3. Search by Name" << endl<<
  759.   "4. Search by Date"<< endl <<
  760.   "5. Back to Main Menu"<< endl;
  761.   cout << "Choice: ";
  762.   cin >> choice;
  763.   cin.clear();
  764.   cin.ignore(1, '\n');
  765.   system("CLS");
  766.  
  767.   switch (choice) {
  768.   //View All Reservations
  769.   case '1':
  770.     RList.PrintList();
  771.     break;
  772.  
  773.   case '2':
  774.     cout<<"Enter Booking Number : ";
  775.     cin>>bNo;
  776.     RList.PrintBookingNoRecords(bNo);
  777.     break;
  778.  
  779.   //Search by Name
  780.   case '3':
  781.     cout<<"Enter Name : ";
  782.     getline(cin, name);
  783.  
  784.     RList.PrintNameRecords(name);
  785.     break;
  786.  
  787.   case '4':
  788.     cout<<"Enter year (e.g : 2019) : ";
  789.     cin>>year;
  790.  
  791.     cout<<"Enter month (e.g : January = 1) : ";
  792.     cin>>month;
  793.  
  794.     cout<<"Enter day : ";
  795.     cin>>day;
  796.  
  797.     date = year*10000 + month*100 + day;
  798.  
  799.     RList.PrintDateRecords(date);
  800.     break;
  801.  
  802.   case '5':
  803.     main();
  804.     break;
  805.  
  806.   default:
  807.     cout << choice << " is not valid choice" << endl;
  808.     viewRecord();
  809.   }
  810.  
  811.   cout << "Press enter to continue . . .";
  812.   getchar();
  813.   system("CLS");
  814.   main();
  815. }
  816.  
  817. //OPTION 5 : Create Text File
  818. void createTextFile(){
  819.   RList.PrintListTextFile();
  820.   getchar();
  821.   cout << "-----------------------------------------------------" << endl;
  822.  
  823.   cout << "Press enter to continue . . .";
  824.   cin.clear();
  825.   cin.ignore(1000, '\n');
  826.   system("CLS");
  827.   main();
  828. }
  829.  
  830. //for checking whether the name is empty
  831. bool checkName(string name){
  832.   int counter = 0;
  833.   for(int i = 0; i<name.length() ; i++){
  834.     if(name.find_first_not_of(' ') != std::string::npos){
  835.       counter++;
  836.     }
  837.   }
  838.  
  839.   if(counter == 0){
  840.     cout<<"Invalid Input! Need a name!"<<endl;
  841.     return false;
  842.   }else{
  843.     return true;
  844.   }
  845. }
  846.  
  847. //for getting the current time
  848. time_t now = time(0);
  849. tm *ltm = localtime(&now);
  850.  
  851. int currentYear = 1900+ltm->tm_year;
  852. int currentMonth = 1 + ltm->tm_mon;
  853. int currentDay = ltm->tm_mday;
  854. int currentHour = ltm->tm_hour;
  855. int currentMinute = ltm->tm_min;
  856.  
  857. bool checkYear(int year){
  858.   if(year < currentYear){
  859.     cout<<"Can't go back in time!"<<endl;
  860.     return false;
  861.   }else{
  862.     return true;
  863.   }
  864. }
  865.  
  866. bool checkMonth(int month, int year){
  867.   if(month < currentMonth && year == currentYear){
  868.     cout<<"Can't go back in time!"<<endl;
  869.     return false;
  870.   }else if(month > 12){
  871.     cout<<"There are only 12 months in a year!"<<endl;
  872.     return false;
  873.   }else{
  874.     return true;
  875.   }
  876. }
  877.  
  878. bool checkDay(int day, int month, int year){
  879.   if(day < currentDay && month == currentMonth && year == currentYear){
  880.     cout<<"Can't go back in time!"<<endl;
  881.   //even months
  882.   }else if(day <= 0){
  883.     cout<<"Can't go back in time!"<<endl;
  884.   }else if(month%2 == 0){
  885.     // if the month is february
  886.     if(month == 2){
  887.       //february in common year that contain 28 days
  888.       if(year%4 != 0 && day>28){
  889.         cout << "Please key in between the day from 1 to 28"<< endl;
  890.       }
  891.       // february in leap year that contain 29 days
  892.       else if(year%4 == 0 && day>29){
  893.         cout << "Please key in between the day from 1 to 29"<< endl;
  894.       }
  895.       else{
  896.         return true;
  897.       }
  898.     }
  899.     // month that are even with 31 days
  900.     else if(month > 7 && day > 31){
  901.       cout << "Please key in between the day from 1 to 31"<< endl;
  902.     }
  903.     // month that are even  with 30 days
  904.     else if(month < 7 && day > 30){
  905.       cout << "Please key in between the day from 1 to 30"<< endl;
  906.     }
  907.     else{
  908.       return true;
  909.     }
  910.  
  911.   // month is odd
  912.   }else if(month%2 != 0){
  913.     // month that are odd with 30 days
  914.     if(month>8 && day > 30){
  915.       cout << "Please key in between the day from 1 to 30"<< endl;
  916.     // month that are odd with 31 days
  917.     }else if(month<8 && day > 31){
  918.       cout << "Please key in between the day from 1 to 31"<< endl;
  919.     }else{
  920.       return true;
  921.     }
  922.   }
  923.   else{
  924.     return true;
  925.   }
  926.   return false;
  927. }
  928.  
  929. bool checkTime(int time, int day, int month, int year){
  930.   int hour = time/100;
  931.   int minute = time - hour*100;
  932.  
  933.   if(hour > 24){
  934.     cout<<"Only 0 to 24 hours."<<endl;
  935.     return false;
  936.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  937.     cout<<"Can't go back in time!"<<endl;
  938.     return false;
  939.   }else{
  940.     return true;
  941.   }
  942.  
  943.   if(minute > 59){
  944.     cout<<"Only 0 to 59 minutes."<<endl;
  945.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  946.     cout<<"Can't go back in time!"<<endl;
  947.   }else{
  948.     return true;
  949.   }
  950.   return false;
  951. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement