Advertisement
Guest User

Data Structure & Algorithms Assignment 1.1

a guest
Oct 9th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h> //for exit() function
  3. #include <fstream>  //for creating text file
  4. // #include "Car.h"  //child class
  5. using namespace std;
  6.  
  7. // //Global variables
  8. // string manufacturer, type, carPlateNo, model;
  9. // int seat;
  10. // const int SIZE = 80; //SIZE is the number of objects that the c[] array can store
  11. // Car c[SIZE]; //c[SIZE] is an array used to store all 80 car details
  12. // string option; //vairable for y/n choices
  13. // int choice; //choice is used for user selections(in menu selection, type selection)
  14. // int total = 0; //total recorded cars(used to keep count of total cars)
  15. // int main();
  16.  
  17. //Type options/selection(used in addRecord() & updateRecord())
  18. // string funcType() {
  19. //   string type;
  20. //   cout << "Select the type of the car\n " <<
  21. //     "1.Hatchback\n" <<
  22. //     " 2.Sedan\n" <<
  23. //     " 3.MPV\n" <<
  24. //     " 4.SUV\n" <<
  25. //     " 5.Crossover\n" <<
  26. //     " 6.Coupe\n" <<
  27. //     " 7.Convertible\n" <<
  28. //     " 8.Others \n";
  29.  
  30. //   cout << "Choice: ";
  31. //   cin >> choice;
  32. //   cin.clear();
  33. //   cin.ignore(1, '\n');
  34.  
  35. //   switch (choice) {
  36.  
  37. //   case 1:
  38. //     return "Hatchback";
  39. //     break;
  40.  
  41. //   case 2:
  42. //     return "Sedan";
  43. //     break;
  44.  
  45. //   case 3:
  46. //     return "MPV";
  47. //     break;
  48.  
  49. //   case 4:
  50. //     return "SUV";
  51. //     break;
  52.  
  53. //   case 5:
  54. //     return "Crossover";
  55. //     break;
  56.  
  57. //   case 6:
  58. //     return "Coupe";
  59. //     break;
  60.  
  61. //   case 7:
  62. //     return "Convertible";
  63. //     break;
  64.  
  65. //   case 8:
  66. //     cout << "Enter Type: ";
  67. //     getline(cin, type);
  68. //     return type;
  69. //     break;
  70.  
  71. //   default:
  72. //     //print error messege and allow user to re-enter choice
  73. //     cout << choice << " is not valid choice. Please choose again" << endl;
  74. //     return funcType();
  75. //   }
  76. // }
  77.  
  78. //Detects special characters and certain alphabets in car plate number(used in addRecord() & updateRecord())
  79. // char checker(string carPlateNo) {
  80. //   char cpnError;
  81. //   string cpnc(carPlateNo);
  82.  
  83. //   if (cpnc.find_first_not_of("abcdefghjklmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY0123456789 ") != string::npos) { //check until the end of the string
  84. //     cout << "Error. There are only alphabets(except for I,O & Z) and numbers in a car plate number.\n";
  85.  
  86. //     return cpnError = 'a';
  87. //   } else {
  88. //     return cpnError = 'b';
  89. //   }
  90.  
  91. // }
  92.  
  93. // //Detects if there is anything other than y or n ((used in addRecord() & updateRecord())
  94. // string checker2(string option) {
  95. //   string ynError(option);
  96.  
  97. //   if (ynError.find_first_not_of("ynYN") != string::npos) {
  98. //     cout << "Invalid input. Only 'y' or 'n'." << endl;
  99. //     return ynError = "a";
  100. //   } else {
  101. //     return ynError = "b";
  102. //   }
  103.  
  104. // }
  105.  
  106. //+1 total recorded cars(used in addRecord())
  107. // void myPlus() {
  108. //   total++;
  109. // }
  110.  
  111. // //-1 total recorded cars(used in deleteRecord())
  112. // void myMinus() {
  113. //   total--;
  114. // }
  115.  
  116. //OPTION 1: Creating cars
  117. void addRecord() {
  118.   cout << "------------------=NEW RECORD=------------------" << endl;
  119.   // cout << "Note: Only a maximum of 80 cars can be created." << endl;
  120.   cout << "------------------RECORD #" << total + 1 << "-------------------" << endl;
  121.  
  122.   //Manufacturer
  123.   cout << "Please enter the name:";
  124.   getline(cin, name);
  125.   // c[total].setManufacturer(manufacturer);
  126.  
  127.   //Type
  128.   // c[total].setType(funcType());
  129.  
  130.   //SeatNumber
  131.   string ynError; //ynError is used to check for anything other than y or n
  132.   // do {
  133.   //   cout << "Do you know what is the number of seats?(y/n)\n";
  134.   //   cin >> option;
  135.   //   ynError = checker2(option);
  136.   // } while ((ynError == "a")); //loop if option has anything other then y or n
  137.  
  138.   // if ((option == "y") || (option == "Y")) {
  139.   //   do {
  140.       cout << "Please enter number of seats: \n";
  141.       cin >> seat;
  142.  
  143.       if ((cin.fail()) || (seat <= 0)) {
  144.         cout << endl;
  145.         cout << "Invalid input. Please key in a positive integer." << endl; //print error message
  146.       }
  147.  
  148.     } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
  149.  
  150.     c[total].setSeat(seat);
  151.   } else {
  152.     c[total].setSeat(1); //seats number is set to 1 if it is unknown(default)
  153.   }
  154.  
  155.   //Car Plate Number
  156.   // char cpnError; //cpnError is used to check for special characters and certain alphabets
  157.   // cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  158.   // cin.ignore(1, '\n');
  159.   // getline(cin, carPlateNo);
  160.   // cpnError = checker(carPlateNo);
  161.  
  162.   if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
  163.     do {
  164.       cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  165.       getline(cin, carPlateNo);
  166.       cpnError = checker(carPlateNo);
  167.     } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
  168.   }
  169.   c[total].setCarPlateNo(carPlateNo);
  170.  
  171.   //Model
  172.   cout << "Please enter the model of the car: \n";
  173.   getline(cin, model);
  174.   c[total].setModel(model);
  175.  
  176.   //+1 to total
  177.   myPlus();
  178.  
  179.   cout << "-------------------------------------------" << endl;
  180.  
  181.   cout << "Press enter to continue . . .";
  182.   cin.clear();
  183.   cin.ignore(1000, '\n');
  184.   system("CLS"); //erase previous output
  185.   main(); //return to main() function
  186. }
  187.  
  188. //OPTION 2: OPTION 1: Display only one car of user's choice
  189. void disCar() {
  190.   int sd; //sd(single display) is the number of the car that user wants to display;
  191.   do {
  192.     //user selects which car they want to display
  193.     cout << "Enter 1 to display the first car that was created,2 for the second, etc." << endl;
  194.     cout << "Please enter which vehicle you want to display: ";
  195.     cin >> sd;
  196.     //checks if the user chose within the number of recorded cars
  197.     if ((sd <= 0) || (sd > total) || (cin.fail())) {
  198.       system("CLS");
  199.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  200.         "Please enter a number within range." << endl;
  201.       cout << endl;
  202.     }
  203.   } while ((sd <= 0) || (sd > total) || (cin.fail())); //allow user to re-enter
  204.  
  205.   //Print user's selected car details
  206.   cout << "------------------Car #" << sd << "------------------" << endl;
  207.   c[sd - 1].display();
  208.   cout << "-------------------------------------------" << endl;
  209.  
  210.   cout << "Press enter to continue . . .";
  211.   cin.get();  //pauses screen before previous output is erased(replace system("Pause"))
  212.   cout << "Press enter to continue . . .";
  213.   cin.clear();
  214.   cin.ignore(1000, '\n');
  215.   system("CLS");
  216.   main();
  217.  
  218. }
  219.  
  220. //OPTION 2: OPTION 2: Display all cars that is in the records
  221. void disCreated() {
  222.   for (int i = 0; i < total; i++) {
  223.     cout << "------------------Car #" << i + 1 << "------------------" << endl;
  224.     c[i].display();
  225.     cout << "-------------------------------------------" << endl;
  226.   }
  227.  
  228.   cout << "Press enter to continue . . .";
  229.   cin.get();
  230.   system("CLS");
  231.   main();
  232. }
  233.  
  234. //OPTION 2: OPTION 3: Display 80 cars(the ones that are not created will display default values)
  235. void dis80() {
  236.   for (int i = 0; i < SIZE; i++) {
  237.     cout << "------------------Car #" << i + 1 << "------------------" << endl;
  238.     c[i].display();
  239.     cout << "-------------------------------------------" << endl;
  240.   }
  241.  
  242.   cout << "Press enter to continue . . .";
  243.   cin.get();
  244.   system("CLS");
  245.   main();
  246. }
  247.  
  248. //OPTION 2: Display Options Menu
  249. void displayRecord() {
  250.   cout << "Select the choice"<<endl
  251.     <<"1.Display a single car" <<endl
  252.     <<"2.Display all cars in the car record"<<endl
  253.     <<"3.Display 80 cars"<<endl;
  254.  
  255.   cout << "Choice: ";
  256.   cin >> choice;
  257.   cin.clear();
  258.   cin.ignore(1, '\n');
  259.   system("CLS");
  260.  
  261.   switch (choice) {
  262.   case 1:
  263.     //print error messege to user when there is no cars in record
  264.     if (total == 0) {
  265.       cout << "You have no created cars available." << endl;
  266.       main();
  267.     }
  268.     //goes to disCar() function (display single car)
  269.     disCar();
  270.     break;
  271.  
  272.   case 2:
  273.     if (total == 0) {
  274.       cout << "You have no created cars available." << endl;
  275.       main();
  276.     }
  277.     disCreated();
  278.     break;
  279.  
  280.   case 3:
  281.     dis80();
  282.     break;
  283.  
  284.   default:
  285.     //print error messege and allow user to re-enter choice
  286.     cout << choice << " is not valid choice" << endl;
  287.     displayRecord();
  288.   }
  289. }
  290.  
  291. //OPTION 3: Updating/Overriding previous car details of user's choice
  292. //Note: updateRecord() is similar to addRecord()
  293. void updateRecord() {
  294.   int u; //u is the number of the vehicle that user wants to update;    
  295.   cout << "------------------=UPDATE=------------------" << endl;
  296.  
  297.   do {
  298.     //user selects which car they want to update
  299.     cout << "Enter 1 to update the first cars that was created,2 for the second, etc." << endl;
  300.     cout << "Please enter which vehicle you want to update: ";
  301.     cin >> u;
  302.     //checks if the user chose within the number of recorded cars
  303.     if ((u <= 0) || (u > total) || (cin.fail())) {
  304.       system("CLS");
  305.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  306.         "Please enter a number within range." << endl;
  307.         cin.clear();
  308.         cin.ignore(1000,'\n');
  309.       cout << endl;
  310.     }
  311.   } while ((u <= 0) || (u > total) || (cin.fail()));
  312.  
  313.   //Prints details of the car user wants to update
  314.   cout << endl;
  315.   cout << "------------------Current Car #" << u << "------------------" << endl;
  316.   c[u - 1].display();
  317.   cout << "-------------------------------------------" << endl;
  318.   cout << endl;
  319.  
  320.   //Manufacturer
  321.   cout << "Please enter the manufacturer of the car: ";
  322.   cin.clear();
  323.   cin.ignore(1000, '\n');
  324.   getline(cin, manufacturer);
  325.   c[u - 1].setManufacturer(manufacturer);
  326.  
  327.   //Type
  328.   c[u - 1].setType(funcType());
  329.  
  330.   //SeatNumber
  331.   string ynError;
  332.   do {
  333.     cout << "Do you know what is the number of seats?(y/n)\n";
  334.     cin >> option;
  335.     ynError = checker2(option);
  336.   } while ((ynError == "a")); //loop if option has anything other then y or n
  337.  
  338.   if ((option == "y") || (option == "Y")) {
  339.     do {
  340.       cout << "Please enter number of seats: \n";
  341.       cin >> seat;
  342.  
  343.       if ((cin.fail()) || (seat <= 0)) {
  344.         cout << endl;
  345.         cout << "Invalid input. Please key in a positive integer." << endl; //print error message
  346.       }
  347.  
  348.     } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
  349.     c[u-1].setSeat(seat);
  350.  
  351.   } else {
  352.     c[u-1].setSeat(1); //seats number is set to 1 if it is unknown(default)
  353.   }
  354.  
  355.   //Car Plate Number
  356.   char cpnError; //cpnError is used to check for special characters and certain alphabets
  357.   cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  358.   cin.ignore(1, '\n');
  359.   getline(cin, carPlateNo);
  360.   cpnError = checker(carPlateNo);
  361.  
  362.   if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
  363.     do {
  364.       cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  365.       getline(cin, carPlateNo);
  366.       cpnError = checker(carPlateNo);
  367.     } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
  368.   }
  369.   c[u - 1].setCarPlateNo(carPlateNo);
  370.  
  371.   //Model
  372.   cout << "Please enter the model of the car: \n";
  373.   getline(cin, model);
  374.   c[u - 1].setModel(model);
  375.  
  376.   cout << "-------------------------------------------" << endl;
  377.  
  378.   cout << "Press enter to continue . . .";
  379.   cin.clear();
  380.   cin.ignore(1000, '\n');
  381.   system("CLS");
  382.   main();
  383. }
  384.  
  385. //OPTION 4: Delete a car of user's choice
  386. void deleteRecord() {
  387.   string ynError;
  388.   int d; //d is number of the car that user wants to delete;
  389.   cout << "------------------=DELETE=------------------" << endl;
  390. g
  391.   do {
  392.     //user selects which car they want to delete
  393.     cout << "Enter 1 to delete the first car that was created,2 for the second, etc." << endl;
  394.     cout << "Please enter which car you want to delete: ";
  395.     cin >> d;
  396.     if ((d <= 0) || (d > total) || (cin.fail())) {
  397.       system("CLS");
  398.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  399.         "Please enter a number within range." << endl;
  400.       cout << endl;
  401.     }
  402.   } while ((d <= 0) || (d > total) || (cin.fail()));
  403.   cout << "------------------Car #" << d << "------------------" << endl;
  404.   c[d - 1].display();
  405.   cout << "-------------------------------------------" << endl;
  406.   cout << endl;
  407.  
  408.     do {
  409.       cout << "Are you sure you want to delete this car?(y/n)" << endl;
  410.       cin.clear();
  411.       cin.ignore();
  412.       cin >> option;
  413.       ynError = checker2(option);
  414.     } while (ynError == "a");
  415.  
  416.     if ((option == "y") || (option == "Y")) {
  417.       for (int i = d - 1; i < SIZE - 1; i++) {
  418.         c[i] = c[d]; //replaces a car's details with details of the next car starting from the car that the user chose to delete
  419.         d++;
  420.       }
  421.       c[SIZE - 1] = Car(); //give the last car(car #80) default values
  422.  
  423.       myMinus();
  424.     }
  425.  
  426.   cout << "Press enter to continue . . .";
  427.   cin.clear();
  428.   cin.ignore(1000, '\n');
  429.   system("CLS");
  430.   main();
  431. }
  432.  
  433. //OPTION 5: Creating a .txt file(To save car record)
  434. // void textFile() {
  435. //   //declare variables
  436. //   string fileName, ma, ty, cpn, mo;
  437. //   int se;
  438.  
  439. //   cout << "------------------=Create Txt File=------------------" << endl;
  440. //   //Create file with the file name user entered
  441. //   cout << "Enter file name: ";
  442. //   cin >> fileName;
  443. //   cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  444. //   fileName = fileName + ".txt";
  445.  
  446. //   //output to .txt file
  447. //   ofstream file(fileName.c_str());
  448.  
  449. //   if (file.is_open()) {
  450. //     //output all the cars in car record
  451. //     for (int i = 0; i < total; i++) {
  452. //       file << "------------------Car #" << i + 1 << "------------------" << endl;
  453. //       ma = c[i].getManufacturer(); //get the manufacturer value of the car stored in the array
  454. //       file << "Manufacturer: " << ma << endl;
  455. //       ty = c[i].getType();
  456. //       file << "Type: " << ty << endl;
  457. //       se = c[i].getSeat();
  458. //       file << "Seat: " << se << endl;
  459. //       cpn = c[i].getCarPlateNo();
  460. //       file << "Car Plate Number: " << cpn << endl;
  461. //       mo = c[i].getModel();
  462. //       file << "Model: " << mo << endl;
  463. //       file << "-------------------------------------------" << endl;
  464. //     }
  465. //     file.close(); //close file
  466. //   }
  467. //   cin.get();
  468. //   cout << "-----------------------------------------------------" << endl;
  469.  
  470. //   cout << "Press enter to continue . . .";
  471. //   cin.clear();
  472. //   cin.ignore(1000, '\n');
  473. //   system("CLS");
  474. //   main();
  475. // }
  476.  
  477. int main() {
  478.   cout << "===============WELCOME TO RESTAURANT RESERVATION SYSTEM===============" << endl;
  479.   cout << "Please enter your selection" << endl <<
  480.     "1. Insert New Record" << endl <<
  481.     "2. Update Existing Record" << endl <<
  482.     "3. Delete Existing Record" << endl <<
  483.     "4. View Existing Record" << endl <<
  484.     "5. Create txt file" << endl <<
  485.     "6. Exit" << endl;
  486.  
  487.   cout << "Choice: ";
  488.   cin >> choice;
  489.   cin.clear();
  490.   cin.ignore(1, '\n');
  491.   system("CLS");
  492.  
  493.   switch (choice) {
  494.   case 1:
  495.     addRecord();
  496.     break;
  497.  
  498.   case 2:
  499.   if (total == 0) {
  500.       cout << "You have no created cars available." << endl;
  501.       main();
  502.     }
  503.     updateRecord();
  504.     break;
  505.  
  506.   case 3:
  507.     if (total == 0) {
  508.       cout << "You have no created cars available." << endl;
  509.       main();
  510.     }
  511.     deleteRecord();
  512.     break;
  513.  
  514.   case 5:
  515.     textFile();
  516.     break;
  517.  
  518.   case 6:
  519.     exit(1); //behaves like return 1 and ends the program
  520.     break;
  521.  
  522.   default:
  523.     cout << choice << " is not valid choice" << endl;
  524.  
  525.     main();
  526.   }
  527.  
  528.   return 0;
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement