Advertisement
tiffprag

int main file(7)

Oct 25th, 2019
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>  //for creating text file
  4. using namespace std;
  5.  
  6. int total = 0;  //total reservation records available
  7. int bookNum = 0;    //unique number for each reservation
  8.  
  9. //linklist of reservations sorted by booking number
  10. linklist RBList;
  11. void addRecord();
  12. void updateRecord();
  13. void deleteRecord();
  14. void viewRecord();
  15. void createTextFile();
  16. bool checkName(string name);
  17. int checkNoOfDigits(int phoneNo);
  18. bool checkYear(int year);
  19. bool checkMonth(int month, int year);
  20. bool checkDay(int day, int month, int year);
  21. bool checkTime(int time, int day, int month, int year);
  22.  
  23. int main() {
  24.   char choice;
  25.   cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
  26.   cout << "Please enter your selection" << endl <<
  27.     "1. Add Reservation Records" << endl <<
  28.     "2. Update Reservation Records" << endl <<
  29.     "3. Delete Reservation Records" << endl <<
  30.     "4. View Reservation Records  " << endl <<
  31.     "5. Create text file" << endl <<
  32.     "6. Exit" << endl;
  33.  
  34.   cout << "Choice: ";
  35.   cin >> choice;
  36.   cin.clear();
  37.   cin.ignore(1, '\n');
  38.   system("CLS");
  39.  
  40.   switch (choice) {
  41.   //Add Records
  42.   case '1':
  43.     addRecord();
  44.     break;
  45.  
  46.   //Update Records
  47.   case '2':
  48.     if (total == 0) {
  49.       cout << "No reservation records available." << endl;
  50.       main();
  51.     }
  52.     updateRecord();
  53.  
  54.     break;
  55.  
  56.   //Delete Records
  57.   case '3':
  58.     if (total == 0) {
  59.       cout << "No reservation records available." << endl;
  60.       main();
  61.     }
  62.     deleteRecord();
  63.  
  64.     break;
  65.  
  66.   //View Reservation Records
  67.   case '4':
  68.     if (total == 0) {
  69.       cout << "No reservation records available." << endl;
  70.       main();
  71.     }
  72.     viewRecord();
  73.     break;
  74.  
  75.   //Create Text File
  76.   case '5':
  77.     if (total == 0) {
  78.       cout << "No reservation records available." << endl;
  79.       main();
  80.     }
  81.     createTextFile();
  82.     break;
  83.  
  84.   case '6':
  85.     exit(1);
  86.     break;
  87.  
  88.   default:
  89.     cout << choice << " is not valid choice" << endl;
  90.     main();
  91.   }
  92.  
  93.   return 0;
  94. }
  95.  
  96. //OPTION 1 : Add Reservation Records
  97. void addRecord(){
  98.   string name;
  99.   int phoneNo, paxNo, day, month, year, time, date;
  100.   int correct = 1;  //variable condition for looping when there is an invalid input
  101.  
  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)>10){
  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++;    //generate unique booking number
  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 booking number exists
  250.   if(RBList.checker(bNo)){
  251.     RBList.PrintBookingNoRecords(bNo);
  252.     RBList.deleteNode(bNo);
  253.     cin.clear();
  254.     cin.ignore(1000, '\n');
  255.  
  256.     do{
  257.       cout<<"Enter name : ";
  258.       getline(cin, name);
  259.     }while(!checkName(name));
  260.  
  261.     if(name=="0"){
  262.       cout << "Press enter to continue . . .";
  263.       getchar();
  264.       system("CLS");
  265.       main();
  266.     }
  267.  
  268.     cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
  269.     cin>>phoneNo;
  270.  
  271.     correct = 1;
  272.     while (correct == 1){
  273.       if (cin.fail() || phoneNo<0 || checkNoOfDigits(phoneNo)>10){
  274.         cin.clear();
  275.         cin.ignore();
  276.         cout << "Invalid input! Enter positive numbers (maximum 11 digits)!" << endl<<endl;
  277.         cout<< "Enter phone number (e.g : 0123456789) : ";
  278.         cin >> phoneNo;
  279.       }else{
  280.         correct = 0;
  281.       }
  282.     }
  283.  
  284.     correct = 1;
  285.     cout<<endl<<"Enter number of pax (maximum : 50) : ";
  286.     cin>>paxNo;
  287.  
  288.     while (correct == 1){
  289.       if (cin.fail() || paxNo<=0 || paxNo > 50){
  290.         cin.clear();
  291.         cin.ignore();
  292.         cout << "Invalid input! Enter positive numbers (max : 50 , min : 1)!" << endl<<endl;
  293.         cout<< "Enter number of pax (maximum : 50) : ";
  294.         cin >> paxNo;
  295.       }else{
  296.         correct = 0;
  297.       }
  298.     }
  299.  
  300.     do{
  301.       correct = 1;
  302.       cout<<endl<<"Enter year (e.g : 2019): ";
  303.       cin>>year;
  304.       while (correct == 1){
  305.         if (cin.fail() || year > 2021){
  306.           cin.clear();
  307.           cin.ignore();
  308.           cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  309.           cout<< "Enter year (e.g : 2019) : ";
  310.           cin >> year;
  311.           //reset if a year before 2019 was entered then 2022 or later is entered
  312.         }else{
  313.           correct = 0;
  314.         }
  315.       }
  316.     }while(!checkYear(year));
  317.  
  318.     do{
  319.       correct = 1;
  320.       cout<<endl<<"Enter month (e.g : January = 1) : ";
  321.       cin>>month;
  322.       while (correct == 1){
  323.         if (cin.fail()){
  324.           cin.clear();
  325.           cin.ignore();
  326.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  327.           cout<< "Enter month (e.g : January = 1) : ";
  328.           cin >> month;
  329.         }else{
  330.           correct = 0;
  331.         }
  332.       }
  333.     }while(!checkMonth(month, year));
  334.  
  335.     do{
  336.       correct = 1;
  337.       cout<<endl<<"Enter day : ";
  338.       cin>>day;
  339.       while (correct == 1){
  340.         if (cin.fail()){
  341.           cin.clear();
  342.           cin.ignore();
  343.           cout << "Invalid input! Enter numbers!" << endl<<endl;
  344.           cout<< "Enter day : ";
  345.           cin >> day;
  346.         }else{
  347.           correct = 0;
  348.         }
  349.       }
  350.     }while(!checkDay(day, month, year));
  351.  
  352.     do{
  353.       correct = 1;
  354.       cout<<endl<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  355.       cin>>time;
  356.       while (correct == 1){
  357.         if (cin.fail()){
  358.           cin.clear();
  359.           cin.ignore();
  360.           cout << "Invalid input! Enter positive numbers (maximum 4 digits)!" << endl<<endl;
  361.           cout<< "Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  362.           cin >> time;
  363.         }else{
  364.           correct = 0;
  365.         }
  366.       }
  367.     }while(!checkTime(time, day, month, year));
  368.  
  369.     //for finding the reservation by date
  370.     date = year*10000 + month*100 + day;
  371.  
  372.     RBList.InsNewNodeByBookNo(bNo, name, phoneNo, paxNo, day, month, year, time, date);
  373.     cout<<"Update process complete."<<endl;
  374.   }else{
  375.     cout<<"No existing record with that booking number."<<endl
  376.     <<"Update process cancelled."<<endl;
  377.   }
  378.  
  379.   cin.clear();
  380.   cin.ignore(1000, '\n');
  381.   cout << "Press enter to continue . . .";
  382.   getchar();
  383.   system("CLS");
  384.   main();
  385. }
  386.  
  387. //OPTION 3 : Delete Reservation Records
  388. void deleteRecord(){
  389.   int  bNo; //booking number
  390.   char confirm;
  391.  
  392.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  393.   cout<<"Enter 0 to return to main menu."<<endl;
  394.   cout<<"Enter Booking Number : ";
  395.   cin>>bNo;
  396.   if(bNo == 0){
  397.     cout << "Press enter to continue . . .";
  398.     getchar();
  399.     system("CLS");
  400.     main();
  401.   }
  402.  
  403.   //if booking number exist
  404.   if(RBList.checker(bNo)){
  405.     RBList.PrintBookingNoRecords(bNo);
  406.     cout<<"Confirm?(y/n) : ";
  407.     cin>>confirm;
  408.  
  409.     switch (confirm) {
  410.       case 'y':
  411.         RBList.deleteNode(bNo);
  412.         total--;
  413.         cout<<"Deletion process complete."<<endl;
  414.         break;
  415.  
  416.       default:
  417.         cout<<"Deletion process cancelled."<<endl;
  418.         main();
  419.     }
  420.   }else{
  421.     cout<<"No existing record with that booking number."<<endl;
  422.   }
  423.  
  424.   cin.clear();
  425.   cin.ignore(1000, '\n');
  426.   cout << "Press enter to continue . . .";
  427.   getchar();
  428.   system("CLS");
  429.   main();
  430. }
  431.  
  432. //OPTION 4 : View Reservation Records
  433. void viewRecord(){
  434.   char choice;
  435.   string name;
  436.   int bNo, year, month, day, date, correct;
  437.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  438.   cout << "Please enter your selection" << endl <<
  439.   "1. View All Reservations(Sorted by Booking Number)" << endl <<
  440.   "2. Search by Name" << endl<<
  441.   "3. Search by Date"<< endl <<
  442.   "4. Back to Main Menu"<< endl;
  443.   cout << "Choice: ";
  444.   cin >> choice;
  445.   cin.clear();
  446.   cin.ignore(1, '\n');
  447.   system("CLS");
  448.  
  449.   switch (choice) {
  450.   //View All Reservations
  451.   case '1':
  452.     RBList.PrintList();
  453.     break;
  454.  
  455.   //Search by Name
  456.   case '2':
  457.     do{
  458.       cout<<"Enter name : ";
  459.       getline(cin, name);
  460.     }while(!checkName(name));
  461.  
  462.     RBList.PrintNameRecords(name);
  463.     break;
  464.  
  465.   case '3':
  466.  
  467.     cout<<endl<<"Enter year (e.g : 2019): ";
  468.     cin>>year;
  469.     while (correct == 1){
  470.       if (cin.fail() || year > 2021){
  471.         cin.clear();
  472.         cin.ignore();
  473.         cout << "Invalid input! Enter numbers (maximum year is 2021)!" << endl<<endl;
  474.         cout<< "Enter year (e.g : 2019) : ";
  475.         cin >> year;
  476.       }else{
  477.           correct = 0;
  478.       }
  479.     }
  480.  
  481.     correct = 1;
  482.     cout<<endl<<"Enter month (e.g : January = 1) : ";
  483.     cin>>month;
  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 month (e.g : January = 1) : ";
  490.         cin >> month;
  491.       }else{
  492.           correct = 0;
  493.       }
  494.     }
  495.  
  496.     cout<<endl<<"Enter day : ";
  497.     cin>>day;
  498.     while (correct == 1){
  499.       if (cin.fail()){
  500.         cin.clear();
  501.         cin.ignore();
  502.         cout << "Invalid input! Enter numbers!" << endl<<endl;
  503.         cout<< "Enter day : ";
  504.         cin >> day;
  505.       }else{
  506.           correct = 0;
  507.       }
  508.     }
  509.  
  510.     date = year*10000 + month*100 + day;
  511.  
  512.     RBList.PrintDateRecords(date);
  513.     cin.clear();
  514.     cin.ignore(1000, '\n');
  515.     break;
  516.  
  517.   case '4':
  518.     main();
  519.     break;
  520.  
  521.   default:
  522.     cout << choice << " is not valid choice" << endl;
  523.     viewRecord();
  524.   }
  525.  
  526.   cout << "Press enter to continue . . .";
  527.   cin.clear();
  528.   cin.ignore(1000, '\n');
  529.   system("CLS");
  530.   main();
  531. }
  532.  
  533. //OPTION 5 : Create Text File
  534. void createTextFile(){
  535.   char choice;
  536.   cout << "------------------=Create Txt File=------------------" << endl;
  537.   cout << "Please enter your selection" << endl <<
  538.   "1. Create Sort By Booking Number." << endl <<
  539.   "2. Back to Main Menu"<< endl;
  540.   cout << "Choice: ";
  541.   cin >> choice;
  542.  
  543.   switch (choice) {
  544.   //View All Reservations
  545.   case '1':
  546.     RBList.PrintListTextFile();
  547.     break;
  548.  
  549.   case '2':
  550.     main();
  551.     break;
  552.  
  553.   default:
  554.     cout << choice << " is not valid choice" << endl;
  555.     viewRecord();
  556.   }
  557.  
  558.   getchar();
  559.   cout << "-----------------------------------------------------" << endl;
  560.  
  561.   cout << "Press enter to continue . . .";
  562.   cin.clear();
  563.   cin.ignore(1000, '\n');
  564.   system("CLS");
  565.   main();
  566. }
  567.  
  568. //for checking whether the name is empty
  569. bool checkName(string name){
  570.   int counter = 0;
  571.   bool found = true;
  572.   //if found any characters other than space
  573.   if(name.find_first_not_of(" ") != std::string::npos){
  574.     counter++;
  575.   }
  576.  
  577.   //if found any characters other than the alphabets and a space
  578.   if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0") != std::string::npos){
  579.     found = true;
  580.   }else{
  581.     found = false;
  582.   }
  583.  
  584.   //if no input or if got any non alphabetic character
  585.   if(counter == 0 || found){
  586.     cout<<"Invalid Input! Need a name!"<<endl;
  587.     return false;
  588.   }else{
  589.     return true;
  590.   }
  591. }
  592.  
  593.  
  594. int checkNoOfDigits(int phoneNo){
  595.   if (phoneNo == 0){
  596.     return 0;
  597.   }else{
  598.     return 1 + checkNoOfDigits(phoneNo / 10);
  599.   }
  600. }
  601.  
  602. //for getting the current time
  603. time_t now = time(0);
  604. tm *ltm = localtime(&now);
  605.  
  606. int currentYear = 1900+ltm->tm_year;
  607. int currentMonth = 1 + ltm->tm_mon;
  608. int currentDay = ltm->tm_mday;
  609. int currentHour = ltm->tm_hour;
  610. int currentMinute = ltm->tm_min;
  611.  
  612. bool checkYear(int year){
  613.   if(year < currentYear){
  614.     cout<<"Can't go back in time!"<<endl;
  615.     return false;
  616.   }else{
  617.     return true;
  618.   }
  619. }
  620.  
  621. bool checkMonth(int month, int year){
  622.   if(month < currentMonth && year == currentYear){
  623.     cout<<"Can't go back in time!"<<endl;
  624.     return false;
  625.   }else if(month > 12){
  626.     cout<<"There are only 12 months in a year!"<<endl;
  627.     return false;
  628.   }else{
  629.     return true;
  630.   }
  631. }
  632.  
  633. bool checkDay(int day, int month, int year){
  634.   if(day < currentDay && month == currentMonth && year == currentYear){
  635.     cout<<"Can't go back in time!"<<endl;
  636.   //even months
  637.   }else if(day <= 0){
  638.     cout<<"Can't go back in time!"<<endl;
  639.   }else if(month%2 == 0){
  640.     // if the month is february
  641.     if(month == 2){
  642.       //february in common year that contain 28 days
  643.       if(year%4 != 0 && day>28){
  644.         cout << "Please key in between the day from 1 to 28"<< endl;
  645.       }
  646.       // february in leap year that contain 29 days
  647.       else if(year%4 == 0 && day>29){
  648.         cout << "Please key in between the day from 1 to 29"<< endl;
  649.       }
  650.       else{
  651.         return true;
  652.       }
  653.     }
  654.     // month that are even with 31 days
  655.     else if(month > 7 && day > 31){
  656.       cout << "Please key in between the day from 1 to 31"<< endl;
  657.     }
  658.     // month that are even  with 30 days
  659.     else if(month < 7 && day > 30){
  660.       cout << "Please key in between the day from 1 to 30"<< endl;
  661.     }
  662.     else{
  663.       return true;
  664.     }
  665.  
  666.   // month is odd
  667.   }else if(month%2 != 0){
  668.     // month that are odd with 30 days
  669.     if(month>8 && day > 30){
  670.       cout << "Please key in between the day from 1 to 30"<< endl;
  671.     // month that are odd with 31 days
  672.     }else if(month<8 && day > 31){
  673.       cout << "Please key in between the day from 1 to 31"<< endl;
  674.     }else{
  675.       return true;
  676.     }
  677.   }
  678.   else{
  679.     return true;
  680.   }
  681.   return false;
  682. }
  683.  
  684. bool checkTime(int time, int day, int month, int year){
  685.   int hour = time/100;
  686.   int minute = time - hour*100;
  687.  
  688.   if(hour > 24){
  689.     cout<<"Only 0 to 24 hours."<<endl;
  690.     return false;
  691.   }else if(hour < currentHour && day == currentDay && month == currentMonth && year == currentYear){
  692.     cout<<"Can't go back in time!"<<endl;
  693.     return false;
  694.   }else if(minute > 59){
  695.     cout<<"Only 0 to 59 minutes."<<endl;
  696.   }else if(minute < currentMinute && hour == currentHour && day == currentDay && month == currentMonth && year == currentYear){
  697.     cout<<"Can't go back in time!"<<endl;
  698.   }else{
  699.     return true;
  700.   }
  701.   return false;
  702. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement