Advertisement
pbwauyo

Untitled

Aug 9th, 2023
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <sstream>
  2. #include <vector>
  3. #include <string.h>
  4. #include <fstream>
  5. #include <stdio.h>
  6.  
  7. //Importing Product class
  8. #include "Product.cpp"
  9.  
  10. using namespace std;
  11.  
  12. class FileHandler{
  13.     public:
  14.     string filename;
  15.  
  16.     vector<Product> readJsonFile(){
  17.  
  18.         // Add code here
  19.         vector<Product> prodList;
  20.         vector<string> prodLines;
  21.         string prodLine;
  22.         Product manProd;
  23.        
  24.  
  25.         if (filename.empty()){
  26.             filename = "data/products.json";
  27.         }
  28.  
  29.         cout<<"Reading "<<filename<< " File........."<<endl;
  30.  
  31.         ifstream prodsFile(filename);
  32.  
  33.         while (getline(prodsFile, prodLine)){
  34.              
  35.                 prodLines.push_back(prodLine);
  36.  
  37.                 if(prodLine.substr(0,1) == "{"){
  38.                     if (prodLine.back() == ',')
  39.                     {
  40.                         prodLine.pop_back();
  41.                     }
  42.                    
  43.                     manProd.productFromJson(prodLine);
  44.                     prodList.push_back(manProd);
  45.                 }
  46.         }          
  47.        
  48.         cout<<"Finished Reading "<<filename<< " File........."<<endl;
  49.  
  50.         return prodList;
  51.     };
  52.  
  53.     void saveToJsonFile(Product p){
  54.  
  55.         vector<Product> pList;
  56.        
  57.         pList = readJsonFile();
  58.  
  59.         pList.push_back(p);
  60.  
  61.          // Check if the file exists.
  62.         ifstream input_file(filename);
  63.  
  64.         if (!input_file.good()) {
  65.             // The file does not exist.
  66.             cout << "First Record ........." << endl;
  67.  
  68.             ofstream jsonFile(filename);
  69.  
  70.             jsonFile<<"["<<endl;
  71.             jsonFile<< p.toJson()<<endl;
  72.             jsonFile<<"]"<<endl;
  73.  
  74.             return;
  75.  
  76.         }
  77.  
  78.         // Delete the file.
  79.         int ret = remove(filename.c_str());
  80.         if (ret != 0) {
  81.             std::cout << "Error deleting file: " << strerror(errno) << "\n";
  82.             return ;
  83.         }
  84.  
  85.         ofstream jsonFile(filename);
  86.         jsonFile<<"["<<endl;
  87.         for(int i=0; i<pList.size(); i++){
  88.  
  89.             if(i< pList.size() -1){
  90.                 jsonFile<< pList.at(i).toJson()<<","<<endl;
  91.             }
  92.             else{
  93.                 jsonFile<< pList.at(i).toJson()<<endl;
  94.             }
  95.         }
  96.         jsonFile<<"]"<<endl;      
  97.     }
  98.  
  99.     void saveToJsonFile(vector<Product> productList){
  100.          // Check if the file exists.
  101.         ifstream input_file(filename);
  102.  
  103.         if (input_file.good())
  104.         {
  105.             // Delete the file.
  106.             int ret = remove(filename.c_str());
  107.             if (ret != 0) {
  108.                 std::cout << "Error deleting file: " << strerror(errno) << "\n";
  109.                 return ;
  110.             }
  111.         }
  112.  
  113.         ofstream jsonFile(filename);
  114.         jsonFile<<"["<<endl;
  115.         for(int i=0; i<productList.size(); i++){
  116.             if(i< productList.size() -1){
  117.                 jsonFile<< productList.at(i).toJson()<<","<<endl;
  118.             }
  119.             else{
  120.                 jsonFile<< productList.at(i).toJson()<<endl;
  121.             }
  122.         }
  123.         jsonFile<<"]"<<endl;      
  124.     }
  125. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement