tiffprag

int main file(6)

Oct 25th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <stdlib.h> //for exit() function
  5. #include <fstream>  //for creating text file
  6. using namespace std;
  7.  
  8. int total = 0;
  9. int bookNum = 0;
  10. //linklist of reservations sorted by booking number
  11. linklist RBList;
  12. void addRecord();
  13. void updateRecord();
  14. void deleteRecord();
  15. void viewRecord();
  16. void createTextFile();
  17. bool checkName(string name);
  18. int checkNoOfDigits(int phoneNo);
  19. bool checkYear(int year);
  20. bool checkMonth(int month, int year);
  21. bool checkDay(int day, int month, int year);
  22. bool checkTime(int time, int day, int month, int year);
  23.  
  24. int main() {
  25.   char choice;
  26.   cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
  27.   cout << "Please enter your selection" << endl <<
  28.     "1. Add Reservation Records" << endl <<
  29.     "2. Update Reservation Records" << endl <<
  30.     "3. Delete Reservation Records" << endl <<
  31.     "4. View Reservation Records  " << endl <<
  32.     "5. Create text file" << endl <<
  33.     "6. Exit" << endl;
  34.  
  35.   cout << "Choice: ";
  36.   cin >> choice;
  37.   cin.clear();
  38.   cin.ignore(1, '\n');
  39.   system("CLS");
  40.  
  41.   switch (choice) {
  42.   //Add Records
  43.   case '1':
  44.     addRecord();
  45.     break;
  46.  
  47.   //Update Records
  48.   case '2':
  49.     if (total == 0) {
  50.       cout << "No reservation records available." << endl;
  51.       main();
  52.     }
  53.     updateRecord();
  54.  
  55.     break;
  56.  
  57.   //Delete Records
  58.   case '3':
  59.     if (total == 0) {
  60.       cout << "No reservation records available." << endl;
  61.       main();
  62.     }
  63.     deleteRecord();
  64.  
  65.     break;
  66.  
  67.   //View Reservation Records
  68.   case '4':
  69.     if (total == 0) {
  70.       cout << "No reservation records available." << endl;
  71.       main();
  72.     }
  73.     viewRecord();
  74.     break;
  75.  
  76.   //Create Text File
  77.   case '5':
  78.     if (total == 0) {
  79.       cout << "No reservation records available." << endl;
  80.       main();
  81.     }
  82.     createTextFile();
  83.     break;
  84.  
  85.   case '6':
  86.     exit(1);
  87.     break;
  88.  
  89.   default:
  90.     cout << choice << " is not valid choice" << endl;
  91.     main();
  92.   }
  93.  
  94.   return 0;
  95. }
  96.  
  97. //OPTION 1 : Add Reservation Records
  98. void addRecord(){
  99.   string name;
  100.   int phoneNo, paxNo, day, month, year, time, date;
  101.   int correct = 1;
  102.   cout << "-----------------=ADD RECORD=-------------------" << endl;
  103.   cout<<"Enter 0 to return to main menu."<<endl;
  104.   do{
  105.     cout<<"Enter name : ";
  106.     getline(cin, name);
  107.   }while(!checkName(name));
  108.  
  109.   if(name=="0"){
  110.     cout << "Press enter to continue . . .";
  111.     getchar();
  112.     system("CLS");
  113.     main();
  114.   }
  115.  
  116.   cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  117.   cin>>phoneNo;
  118.  
  119.   correct = 1;
  120.   while (correct == 1){
  121.     if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  122.       cin.clear();
  123.       cin.ignore();
  124.       cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  125.       cout<< "Enter phone number (e.g : 0123456789) : ";
  126.       cin >> phoneNo;
  127.     }else{
  128.       correct = 0;
  129.     }
  130.   }
  131.  
  132.   correct = 1;
  133.   cout<<endl<<"Enter number of pax (maximum : 50) : ";
  134.   cin>>paxNo;
  135.  
  136.   while (correct == 1){
  137.     if (cin.fail() || paxNo<=0 || paxNo > 50){
  138.       cin.clear();
  139.       cin.ignore();
  140.       cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  141.       cout<< "Enter number of pax (maximum : 50) : ";
  142.       cin >> paxNo;
  143.     }else{
  144.       correct = 0;
  145.     }
  146.   }
  147.  
  148.  
  149.   do{
  150.     correct = 1;
  151.     cout<<endl<<"Enter year (e.g : 2019): ";
  152.     cin>>year;
  153.     while (correct == 1){
  154.       if (cin.fail() || year > 2021){
  155.         cin.clear();
  156.         cin.ignore();
  157.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  158.         cout<< "Enter year (e.g : 2019) : ";
  159.         cin >> year;
  160.         //reset if a year before 2019 was entered then 2022 or later is entered
  161.       }else{
  162.         correct = 0;
  163.       }
  164.     }
  165.   }while(!checkYear(year));
  166.  
  167.   do{
  168.     correct = 1;
  169.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  170.     cin>>month;
  171.     while (correct == 1){
  172.       if (cin.fail()){
  173.         cin.clear();
  174.         cin.ignore();
  175.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  176.         cout<< "Enter month (e.g : January = 1) : ";
  177.         cin >> month;
  178.       }else{
  179.         correct = 0;
  180.       }
  181.     }
  182.   }while(!checkMonth(month, year));
  183.  
  184.   do{
  185.     correct = 1;
  186.     cout<<endl<<"Enter day : ";
  187.     cin>>day;
  188.     while (correct == 1){
  189.       if (cin.fail()){
  190.         cin.clear();
  191.         cin.ignore();
  192.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  193.         cout<< "Enter day : ";
  194.         cin >> day;
  195.       }else{
  196.         correct = 0;
  197.       }
  198.     }
  199.   }while(!checkDay(day, month, year));
  200.  
  201.   do{
  202.     correct = 1;
  203.     cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  204.     cin>>time;
  205.     while (correct == 1){
  206.       if (cin.fail()){
  207.         cin.clear();
  208.         cin.ignore();
  209.         cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  210.         cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  211.         cin >> time;
  212.       }else{
  213.         correct = 0;
  214.       }
  215.     }
  216.   }while(!checkTime(time, day, month, year));
  217.  
  218.   bookNum++;
  219.   cout<<endl<<"Booking Number : "<<bookNum<<endl;
  220.  
  221.   date = year*10000 + month*100 + day;
  222.  
  223.   RBList.InsNewNodeByBookNo(bookNum, name, phoneNo, paxNo, day, month, year, time, date);
  224.   total++;
  225.   cin.clear();
  226.   cin.ignore(1000, '\n');
  227.   cout << "Press enter to continue . . .";
  228.   getchar();
  229.   system("CLS");
  230.   main();
  231. }
  232.  
  233. //OPTION 2 : Update Reservation Records
  234. void updateRecord(){
  235.   string name;
  236.   int bNo, phoneNo, paxNo, day, month, year, time, date;
  237.   int correct = 1;
  238.   cout << "----------------=UPDATE RECORD=-----------------" << endl;
  239.   cout<<"Enter 0 to return to main menu."<<endl;
  240.   cout << "Enter Booking Number : ";
  241.   cin >> bNo;
  242.   if(bNo == 0){
  243.     cout << "Press enter to continue . . .";
  244.     getchar();
  245.     system("CLS");
  246.     main();
  247.   }
  248.  
  249.   if(RBList.checker(bNo)){
  250.     RBList.deleteNode(bNo);
  251.     cin.clear();
  252.     cin.ignore(1000, '\n');
  253.  
  254.     cout<<endl;
  255.     cout<<"Enter name : ";
  256.     getline(cin, name);
  257.  
  258.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  259.     cin>>phoneNo;
  260.  
  261.     correct = 1;
  262.     while (correct == 1){
  263.       if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>11){
  264.         cin.clear();
  265.         cin.ignore();
  266.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  267.         cout<< "Enter phone number (e.g : 0123456789) : ";
  268.         cin >> phoneNo;
  269.       }else{
  270.         correct = 0;
  271.       }
  272.     }
  273.  
  274.     correct = 1;
  275.     cout<<endl<<"Enter number of pax (maximum : 50) : ";
  276.     cin>>paxNo;
  277.  
  278.     while (correct == 1){
  279.       if (cin.fail() || paxNo<=0 || paxNo > 50){
  280.         cin.clear();
  281.         cin.ignore();
  282.         cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  283.         cout<< "Enter number of pax (maximum : 50) : ";
  284.         cin >> paxNo;
  285.       }else{
  286.         correct = 0;
  287.       }
  288.     }
  289.  
  290.     do{
  291.       correct = 1;
  292.       cout<<endl<<"Enter year (e.g : 2019): ";
  293.       cin>>year;
  294.       while (correct == 1){
  295.         if (cin.fail() || year > 2021){
  296.           cin.clear();
  297.           cin.ignore();
  298.           cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  299.           cout<< "Enter year (e.g : 2019) : ";
  300.           cin >> year;
  301.           //reset if a year before 2019 was entered then 2022 or later is entered
  302.         }else{
  303.           correct = 0;
  304.         }
  305.       }
  306.     }while(!checkYear(year));
  307.  
  308.     do{
  309.       correct = 1;
  310.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  311.       cin>>month;
  312.       while (correct == 1){
  313.         if (cin.fail()){
  314.           cin.clear();
  315.           cin.ignore();
  316.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  317.           cout<< "Enter month (e.g : January = 1) : ";
  318.           cin >> month;
  319.         }else{
  320.           correct = 0;
  321.         }
  322.       }
  323.     }while(!checkMonth(month, year));
  324.  
  325.     do{
  326.       correct = 1;
  327.       cout<<endl<<"Enter day : ";
  328.       cin>>day;
  329.       while (correct == 1){
  330.         if (cin.fail()){
  331.           cin.clear();
  332.           cin.ignore();
  333.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  334.           cout<< "Enter day : ";
  335.           cin >> day;
  336.         }else{
  337.           correct = 0;
  338.         }
  339.       }
  340.     }while(!checkDay(day, month, year));
  341.  
  342.     do{
  343.       correct = 1;
  344.       cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  345.       cin>>time;
  346.       while (correct == 1){
  347.         if (cin.fail()){
  348.           cin.clear();
  349.           cin.ignore();
  350.           cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  351.           cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  352.           cin >> time;
  353.         }else{
  354.           correct = 0;
  355.         }
  356.       }
  357.     }while(!checkTime(time, day, month, year));
  358.  
  359.     //for finding the reservation by date
  360.     date = year*10000 + month*100 + day;
  361.  
  362.     RBList.InsNewNodeByBookNo(bNo, name, phoneNo, paxNo, day, month, year, time, date);
  363.     cout<<"Update process complete."<<endl;
  364.   }else{
  365.     cout<<"No existing record with that booking number."<<endl
  366.     <<"Update process cancelled."<<endl;
  367.   }
  368.  
  369.   cin.clear();
  370.   cin.ignore(1000, '\n');
  371.   cout << "Press enter to continue . . .";
  372.   getchar();
  373.   system("CLS");
  374.   main();
  375. }
  376.  
  377. //OPTION 3 : Delete Reservation Records
  378. void deleteRecord(){
  379.   int  bNo;
  380.   char confirm;
  381.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  382.   cout<<"Enter 0 to return to main menu."<<endl;
  383.   cout<<"Enter Booking Number : ";
  384.   cin>>bNo;
  385.   if(bNo == 0){
  386.     cout << "Press enter to continue . . .";
  387.     getchar();
  388.     system("CLS");
  389.     main();
  390.   }
  391.   if(RBList.checker(bNo)){
  392.     RBList.PrintBookingNoRecords(bNo);
  393.     cout<<"Confirm?(y/n) : ";
  394.     cin>>confirm;
  395.  
  396.     switch (confirm) {
  397.       case 'y':
  398.         RBList.deleteNode(bNo);
  399.         total--;
  400.         cout<<"Deletion process complete."<<endl;
  401.         break;
  402.       default:
  403.         cout<<"Deletion process cancelled."<<endl;
  404.         main();
  405.     }
  406.   }else{
  407.     cout<<"No existing record with that booking number."<<endl;
  408.   }
  409.  
  410.   cin.clear();
  411.   cin.ignore(1000, '\n');
  412.   cout << "Press enter to continue . . .";
  413.   getchar();
  414.   system("CLS");
  415.   main();
  416. }
  417.  
  418. //OPTION 4 : View Reservation Records
  419. void viewRecord(){
  420.   char choice;
  421.   string name;
  422.   int bNo, year, month, day, date, correct;
  423.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  424.   cout << "Please enter your selection" << endl <<
  425.   "1. View All Reservations(Sorted by Booking Number)" << endl <<
  426.   "2. Search by Name" << endl<<
  427.   "3. Search by Date"<< endl <<
  428.   "4. Back to Main Menu"<< endl;
  429.   cout << "Choice: ";
  430.   cin >> choice;
  431.   cin.clear();
  432.   cin.ignore(1, '\n');
  433.   system("CLS");
  434.  
  435.   switch (choice) {
  436.   //View All Reservations
  437.   case '1':
  438.     RBList.PrintList();
  439.     break;
  440.  
  441.   //Search by Name
  442.   case '2':
  443.     do{
  444.       cout<<"Enter name : ";
  445.       getline(cin, name);
  446.     }while(!checkName(name));
  447.  
  448.     RBList.PrintNameRecords(name);
  449.     break;
  450.  
  451.   case '3':
  452.  
  453.     cout<<endl<<"Enter year (e.g : 2019): ";
  454.     cin>>year;
  455.     while (correct == 1){
  456.       if (cin.fail() || year > 2021){
  457.         cin.clear();
  458.         cin.ignore();
  459.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  460.         cout<< "Enter year (e.g : 2019) : ";
  461.         cin >> year;
  462.       }else{
  463.           correct = 0;
  464.       }
  465.     }
  466.  
  467.     correct = 1;
  468.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  469.     cin>>month;
  470.     while (correct == 1){
  471.       if (cin.fail()){
  472.         cin.clear();
  473.         cin.ignore();
  474.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  475.         cout<< "Enter month (e.g : January = 1) : ";
  476.         cin >> month;
  477.       }else{
  478.           correct = 0;
  479.       }
  480.     }
  481.  
  482.     cout<<endl<<"Enter day : ";
  483.     cin>>day;
  484.     while (correct == 1){
  485.       if (cin.fail()){
  486.         cin.clear();
  487.         cin.ignore();
  488.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  489.         cout<< "Enter day : ";
  490.         cin >> day;
  491.       }else{
  492.           correct = 0;
  493.       }
  494.     }
  495.  
  496.     date = year*10000 + month*100 + day;
  497.  
  498.     RBList.PrintDateRecords(date);
  499.     cin.clear();
  500.     cin.ignore(1000, '\n');
  501.     break;
  502.  
  503.   case '4':
  504.     main();
  505.     break;
  506.  
  507.   default:
  508.     cout << choice << " is not valid choice" << endl;
  509.     viewRecord();
  510.   }
  511.  
  512.   cout << "Press enter to continue . . .";
  513.   cin.clear();
  514.   cin.ignore(1000, '\n');
  515.   system("CLS");
  516.   main();
  517. }
  518.  
  519. //OPTION 5 : Create Text File
  520. void createTextFile(){
  521.   char choice;
  522.   cout << "------------------=Create Txt File=------------------" << endl;
  523.   cout << "Please enter your selection" << endl <<
  524.   "1. Create Sort By Booking Number." << endl <<
  525.   "2. Back to Main Menu"<< endl;
  526.   cout << "Choice: ";
  527.   cin >> choice;
  528.  
  529.   switch (choice) {
  530.   //View All Reservations
  531.   case '1':
  532.     RBList.PrintListTextFile();
  533.     break;
  534.  
  535.   case '2':
  536.     main();
  537.     break;
  538.  
  539.   default:
  540.     cout << choice << " is not valid choice" << endl;
  541.     viewRecord();
  542.   }
  543.  
  544.   getchar();
  545.   cout << "-----------------------------------------------------" << endl;
  546.  
  547.   cout << "Press enter to continue . . .";
  548.   cin.clear();
  549.   cin.ignore(1000, '\n');
  550.   system("CLS");
  551.   main();
  552. }
  553.  
  554. //for checking whether the name is empty
  555. bool checkName(string name){
  556.   int counter = 0;
  557.   bool found = true;
  558.   if(name.find_first_not_of(" ") != std::string::npos){
  559.     counter++;
  560.   }
  561.  
  562.   if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0") != std::string::npos){
  563.     found = false;
  564.   }else{
  565.     found = true;
  566.   }
  567.  
  568.   if(counter == 0 || !found){
  569.     cout<<"Invalid Input! Need a name!"<<endl;
  570.     return false;
  571.   }else{
  572.     return true;
  573.   }
  574. }
  575.  
  576. int checkNoOfDigits(int phoneNo){
  577.   if (phoneNo == 0){
  578.     return 0;
  579.   }else{
  580.     return 1 + checkNoOfDigits(phoneNo / 10);
  581.   }
  582. }
  583.  
  584. //for getting the current time
  585. time_t now = time(0);
  586. tm *ltm = localtime(&now);
  587.  
  588. int currentYear = 1900+ltm->tm_year;
  589. int currentMonth = 1 + ltm->tm_mon;
  590. int currentDay = ltm->tm_mday;
  591. int currentHour = ltm->tm_hour;
  592. int currentMinute = ltm->tm_min;
  593.  
  594. bool checkYear(int year){
  595.   if(year < currentYear){
  596.     cout<<"Can't go back in time!"<<endl;
  597.     return false;
  598.   }else{
  599.     return true;
  600.   }
  601. }
  602.  
  603. bool checkMonth(int month, int year){
  604.   if(month < currentMonth && year == currentYear){
  605.     cout<<"Can't go back in time!"<<endl;
  606.     return false;
  607.   }else if(month > 12){
  608.     cout<<"There are only 12 months in a year!"<<endl;
  609.     return false;
  610.   }else{
  611.     return true;
  612.   }
  613. }
  614.  
  615. bool checkDay(int day, int month, int year){
  616.   if(day < currentDay && month == currentMonth && year == currentYear){
  617.     cout<<"Can't go back in time!"<<endl;
  618.   //even months
  619.   }else if(day <= 0){
  620.     cout<<"Can't go back in time!"<<endl;
  621.   }else if(month%2 == 0){
  622.     // if the month is february
  623.     if(month == 2){
  624.       //february in common year that contain 28 days
  625.       if(year%4 != 0 && day>28){
  626.         cout << "Please key in between the day from 1 to 28"<< endl;
  627.       }
  628.       // february in leap year that contain 29 days
  629.       else if(year%4 == 0 && day>29){
  630.         cout << "Please key in between the day from 1 to 29"<< endl;
  631.       }
  632.       else{
  633.         return true;
  634.       }
  635.     }
  636.     // month that are even with 31 days
  637.     else if(month > 7 && day > 31){
  638.       cout << "Please key in between the day from 1 to 31"<< endl;
  639.     }
  640.     // month that are even  with 30 days
  641.     else if(month < 7 && day > 30){
  642.       cout << "Please key in between the day from 1 to 30"<< endl;
  643.     }
  644.     else{
  645.       return true;
  646.     }
  647.  
  648.   // month is odd
  649.   }else if(month%2 != 0){
  650.     // month that are odd with 30 days
  651.     if(month>8 && day > 30){
  652.       cout << "Please key in between the day from 1 to 30"<< endl;
  653.     // month that are odd with 31 days
  654.     }else if(month<8 && day > 31){
  655.       cout << "Please key in between the day from 1 to 31"<< endl;
  656.     }else{
  657.       return true;
  658.     }
  659.   }
  660.   else{
  661.     return true;
  662.   }
  663.   return false;
  664. }
  665.  
  666. bool checkTime(int time, int day, int month, int year){
  667.   int hour = time/100;
  668.   int minute = time - hour*100;
  669.  
  670.   if(hour > 24){
  671.     cout<<"Only 0 to 24 hours."<<endl;
  672.     return false;
  673.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  674.     cout<<"Can't go back in time!"<<endl;
  675.     return false;
  676.   }else if(minute > 59){
  677.     cout<<"Only 0 to 59 minutes."<<endl;
  678.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  679.     cout<<"Can't go back in time!"<<endl;
  680.   }else{
  681.     return true;
  682.   }
  683.   return false;
  684. }
Add Comment
Please, Sign In to add comment