Advertisement
Zeinab_Hamdy

Untitled

Mar 17th, 2023
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <string.h>
  4. #include <fstream>
  5. using namespace std;
  6. class Product
  7. {
  8.   int id;
  9.   int price;
  10.   char name[20];
  11.  
  12. public:
  13.   void setId()
  14.   {
  15.     cout << "Enter ID : ";
  16.     cin >> id;
  17.   }
  18.  
  19.   int getId()
  20.   {
  21.     return id;
  22.   }
  23.  
  24.   void setPrice()
  25.   {
  26.     cout << "Enter price : ";
  27.     cin >> price;
  28.   }
  29.  
  30.   int getPrice()
  31.   {
  32.     return price;
  33.   }
  34.  
  35.   void setName()
  36.   {
  37.     cout << "Enter name : ";
  38.     cin >> name;
  39.   }
  40.  
  41.   char *getName()
  42.   {
  43.     return name;
  44.   }
  45. };
  46.  
  47. void writeproduct()
  48. {
  49.   fstream out("product.txt", ios::out | ios::app);
  50.   Product p1;
  51.   char ch;
  52.   do
  53.   {
  54.     p1.setId();
  55.     p1.setName();
  56.     p1.setPrice();
  57.  
  58.     out.write((char *)&p1, sizeof(p1));
  59.     cout << "if you want to enter another data\tpress Y/N \n";
  60.     cin >> ch;
  61.   } while (ch == 'Y' || ch == 'y');
  62.   out.close();
  63. }
  64.  
  65. void readproduct()
  66. {
  67.   Product p2;
  68.   ifstream in;
  69.   in.open("product.txt", ios::in);
  70.   if (in.is_open())
  71.   {
  72.     cout << "Id\tName\tPrice\n";
  73.     while (!in.eof())
  74.     {
  75.       in.read((char *)&p2, sizeof(p2));
  76.       if (!in.eof())
  77.         cout << p2.getId() << '\t' << p2.getName() << '\t' << p2.getPrice() << "\n";
  78.     } // in.close();
  79.   }
  80.   else
  81.   {
  82.     cout << "Can't open the file \n";
  83.   }
  84. }
  85.  
  86. void searchproduct()
  87. {
  88.  
  89.   Product p3; // creating new third object
  90.   char str[10];
  91.   bool found = false;
  92.   cout << "Enter name to search for :";
  93.   cin >> str;
  94.   fstream in2;                      // check this
  95.   in2.open("product.txt", ios::in); // check this
  96.   if (in2.is_open())
  97.   {
  98.     in2.read((char *)&p3, sizeof(p3)); // read
  99.     while (!in2.eof())
  100.     { // check
  101.       if (strcmp(str, p3.getName()) == 0)
  102.       {
  103.  
  104.         cout << p3.getId() << '\t' << p3.getName() << '\t' << p3.getPrice() << "\n"; // print
  105.         found = true;
  106.       }
  107.       in2.read((char *)&p3, sizeof(p3)); // read
  108.     }
  109.     if (!found)
  110.       cout << "Name not found\n";
  111.     in2.close();
  112.   }
  113.   else
  114.     cout << "Cannot open this file"
  115.          << "\n";
  116. }
  117.  
  118. void updateproduct()
  119. {
  120.   Product p4;
  121.   fstream f("product.txt", ios::in | ios::out);
  122.   if (f.is_open())
  123.   {
  124.     char str[20];
  125.     cout << "Enter the product name to update his record ";
  126.     cin >> str;
  127.     bool found = false;
  128.     while (!f.eof())
  129.     {
  130.       f.read((char *)&p4, sizeof(p4));
  131.       if (strcmp(str, p4.getName()) == 0)
  132.       {
  133.         cout << "Enter the new price for " << str << " ";
  134.         p4.setPrice();
  135.         int curpos = f.tellg();
  136.         int stusize = sizeof(p4);
  137.         f.seekp(curpos - stusize, ios::beg);
  138.         f.write((char *)&p4, sizeof(p4));
  139.         found = true;
  140.         break;
  141.       }
  142.     }
  143.     f.close();
  144.     if (!found)
  145.       cout << "Product name does not exist.\n";
  146.   }
  147. }
  148.  
  149. void deleterecord()
  150. {
  151.   fstream in("product.txt", ios::in);
  152.   ofstream out("Product Temp.txt", ios::out);
  153.   if (in.is_open())
  154.   {
  155.     Product p5;
  156.     char str[20];
  157.     int num;
  158.     cout << "Enter the id of product to delete his record ";
  159.     cin >> num;
  160.     while (in.read((char *)&p5, sizeof(p5)))
  161.     {
  162.      if (num!=p5.getId())
  163.       {
  164.         out.write((char *)&p5, sizeof(p5));
  165.       }
  166.     }
  167.     in.close();
  168.     out.close();
  169.     remove("product.txt");
  170.     rename("Product Temp.txt", "product.txt");
  171.   }
  172. }
  173.  
  174. int main()
  175. {
  176.  // writeproduct();
  177.    readproduct();
  178.   // updateproduct();
  179.   // readproduct();
  180.   // searchproduct();
  181.    deleterecord();
  182.   //readproduct();
  183.   //updateproduct();
  184.   readproduct();
  185.  
  186.   return 0;
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement