Advertisement
Zeinab_Hamdy

Untitled

Mar 17th, 2023
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 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. public :
  12.  
  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. };
  48.  
  49. void writeproduct()
  50. {
  51.     fstream out("product.txt", ios::out | ios::app );
  52.     Product p1 ;
  53.     char ch;
  54.     do
  55.     {
  56.         p1.setId();
  57.         p1.setName();
  58.         p1.setPrice();
  59.  
  60.         out.write( (char*)&p1, sizeof(p1));
  61.         cout <<"if you want to enter another data\tpress Y/N \n";
  62.         cin >> ch ;
  63.     }
  64.     while( ch=='Y' || ch=='y');
  65.     out.close();
  66. }
  67.  
  68. void readproduct()
  69. {
  70.     Product p2;
  71.     ifstream in;
  72.     in.open ("product.txt", ios::in);
  73.     if(in.is_open())
  74.     {
  75.         cout << "Id\tName\tPrice\n";
  76.         while(!in.eof())
  77.         {
  78.             in.read( (char *) &p2, sizeof(p2));
  79.             if(!in.eof())
  80.                 cout << p2.getId() << '\t' << p2.getName()<< '\t' << p2.getPrice() << "\n" ;
  81.         }//in.close();
  82.     }
  83.     else
  84.     {
  85.         cout <<"Can't open the file \n";
  86.     }
  87.  
  88.  
  89. }
  90.  
  91. void searchproduct()
  92. {
  93.  
  94.     Product p3;//creating new third object
  95.     char str[10];
  96.     bool found=false;
  97.     cout<<"Enter name to search for :";
  98.     cin>>str;
  99.     fstream in2;//check this
  100.     in2.open("product.txt",ios::in);//check this
  101.     if(in2.is_open())
  102.     {
  103.         in2.read((char*)&p3,sizeof(p3));//read
  104.         while(!in2.eof()) //check
  105.         {
  106.             if(strcmp(str,p3.getName())==0)
  107.             {
  108.  
  109.                 cout << p3.getId() << '\t' << p3.getName()<< '\t' << p3.getPrice() << "\n" ;//print
  110.                 found=true;
  111.  
  112.             }
  113.             in2.read((char*)&p3,sizeof(p3));//read
  114.         }
  115.         if(!found)cout<<"Name not found\n";
  116.         in2.close();
  117.  
  118.     }
  119.     else cout<<"Cannot open this file"<<"\n";
  120.  
  121. }
  122.  
  123. void updateproduct()
  124. {
  125.     Product p;
  126.     fstream f("product.txt",ios::in | ios::out);
  127.     if(f.is_open())
  128.     {
  129.         char str[20];
  130.         cout<<"Enter the product name to update his record ";
  131.         cin>>str;
  132.         bool found=false;
  133.         while(!f.eof())
  134.         {
  135.             f.read((char*)&p,sizeof(p));
  136.             if(strcmp(str,p.getName())==0)
  137.             {
  138.                 cout<<"Enter the new price for "<<str<<" ";
  139.                 p.setPrice();
  140.                 int curpos=f.tellg();
  141.                 int stusize=sizeof(p);
  142.                 f.seekp(curpos-stusize,ios::beg);
  143.                 f.write((char*)&p,sizeof(p));
  144.                 found=true;
  145.                 break;
  146.             }
  147.         }
  148.         f.close();
  149.         if(!found)
  150.             cout<<"Product name does not exist.";
  151.     }
  152. }
  153.  
  154. void delet()
  155. {
  156.     fstream in("product.txt",ios::in);
  157.     ofstream out("Product Temp.txt",ios::out);
  158.     Product p5;
  159.         char str[20];
  160.         cout<<"Enter the name of product to delete his record ";
  161.         cin>>str;
  162.     if(in.is_open())
  163.     {
  164.  
  165.         while(in.read((char*)&p5,sizeof(p5)))
  166.         {
  167.             if(strcmp(str,p5.getName())!=0)
  168.             {
  169.                 out.write((char*)&p5,sizeof(p5));
  170.             }
  171.         }
  172.         in.close();
  173.         out.close();
  174.         remove("product.txt");
  175.         rename("Product Temp.txt","product.txt");
  176.     }
  177. }
  178.  
  179. int main()
  180. {
  181.  
  182.  
  183.     // writeproduct();
  184.      readproduct();
  185.  
  186.    // updateproduct();
  187.     //readproduct();
  188.     //searchproduct();
  189.     //readproduct();
  190.     //delet();
  191.     //readproduct();
  192.  
  193.  
  194.     return 0;
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement