Advertisement
tiffprag

int main file(4)

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