SHARE
TWEET

Untitled

a guest Dec 7th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "service.h"
  2. #include "os_utils.h"
  3.  
  4. // array of `service_item`s
  5. std::vector<service_item*>* service_items = new std::vector<service_item*>();
  6.  
  7. // converts status code to readable string
  8. std::string status_code_to_string(char code)
  9. {
  10.     switch (code)
  11.     {
  12.         case 0: return "PENDING ASSESSMENT";
  13.         case 1: return "SERVICE IN PROGRESS";
  14.         default: return "ERROR FINDING STATUS";
  15.     }
  16. }
  17.  
  18. // helper function to check if a vehicle exists
  19. bool vehicle_exists(std::string name)
  20. {
  21.     // lambda function that finds the vehicle with a specific name
  22.     auto item = std::find_if(service_items->begin(), service_items->end(),
  23.         [name](service_item* x) -> bool {
  24.             return x->vehicleCode == name;
  25.         }
  26.     );
  27.  
  28.     // if an item is found, return true, otherwise return false
  29.     return item != service_items->end();
  30. }
  31.  
  32. // display function, as required
  33. void display()
  34. {
  35.     std::cout << "VEHICLE CODE / STATUS\n\n";
  36.  
  37.     // for each vehicle, print "<vehicle code> / <status>"
  38.     for each (service_item* item in *service_items)
  39.     {
  40.         std::cout << item->vehicleCode + " / " + status_code_to_string(item->status) + "\n";
  41.     }
  42.  
  43.     // if there are no vehicles, just print NO VEHICLES
  44.     if (service_items->size() <= 0) {
  45.         std::cout << "NO VEHICLES\n";
  46.     }
  47.  
  48.     // print the option menu
  49.     std::cout << "\n1. ADD VEHICLE\n2. UPDATE STATUS\n3. REMOVE VEHICLE\n4. EXIT\n";
  50. }
  51.  
  52. // add vehicle function, as required
  53. void add_vehicle(std::string name)
  54. {
  55.     // make a new vehicle and give it a name
  56.     service_item* new_item = new service_item();
  57.     new_item->vehicleCode = name;
  58.  
  59.     // add the vehicle to the list
  60.     service_items->insert(service_items->end(), new_item);
  61. }
  62.  
  63. // update vehicle function, as required
  64. void update_vehicle(std::string name, char status)
  65. {
  66.     // make sure the vehicle even exists
  67.     if (!vehicle_exists(name)) {
  68.         std::cout << "THAT VEHICLE DOES NOT EXIST. TRY AGAIN.";
  69.         std::getchar();
  70.         return;
  71.     }
  72.  
  73.     // make sure the status is either 0 or 1
  74.     if (status < 0 || status > 1) {
  75.         std::cout << "INVALID STATUS. MUST BE EITHER 0 OR 1.";
  76.         std::getchar();
  77.         return;
  78.     }
  79.  
  80.     // lambda function to find a vehicle with a specific name
  81.     auto item = std::find_if(service_items->begin(), service_items->end(),
  82.         [name](service_item* x) -> bool {
  83.             return x->vehicleCode == name;
  84.         }
  85.     );
  86.  
  87.     // set the vehicle's status
  88.     (*item)->status = status;
  89. }
  90.  
  91. // remove vehicle function, as required
  92. void remove_vehicle(std::string name)
  93. {
  94.     // make sure the vehicle even exists
  95.     if (!vehicle_exists(name)) {
  96.         std::cout << "THAT VEHICLE DOES NOT EXIST. TRY AGAIN.";
  97.         std::getchar();
  98.         return;
  99.     }
  100.  
  101.     // lambda function to remove a vehicle with a specific name
  102.     auto removed = std::remove_if(service_items->begin(), service_items->end(),
  103.         [name](service_item* x) -> bool {
  104.             return x->vehicleCode == name;
  105.         }
  106.     );
  107.  
  108.     service_items->erase(removed, service_items->end()); // some non-important C++ bullshit
  109. }
  110.  
  111. // save data function, as required
  112. void save_data()
  113. {
  114.     // start with an empty string and fill it in the following format:
  115.     // <vehicle code> [SPACE] <vehicle status>
  116.     std::string output = "";
  117.     for each (service_item* item in *service_items)
  118.     {
  119.         output += item->vehicleCode + " " + std::to_string(item->status) + "\n";
  120.     }
  121.  
  122.     // write the output text to a file
  123.     std::ofstream data_file("data.txt");
  124.     data_file << output;
  125.     data_file.close();
  126. }
  127.  
  128. // load data function, as required
  129. void load_data()
  130. {
  131.     // open file
  132.     std::ifstream data_file("data.txt");
  133.  
  134.     // read each line
  135.     std::string line;
  136.     while (std::getline(data_file, line))
  137.     {
  138.         // if line is empty, ignore it
  139.         if (line == "") continue;
  140.  
  141.         // split the line into an string array containing:
  142.         // { 0: vehicle code, 1: vehicle status }
  143.         auto parts = split(line);
  144.  
  145.         // add a vehicle using the name (which is the 0th item in the array)
  146.         add_vehicle(parts[0]);
  147.  
  148.         // update the vehicle using the name (0) and the status it should have (1)
  149.         update_vehicle(parts[0], std::stoi(parts[1]));
  150.     }
  151.  
  152.     data_file.close();
  153. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top