Advertisement
tiffprag

int main file(5)

Oct 25th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.68 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(bNo, 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.         total--;
  401.         cout<<"Deletion process complete."<<endl;
  402.         break;
  403.       default:
  404.         cout<<"Deletion process cancelled."<<endl;
  405.         main();
  406.     }
  407.   }else{
  408.     cout<<"No existing record with that booking number."<<endl;
  409.   }
  410.  
  411.   cin.clear();
  412.   cin.ignore(1000, '\n');
  413.   cout << "Press enter to continue . . .";
  414.   getchar();
  415.   system("CLS");
  416.   main();
  417. }
  418.  
  419. //OPTION 4 : View Reservation Records
  420. void viewRecord(){
  421.   char choice;
  422.   string name;
  423.   int bNo, year, month, day, date, correct;
  424.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  425.   cout << "Please enter your selection" << endl <<
  426.   "1. View All Reservations(Sorted by Booking Number)" << endl <<
  427.   "2. Search by Name" << endl<<
  428.   "3. Search by Date"<< endl <<
  429.   "4. Back to Main Menu"<< endl;
  430.   cout << "Choice: ";
  431.   cin >> choice;
  432.   cin.clear();
  433.   cin.ignore(1, '\n');
  434.   system("CLS");
  435.  
  436.   switch (choice) {
  437.   //View All Reservations
  438.   case '1':
  439.     RBList.PrintList();
  440.     break;
  441.  
  442.   //Search by Name
  443.   case '2':
  444.     do{
  445.       cout<<"Enter name : ";
  446.       getline(cin, name);
  447.     }while(!checkName(name));
  448.  
  449.     RBList.PrintNameRecords(name);
  450.     break;
  451.  
  452.   case '3':
  453.  
  454.     cout<<endl<<"Enter year (e.g : 2019): ";
  455.     cin>>year;
  456.     while (correct == 1){
  457.       if (cin.fail() || year > 2021){
  458.         cin.clear();
  459.         cin.ignore();
  460.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  461.         cout<< "Enter year (e.g : 2019) : ";
  462.         cin >> year;
  463.       }else{
  464.           correct = 0;
  465.       }
  466.     }
  467.  
  468.     correct = 1;
  469.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  470.     cin>>month;
  471.     while (correct == 1){
  472.       if (cin.fail()){
  473.         cin.clear();
  474.         cin.ignore();
  475.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  476.         cout<< "Enter month (e.g : January = 1) : ";
  477.         cin >> month;
  478.       }else{
  479.           correct = 0;
  480.       }
  481.     }
  482.  
  483.     cout<<endl<<"Enter day : ";
  484.     cin>>day;
  485.     while (correct == 1){
  486.       if (cin.fail()){
  487.         cin.clear();
  488.         cin.ignore();
  489.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  490.         cout<< "Enter day : ";
  491.         cin >> day;
  492.       }else{
  493.           correct = 0;
  494.       }
  495.     }
  496.  
  497.     date = year*10000 + month*100 + day;
  498.  
  499.     RBList.PrintDateRecords(date);
  500.     cin.clear();
  501.     cin.ignore(1000, '\n');
  502.     break;
  503.  
  504.   case '4':
  505.     main();
  506.     break;
  507.  
  508.   default:
  509.     cout << choice << " is not valid choice" << endl;
  510.     viewRecord();
  511.   }
  512.  
  513.   cout << "Press enter to continue . . .";
  514.   cin.clear();
  515.   cin.ignore(1000, '\n');
  516.   system("CLS");
  517.   main();
  518. }
  519.  
  520. //OPTION 5 : Create Text File
  521. void createTextFile(){
  522.   char choice;
  523.   cout << "------------------=Create Txt File=------------------" << endl;
  524.   cout << "Please enter your selection" << endl <<
  525.   "1. Create Sort By Booking Number." << endl <<
  526.   "2. Back to Main Menu"<< endl;
  527.   cout << "Choice: ";
  528.   cin >> choice;
  529.  
  530.   switch (choice) {
  531.   //View All Reservations
  532.   case '1':
  533.     RBList.PrintListTextFile();
  534.     break;
  535.  
  536.   case '2':
  537.     main();
  538.     break;
  539.  
  540.   default:
  541.     cout << choice << " is not valid choice" << endl;
  542.     viewRecord();
  543.   }
  544.  
  545.   getchar();
  546.   cout << "-----------------------------------------------------" << endl;
  547.  
  548.   cout << "Press enter to continue . . .";
  549.   cin.clear();
  550.   cin.ignore(1000, '\n');
  551.   system("CLS");
  552.   main();
  553. }
  554.  
  555. //for checking whether the name is empty
  556. bool checkName(string name){
  557.   int counter = 0;
  558.   bool found = true;
  559.   if(name.find_first_not_of(" ") != std::string::npos){
  560.     counter++;
  561.   }
  562.  
  563.   if(name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0") != std::string::npos){
  564.     found = false;
  565.   }else{
  566.     found = true;
  567.   }
  568.  
  569.   if(counter == 0 || !found){
  570.     cout<<"Invalid Input! Need a name!"<<endl;
  571.     return false;
  572.   }else{
  573.     return true;
  574.   }
  575. }
  576.  
  577. int checkNoOfDigits(int phoneNo){
  578.   if (phoneNo == 0){
  579.     return 0;
  580.   }else{
  581.     return 1 + checkNoOfDigits(phoneNo / 10);
  582.   }
  583. }
  584.  
  585. //for getting the current time
  586. time_t now = time(0);
  587. tm *ltm = localtime(&now);
  588.  
  589. int currentYear = 1900+ltm->tm_year;
  590. int currentMonth = 1 + ltm->tm_mon;
  591. int currentDay = ltm->tm_mday;
  592. int currentHour = ltm->tm_hour;
  593. int currentMinute = ltm->tm_min;
  594.  
  595. bool checkYear(int year){
  596.   if(year < currentYear){
  597.     cout<<"Can't go back in time!"<<endl;
  598.     return false;
  599.   }else{
  600.     return true;
  601.   }
  602. }
  603.  
  604. bool checkMonth(int month, int year){
  605.   if(month < currentMonth && year == currentYear){
  606.     cout<<"Can't go back in time!"<<endl;
  607.     return false;
  608.   }else if(month > 12){
  609.     cout<<"There are only 12 months in a year!"<<endl;
  610.     return false;
  611.   }else{
  612.     return true;
  613.   }
  614. }
  615.  
  616. bool checkDay(int day, int month, int year){
  617.   if(day < currentDay && month == currentMonth && year == currentYear){
  618.     cout<<"Can't go back in time!"<<endl;
  619.   //even months
  620.   }else if(day <= 0){
  621.     cout<<"Can't go back in time!"<<endl;
  622.   }else if(month%2 == 0){
  623.     // if the month is february
  624.     if(month == 2){
  625.       //february in common year that contain 28 days
  626.       if(year%4 != 0 && day>28){
  627.         cout << "Please key in between the day from 1 to 28"<< endl;
  628.       }
  629.       // february in leap year that contain 29 days
  630.       else if(year%4 == 0 && day>29){
  631.         cout << "Please key in between the day from 1 to 29"<< endl;
  632.       }
  633.       else{
  634.         return true;
  635.       }
  636.     }
  637.     // month that are even with 31 days
  638.     else if(month > 7 && day > 31){
  639.       cout << "Please key in between the day from 1 to 31"<< endl;
  640.     }
  641.     // month that are even  with 30 days
  642.     else if(month < 7 && day > 30){
  643.       cout << "Please key in between the day from 1 to 30"<< endl;
  644.     }
  645.     else{
  646.       return true;
  647.     }
  648.  
  649.   // month is odd
  650.   }else if(month%2 != 0){
  651.     // month that are odd with 30 days
  652.     if(month>8 && day > 30){
  653.       cout << "Please key in between the day from 1 to 30"<< endl;
  654.     // month that are odd with 31 days
  655.     }else if(month<8 && day > 31){
  656.       cout << "Please key in between the day from 1 to 31"<< endl;
  657.     }else{
  658.       return true;
  659.     }
  660.   }
  661.   else{
  662.     return true;
  663.   }
  664.   return false;
  665. }
  666.  
  667. bool checkTime(int time, int day, int month, int year){
  668.   int hour = time/100;
  669.   int minute = time - hour*100;
  670.  
  671.   if(hour > 24){
  672.     cout<<"Only 0 to 24 hours."<<endl;
  673.     return false;
  674.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  675.     cout<<"Can't go back in time!"<<endl;
  676.     return false;
  677.   }else if(minute > 59){
  678.     cout<<"Only 0 to 59 minutes."<<endl;
  679.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  680.     cout<<"Can't go back in time!"<<endl;
  681.   }else{
  682.     return true;
  683.   }
  684.   return false;
  685. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement