Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "service.h"
- #include "os_utils.h"
- // array of `service_item`s
- std::vector<service_item*>* service_items = new std::vector<service_item*>();
- // converts status code to readable string
- std::string status_code_to_string(char code)
- {
- switch (code)
- {
- case 0: return "PENDING ASSESSMENT";
- case 1: return "SERVICE IN PROGRESS";
- default: return "ERROR FINDING STATUS";
- }
- }
- // helper function to check if a vehicle exists
- bool vehicle_exists(std::string name)
- {
- // lambda function that finds the vehicle with a specific name
- auto item = std::find_if(service_items->begin(), service_items->end(),
- [name](service_item* x) -> bool {
- return x->vehicleCode == name;
- }
- );
- // if an item is found, return true, otherwise return false
- return item != service_items->end();
- }
- // display function, as required
- void display()
- {
- std::cout << "VEHICLE CODE / STATUS\n\n";
- // for each vehicle, print "<vehicle code> / <status>"
- for each (service_item* item in *service_items)
- {
- std::cout << item->vehicleCode + " / " + status_code_to_string(item->status) + "\n";
- }
- // if there are no vehicles, just print NO VEHICLES
- if (service_items->size() <= 0) {
- std::cout << "NO VEHICLES\n";
- }
- // print the option menu
- std::cout << "\n1. ADD VEHICLE\n2. UPDATE STATUS\n3. REMOVE VEHICLE\n4. EXIT\n";
- }
- // add vehicle function, as required
- void add_vehicle(std::string name)
- {
- // make a new vehicle and give it a name
- service_item* new_item = new service_item();
- new_item->vehicleCode = name;
- // add the vehicle to the list
- service_items->insert(service_items->end(), new_item);
- }
- // update vehicle function, as required
- void update_vehicle(std::string name, char status)
- {
- // make sure the vehicle even exists
- if (!vehicle_exists(name)) {
- std::cout << "THAT VEHICLE DOES NOT EXIST. TRY AGAIN.";
- std::getchar();
- return;
- }
- // make sure the status is either 0 or 1
- if (status < 0 || status > 1) {
- std::cout << "INVALID STATUS. MUST BE EITHER 0 OR 1.";
- std::getchar();
- return;
- }
- // lambda function to find a vehicle with a specific name
- auto item = std::find_if(service_items->begin(), service_items->end(),
- [name](service_item* x) -> bool {
- return x->vehicleCode == name;
- }
- );
- // set the vehicle's status
- (*item)->status = status;
- }
- // remove vehicle function, as required
- void remove_vehicle(std::string name)
- {
- // make sure the vehicle even exists
- if (!vehicle_exists(name)) {
- std::cout << "THAT VEHICLE DOES NOT EXIST. TRY AGAIN.";
- std::getchar();
- return;
- }
- // lambda function to remove a vehicle with a specific name
- auto removed = std::remove_if(service_items->begin(), service_items->end(),
- [name](service_item* x) -> bool {
- return x->vehicleCode == name;
- }
- );
- service_items->erase(removed, service_items->end()); // some non-important C++ bullshit
- }
- // save data function, as required
- void save_data()
- {
- // start with an empty string and fill it in the following format:
- // <vehicle code> [SPACE] <vehicle status>
- std::string output = "";
- for each (service_item* item in *service_items)
- {
- output += item->vehicleCode + " " + std::to_string(item->status) + "\n";
- }
- // write the output text to a file
- std::ofstream data_file("data.txt");
- data_file << output;
- data_file.close();
- }
- // load data function, as required
- void load_data()
- {
- // open file
- std::ifstream data_file("data.txt");
- // read each line
- std::string line;
- while (std::getline(data_file, line))
- {
- // if line is empty, ignore it
- if (line == "") continue;
- // split the line into an string array containing:
- // { 0: vehicle code, 1: vehicle status }
- auto parts = split(line);
- // add a vehicle using the name (which is the 0th item in the array)
- add_vehicle(parts[0]);
- // update the vehicle using the name (0) and the status it should have (1)
- update_vehicle(parts[0], std::stoi(parts[1]));
- }
- data_file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement