Advertisement
Guest User

DSA Int Main(2)

a guest
Oct 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <stdlib.h> //for exit() function
  4. #include <fstream>  //for creating text file
  5. using namespace std;
  6.  
  7. int total = 0;
  8.  
  9. class Reservation{
  10. private:
  11.     string name;
  12.     int phoneNo;
  13.     int paxNo;
  14.     int day;
  15.     int month;
  16.     int year;
  17.     int time;
  18.     int date;
  19.     long dateAndTime;
  20. public:
  21.     Reservation(){
  22.         name = "Tan Phit Huan";
  23.         phoneNo = 0123456777;
  24.         paxNo = 1;
  25.         day = 1;
  26.         month = 1;
  27.         year = 2019;
  28.         time = 1500;
  29.         date = 20190101;
  30.         dateAndTime = 201910201649;
  31.     }
  32.     Reservation(string na, int phNo, int pxNo, int d, int mon, int y, int t,int da, long dNt){
  33.         name = na;
  34.         phoneNo = phNo;
  35.         paxNo = pxNo;
  36.         day = d;
  37.         month = mon;
  38.         year = y;
  39.         time = t;
  40.         date = da;
  41.         dateAndTime =dNt;
  42.     }
  43.     void setName(string na){
  44.         name = na;
  45.     }
  46.     string getName(){
  47.         return name;
  48.     }
  49.     void setPhoneNo(int phNo){
  50.         phoneNo = phNo;
  51.     }
  52.     int getPhoneNo(){
  53.         return phoneNo;
  54.     }
  55.     void setPaxNo(int pxNo){
  56.         paxNo = pxNo;
  57.     }
  58.     int getPaxNo(){
  59.         return paxNo;
  60.     }
  61.     void setDay(int d){
  62.         day = d;
  63.     }
  64.     int getDay(){
  65.         return day;
  66.     }
  67.     void setMonth(int m){
  68.         month = m;
  69.     }
  70.     int getMonth(){
  71.         return month;
  72.     }
  73.     void setYear(int y){
  74.         year = y;
  75.     }
  76.     int getYear(){
  77.         return year;
  78.     }
  79.     void setTime(int t){
  80.         time = t;
  81.     }
  82.     int getTime(){
  83.         return time;
  84.     }
  85.     void setDate(int da){
  86.         date = da;
  87.     }
  88.     int getDate(){
  89.         return date;
  90.     }
  91.     void setDateAndTime(long dNt){
  92.       dateAndTime = dNt;
  93.     }
  94.     long getDateAndTime(){
  95.       return dateAndTime;
  96.     }
  97. };
  98.  
  99. class linklist{
  100. private:
  101.   struct node{
  102.     Reservation info;
  103.     node *link;
  104.   }*head;
  105.  
  106. public:
  107.   linklist(){
  108.     head=NULL;
  109.   }
  110.  
  111.   void InsNewNode(string na, int phNo, int pxNo, int d, int mon, int y, int t, long dNt){
  112.     node*newNode = new node;
  113.     node*p,*q = head;
  114.     newNode -> info.setName(na);
  115.     newNode -> info.setPhoneNo(phNo);
  116.     newNode -> info.setPaxNo(pxNo);
  117.     newNode -> info.setDay(d);
  118.     newNode -> info.setMonth(mon);
  119.     newNode -> info.setYear(y);
  120.     newNode -> info.setTime(t);
  121.     newNode -> info.setDateAndTime(dNt);
  122.  
  123.     //If no records
  124.     if (head == NULL){
  125.       head = newNode;
  126.       return;
  127.     }
  128.  
  129.     //Insert at the start of list
  130.     if (head -> info.getDateAndTime() > newNode -> info.getDateAndTime()){
  131.       newNode -> link = head;
  132.       head = newNode;
  133.       return;
  134.     }
  135.  
  136.     while(q->info.getDateAndTime() < newNode -> info.getDateAndTime() && q->link != NULL){
  137.       p = q;
  138.       q = q->link;
  139.     }
  140.     //Insert at the end of the list
  141.     if(q == NULL){
  142.       q = newNode;
  143.       newNode-> link = NULL;
  144.       return;
  145.     //Insert in the middle of the list
  146.     }else{
  147.       p = q;
  148.       q = q->link;
  149.       p->link = newNode;
  150.       newNode-> link = q;
  151.       return;
  152.     }
  153.   }
  154.  
  155.   void deleteNode(int num){
  156.     //Delete first node
  157.     if(num == 1){
  158.       node*temp = head;
  159.       head = head -> link;
  160.       delete temp;
  161.     }
  162.     //Delete last node
  163.     else if(num == total && total != 1){
  164.       node*p, *q = head;
  165.       while(q -> link != NULL){
  166.         p = q;
  167.         q = q -> link;
  168.       }
  169.       p->link = NULL;
  170.       delete q;
  171.     }
  172.     //Delete nodes in the middle
  173.     else{
  174.       node*p, *q = head;
  175.       for(int i = 1; i<num; i++){
  176.         p = q;
  177.         q = q -> link;
  178.       }
  179.       p -> link = q ->link;
  180.       delete q;
  181.     }
  182.     total--;
  183.   }
  184.   void PrintList(){
  185.     node *current;
  186.     current = head;
  187.     int count = 0;
  188.     cout<<setfill(' ')<<setw(5)<<"| No. "
  189.     <<left<<setw(30)<<"Name"
  190.     <<setw(5)<<"Phone No."
  191.     <<right<<setw(16)<<"Pax No."
  192.     <<setw(10)<<"Date"
  193.     <<setw(16)<<"Time |"<<endl
  194.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  195.  
  196.     while (current != NULL){
  197.       count++;
  198.       cout<<"| "<<setfill(' ')<<count<<"   "
  199.       <<left<<setw(30)<< current->info.getName()
  200.       <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  201.       <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  202.       <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  203.       <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  204.       current = current->link;
  205.     }
  206.     cout<<endl;
  207.   }
  208.  
  209.   void PrintNameRecords(string name){
  210.     node *current;
  211.     current = head;
  212.     int count = 0;
  213.     cout<<setfill(' ')<<setw(5)<<"| No. "
  214.     <<left<<setw(30)<<"Name"
  215.     <<setw(5)<<"Phone No."
  216.     <<right<<setw(16)<<"Pax No."
  217.     <<setw(10)<<"Date"
  218.     <<setw(16)<<"Time |"<<endl
  219.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  220.  
  221.     while (current != NULL){
  222.       //If there is any reservation with the same name
  223.       if(name == current->info.getName()){
  224.         count++;
  225.         cout<<"| "<<setfill(' ')<<count<<"   "
  226.         <<left<<setw(30)<< current->info.getName()
  227.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  228.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  229.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  230.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  231.       }
  232.       current = current->link;
  233.     }
  234.     cout<<endl;
  235.   }
  236.  
  237.   void PrintDateRecords(int date){
  238.     node *current;
  239.     current = head;
  240.     int count = 0;
  241.     cout<<setfill(' ')<<setw(5)<<"| No. "
  242.     <<left<<setw(30)<<"Name"
  243.     <<setw(5)<<"Phone No."
  244.     <<right<<setw(16)<<"Pax No."
  245.     <<setw(10)<<"Date"
  246.     <<setw(16)<<"Time |"<<endl
  247.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  248.  
  249.     while (current != NULL){
  250.       //If there is any reservation with the same date
  251.       if(date == current->info.getDate()){
  252.         count++;
  253.         cout<<"| "<<setfill(' ')<<count<<"   "
  254.         <<left<<setw(30)<< current->info.getName()
  255.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  256.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  257.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  258.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  259.       }
  260.       current = current->link;
  261.     }
  262.     cout<<endl;
  263.   }
  264.  
  265.   void PrintListTextFile(){
  266.     string fileName;
  267.     int count = 0;
  268.     cout << "---------------=CREATE TEXT FILE=---------------" << endl;
  269.     //Create file with the file name user entered
  270.     cout << "Enter file name: ";
  271.     cin >> fileName;
  272.     cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  273.     fileName = fileName + ".txt";
  274.  
  275.     //output to .txt file
  276.     ofstream file(fileName.c_str());
  277.  
  278.     if (file.is_open()) {
  279.       //output all the reservation records
  280.       node *current;
  281.       current = head;
  282.       file<<setfill(' ')<<setw(5)<<"| No. "
  283.       <<left<<setw(30)<<"Name"
  284.       <<setw(5)<<"Phone No."
  285.       <<right<<setw(16)<<"Pax No."
  286.       <<setw(10)<<"Date"
  287.       <<setw(16)<<"Time |"<<endl
  288.       <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  289.  
  290.       while (current != NULL){
  291.         count++;
  292.         file<<"| "<<setfill(' ')<<count<<"   "
  293.         <<left<<setw(30)<< current->info.getName()
  294.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  295.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  296.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  297.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  298.         current = current->link;
  299.       }
  300.       file.close(); //close file
  301.     }        
  302.   }
  303.   // ~linklist(){
  304.   //     node *q;
  305.   //     if(head == NULL){
  306.   //         return;
  307.   //     }
  308.   //     while( head != NULL ){
  309.   //         q = head->link;
  310.   //         delete head;
  311.   //         head = q;
  312.   //     }
  313.   // }
  314. };
  315.  
  316. linklist RList;
  317. void addRecord();
  318. void updateRecord();
  319. void deleteRecord();
  320. void viewRecord();  //1. select by name. 2. Display all
  321. void createTextFile();
  322.  
  323. int main() {
  324.   int choice;
  325.   cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
  326.   cout << "Please enter your selection" << endl <<
  327.     "1. Add Reservation Records" << endl <<
  328.     "2. Update Reservation Records" << endl <<
  329.     "3. Delete Reservation Records" << endl <<
  330.     "4. View Reservation Records  " << endl <<
  331.     "5. Create text file" << endl <<
  332.     "6. Exit" << endl;
  333.  
  334.   cout << "Choice: ";
  335.   cin >> choice;
  336.   cin.clear();
  337.   cin.ignore(1, '\n');
  338.   system("CLS");
  339.  
  340.   switch (choice) {
  341.   //Add Records
  342.   case 1:
  343.     addRecord();
  344.     break;
  345.  
  346.   //Update Records
  347.   case 2:
  348.     if (total == 0) {
  349.       cout << "No reservation records available." << endl;
  350.       main();
  351.     }
  352.     updateRecord();
  353.  
  354.     break;
  355.  
  356.   //Delete Records
  357.   case 3:
  358.     if (total == 0) {
  359.       cout << "No reservation records available." << endl;
  360.       main();
  361.     }
  362.     deleteRecord();
  363.  
  364.     break;
  365.  
  366.   //View Reservation Records
  367.   case 4:
  368.     if (total == 0) {
  369.       cout << "No reservation records available." << endl;
  370.       main();
  371.     }
  372.     viewRecord();
  373.     break;
  374.  
  375.   //Create Text File
  376.   case 5:
  377.     if (total == 0) {
  378.       cout << "No reservation records available." << endl;
  379.       main();
  380.     }
  381.     createTextFile();
  382.     break;
  383.  
  384.   case 6:
  385.     exit(1);
  386.     break;
  387.  
  388.   default:
  389.     cout << choice << " is not valid choice" << endl;
  390.     main();
  391.   }
  392.  
  393.   return 0;
  394. }
  395.  
  396. //OPTION 1 : Add Reservation Records
  397. void addRecord(){
  398.   string name;
  399.   int phoneNo, paxNo, day, month, year, time;
  400.   long dateAndTime;
  401.   cout << "-----------------=ADD RECORD=-------------------" << endl;
  402.   cout<<"Enter name : ";
  403.   getline(cin, name);
  404.  
  405.   cout<<"Enter phone No. (e.g : 0123456789) : ";
  406.   cin>>phoneNo;
  407.  
  408.   cout<<"Enter pax No. : ";
  409.   cin>>paxNo;
  410.  
  411.   cout<<"Enter year (e.g : 2019) : ";
  412.   cin>>year;
  413.  
  414.   cout<<"Enter month (e.g : January = 1) : ";
  415.   cin>>month;
  416.  
  417.   cout<<"Enter day : ";
  418.   cin>>day;
  419.  
  420.   cout<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
  421.   cin>>time;
  422.  
  423.   //for inserting the node in order
  424.   dateAndTime = year*100000000 + month*1000000 + day*10000 + time;
  425.  
  426.   RList.InsNewNode(name, phoneNo, paxNo, day, month, year, time, dateAndTime);
  427.   total++;
  428.   cin.clear();
  429.   cin.ignore(1000, '\n');
  430.   cout << "Press enter to continue . . .";
  431.   getchar();
  432.   system("CLS");
  433.   main();
  434. }
  435.  
  436. //OPTION 2 : Update Reservation Records
  437. void updateRecord(){
  438.   cout << "----------------=UPDATE RECORD=-----------------" << endl;
  439. }
  440.  
  441. //OPTION 3 : Delete Reservation Records
  442. void deleteRecord(){
  443.   int num;
  444.   bool correct;
  445.   cout << "----------------=DELETE RECORD=-----------------" << endl;
  446.   RList.PrintList();
  447.   do{
  448.     correct = true;
  449.     cout<<"(Enter 0 to go back to Main Menu)"<<endl
  450.     <<"Enter the No. you wish to delete : ";
  451.     cin>>num;
  452.     //if more than total reservations or negative number
  453.     if(num > total || num < 0){
  454.       cout<<endl
  455.       <<"INVALID INPUT!"<<endl
  456.       <<"Total Reservations : "<<total<<endl;
  457.       correct = false;
  458.     }else if(num == 0){
  459.       main();
  460.     }else{
  461.       RList.deleteNode(num);
  462.     }
  463.   }while(!correct);
  464.  
  465.   cin.clear();
  466.   cin.ignore(1000, '\n');
  467.   cout << "Press enter to continue . . .";
  468.   getchar();
  469.   system("CLS");
  470.   main();
  471. }
  472.  
  473. //OPTION 4 : View Reservation Records
  474. void viewRecord(){
  475.   int choice;
  476.   string name;
  477.   int year, month, day, date;
  478.   cout << "-----------------=VIEW RECORD=------------------" << endl;
  479.   cout << "Please enter your selection" << endl <<
  480.   "1. View All Reservations" << endl <<
  481.   "2. Search by Name" << endl<<
  482.   "3. Search by Date"<<endl<<
  483.   "4. Back to Main Menu"<<endl;
  484.   cout << "Choice: ";
  485.   cin >> choice;
  486.   cin.clear();
  487.   cin.ignore(1, '\n');
  488.   system("CLS");
  489.  
  490.   switch (choice) {
  491.   //View All Reservations
  492.   case 1:
  493.     RList.PrintList();
  494.     break;
  495.  
  496.   //Search by Name
  497.   case 2:
  498.     cout<<"Enter Name : ";
  499.     getline(cin, name);
  500.  
  501.     RList.PrintNameRecords(name);
  502.  
  503.     break;
  504.  
  505.   case 3:
  506.     cout<<"Enter year (e.g : 2019) : ";
  507.     cin>>year;
  508.  
  509.     cout<<"Enter month (e.g : January = 1) : ";
  510.     cin>>month;
  511.  
  512.     cout<<"Enter day : ";
  513.     cin>>day;
  514.  
  515.     date = year*10000 + month*100 + day;
  516.  
  517.  
  518.     break;
  519.  
  520.   case 4:
  521.     main();
  522.     break;
  523.  
  524.   default:
  525.     cout << choice << " is not valid choice" << endl;
  526.     main();
  527.   }
  528.  
  529.   cout << "Press enter to continue . . .";
  530.   getchar();
  531.   system("CLS");
  532.   main();
  533. }
  534.  
  535. //OPTION 5 : Create Text File
  536. void createTextFile(){
  537.   RList.PrintListTextFile();
  538.   getchar();
  539.   cout << "-----------------------------------------------------" << endl;
  540.  
  541.   cout << "Press enter to continue . . .";
  542.   cin.clear();
  543.   cin.ignore(1000, '\n');
  544.   system("CLS");
  545.   main();
  546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement