Advertisement
Zeinab_Hamdy

Untitled

Mar 18th, 2023
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Customer
  5. {
  6.  
  7.     int id ;
  8.     char name[20], password[20], username[20];
  9.  
  10. public :
  11.  
  12.  
  13.     int getId()
  14.     {
  15.         return id;
  16.     }
  17.  
  18.     char* getName()
  19.     {
  20.         return name;
  21.     }
  22.  
  23.     char* getPassword()
  24.     {
  25.         return password;
  26.     }
  27.  
  28.     char* getUsername()
  29.     {
  30.         return username;
  31.     }
  32.  
  33.  
  34.     bool searchId(int val)
  35.     {
  36.  
  37.         ifstream inFile("Customer.txt", ios::in);
  38.  
  39.         if(inFile.is_open())
  40.         {
  41.             Customer c1 ;
  42.             while(!inFile.eof())
  43.             {
  44.                 inFile.read( (char *)&c1, sizeof(c1) );
  45.  
  46.                 if(c1.getId() == val) return true;
  47.             }
  48.  
  49.         }
  50.         else
  51.             cout <<"Cann't be opened the file \n" ;
  52.         inFile.close();
  53.  
  54.         return false;
  55.     }
  56.  
  57.  
  58.     bool validId(double val)
  59.     {
  60.         return val > 0 and int(val)==val and !searchId(val) ;
  61.     }
  62.  
  63.     void setID() // new user only
  64.     {
  65.         double x ;
  66.         cout << "Enter ID : ";
  67.         cin >> x;
  68.  
  69.  
  70.         while(!validId(x))
  71.         {
  72.             cout <<"Invalid Id , please try again \n";
  73.             cout << "Enter ID : ";
  74.             cin >> x;
  75.  
  76.         }
  77.         id = int(x);
  78.     }
  79.  
  80.  
  81.     void setName()
  82.     {
  83.         cout <<"Enter Name : ";
  84.         cin >> name;
  85.     }
  86.  
  87.     bool validUsername(char val[20])    // search if found same username in the file
  88.     {
  89.  
  90.         ifstream inFile("Customer.txt", ios::in);
  91.  
  92.         if(inFile.is_open())
  93.         {
  94.             Customer c1 ;
  95.             while(!inFile.eof())
  96.             {
  97.                 inFile.read( (char *)&c1, sizeof(c1) );
  98.  
  99.                 if(strcmp(c1.getUsername(), val)==0 ) return false;
  100.             }
  101.         }
  102.         else
  103.             cout <<"Cann't be opened the file \n" ;
  104.  
  105.         inFile.close();
  106.  
  107.         return true;
  108.     }
  109.  
  110.     void setUsername( int option )
  111.     {
  112.         if(option == 1) // new user -> check if valid username
  113.         {
  114.             char temp[20];
  115.             cout <<"Enter username : " ;
  116.             cin >> temp;
  117.  
  118.             while(!validUsername(temp))
  119.             {
  120.                 cout <<"Invalid username , please try again \n";
  121.                 cout << "Enter username : ";
  122.                 cin >> temp;
  123.             }
  124.  
  125.             for(int i=0; i< 20; i++) username[i]=temp[i];
  126.  
  127.         }
  128.  
  129.         else  // old user
  130.         {
  131.             cout << "Enter username : ";
  132.             cin >> username;
  133.         }
  134.     }
  135.  
  136.     void setPassword()
  137.     {
  138.         cout <<"Enter Password : ";
  139.         cin >> password;
  140.     }
  141.  
  142.  
  143.     bool validLogin( char user[20], char pass [20] )
  144.     {
  145.         ifstream inFile("Customer.txt", ios::in);
  146.  
  147.         if(inFile.is_open())
  148.         {
  149.             Customer c1 ;
  150.             while(!inFile.eof())
  151.             {
  152.                 inFile.read( (char *)&c1, sizeof(c1) );
  153.  
  154.                 if(strcmp(c1.getUsername(), user)==0
  155.                         and strcmp(c1.getPassword(), pass)==0)
  156.                 {
  157.                     cout << "Logged in Successfully \n Welcome \n";
  158.                     cout << "Id\tname\tusername\tpassword\n";
  159.                     cout << c1.getId() << '\t' << c1.getName() << '\t' <<
  160.                          c1.getUsername() << '\t' << c1.getPassword() << "\n";
  161.                     return true;
  162.                 }
  163.             }
  164.         }
  165.         else
  166.             cout <<"Cann't be opened the file \n" ;
  167.         return false;
  168.     }
  169.  
  170.     string decryted(string s)
  171.     {
  172.         for(int i=0 ; i < 20 ; i++ )
  173.             s[i]+='&';
  174.         return s;
  175.     }
  176.  
  177.     string incryted(string s)
  178.     {
  179.         for(int i=0 ; i < 20 ; i++ )
  180.             s[i]-='&';
  181.         return s;
  182.     }
  183.     void printCustomers()
  184.     {
  185.         ifstream inFile("Customer.txt", ios::in);
  186.         if(inFile.is_open())
  187.         {
  188.             Customer c1 ;
  189.             cout <<"ID\tName\tUsername\tpassword\n\n";
  190.             while(!inFile.eof())
  191.             {
  192.                 inFile.read((char *)&c1, sizeof(c1));
  193.  
  194.  
  195.                 string temp  = c1.getPassword();
  196.  
  197.                 if(!inFile.eof())
  198.                     cout <<c1.getId() <<'\t' << c1.getName() <<'\t' <<
  199.                          c1.getUsername() << '\t' << decryted(temp) << "\n";
  200.             }
  201.         }
  202.         else cout <<"Cann't opened this file\n";
  203.  
  204.         inFile.close();
  205.  
  206.     }
  207.  
  208. };
  209.  
  210.  
  211. class Employee
  212. {
  213.  
  214.     int id ;
  215.     char name[20], password[20], username[20];
  216.  
  217. public :
  218.  
  219.  
  220.     int getId()
  221.     {
  222.         return id;
  223.     }
  224.  
  225.     char* getName()
  226.     {
  227.         return name;
  228.     }
  229.  
  230.     char* getPassword()
  231.     {
  232.         return password;
  233.     }
  234.  
  235.     char* getUsername()
  236.     {
  237.         return username;
  238.     }
  239.  
  240.  
  241.  
  242.     // search about id in file of customers
  243.     bool searchId(int val)
  244.     {
  245.  
  246.         ifstream inFile("Employee.txt", ios::in);
  247.  
  248.         if(inFile.is_open())
  249.         {
  250.             Employee e1 ;
  251.             while(!inFile.eof())
  252.             {
  253.                 inFile.read( (char *)&e1, sizeof(e1) );
  254.  
  255.                 if(e1.getId() == val) return true;
  256.             }
  257.  
  258.         }
  259.         else
  260.             cout <<"Cann't be opened the file \n" ;
  261.         inFile.close();
  262.  
  263.         return false;
  264.     }
  265.  
  266.  
  267.     bool validId(double val)
  268.     {
  269.         return val > 0 and int(val)==val and !searchId(val) ;
  270.     }
  271.  
  272.     void setID() // new user only
  273.     {
  274.         double x ;
  275.         cout << "Enter ID : ";
  276.         cin >> x;
  277.  
  278.  
  279.         while(!validId(x))
  280.         {
  281.             cout <<"Invalid Id , please try again \n";
  282.             cout << "Enter ID : ";
  283.             cin >> x;
  284.  
  285.         }
  286.         id = int(x);
  287.     }
  288.  
  289.  
  290.     void setName()
  291.     {
  292.         cout <<"Enter Name : ";
  293.         cin >> name;
  294.     }
  295.  
  296.     bool validUsername(char val[20])    // search if found same username in the file
  297.     {
  298.  
  299.         ifstream inFile("Employee.txt", ios::in);
  300.  
  301.         if(inFile.is_open())
  302.         {
  303.             Employee e1 ;
  304.             while(!inFile.eof())
  305.             {
  306.                 inFile.read( (char *)&e1, sizeof(e1) );
  307.  
  308.                 if(strcmp(e1.getUsername(), val)==0 ) return false;
  309.             }
  310.         }
  311.         else
  312.             cout <<"Cann't be opened the file \n" ;
  313.  
  314.         inFile.close();
  315.  
  316.         return true;
  317.     }
  318.  
  319.     void setUsername( int option )
  320.     {
  321.         if(option == 1) // new user -> check if valid username
  322.         {
  323.             char temp[20];
  324.             cout <<"Enter username : " ;
  325.             cin >> temp;
  326.  
  327.             while(!validUsername(temp))
  328.             {
  329.                 cout <<"Invalid username , please try again \n";
  330.                 cout << "Enter username : ";
  331.                 cin >> temp;
  332.             }
  333.  
  334.             for(int i=0; i< 20; i++) username[i]=temp[i];
  335.  
  336.         }
  337.  
  338.         else  // old user
  339.         {
  340.             cout << "Enter username : ";
  341.             cin >> username;
  342.         }
  343.     }
  344.  
  345.     void setPassword()
  346.     {
  347.         cout <<"Enter Password : ";
  348.         cin >> password;
  349.     }
  350.  
  351.  
  352.     bool validLogin( char user[20], char pass [20] )
  353.     {
  354.         ifstream inFile("Employee.txt", ios::in);
  355.  
  356.         if(inFile.is_open())
  357.         {
  358.             Employee e1 ;
  359.             while(!inFile.eof())
  360.             {
  361.                 inFile.read( (char *)&e1, sizeof(e1) );
  362.  
  363.                 if(strcmp(e1.getUsername(), user)==0
  364.                         and strcmp(e1.getPassword(), pass)==0)
  365.                 {
  366.                     cout << "Logged in Successfully \n Welcome \n";
  367.                     cout << "Id\tname\tusername\tpassword\n";
  368.                     cout << e1.getId() << '\t' << e1.getName() << '\t' <<
  369.                          e1.getUsername() << '\t' << e1.getPassword() << "\n";
  370.                     return true;
  371.                 }
  372.             }
  373.         }
  374.         else
  375.             cout <<"Cann't be opened the file \n" ;
  376.         return false;
  377.     }
  378.  
  379.     string decryted(string s)
  380.     {
  381.         for(int i=0 ; i < 20 ; i++ )
  382.             s[i]+='&';
  383.         return s;
  384.     }
  385.  
  386.     string incryted(string s)
  387.     {
  388.         for(int i=0 ; i < 20 ; i++ )
  389.             s[i]-='&';
  390.         return s;
  391.     }
  392.     void printEmployee()
  393.     {
  394.         ifstream inFile("Employee.txt", ios::in);
  395.         if(inFile.is_open())
  396.         {
  397.             Employee e1 ;
  398.             cout <<"ID\tName\tUsername\tpassword\n\n";
  399.             while(!inFile.eof())
  400.             {
  401.                 inFile.read((char *)&e1, sizeof(e1));
  402.  
  403.  
  404.                 string temp  = e1.getPassword();
  405.  
  406.                 if(!inFile.eof())
  407.                     cout <<e1.getId() <<'\t' << e1.getName() <<'\t' <<
  408.                          e1.getUsername() << '\t' << decryted(temp) << "\n";
  409.             }
  410.         }
  411.         else cout <<"Cann't opened this file\n";
  412.  
  413.         inFile.close();
  414.  
  415.     }
  416.  
  417. };
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424. class Product
  425. {
  426.     int id , price;
  427.     char name[20];
  428.  
  429. public:
  430.  
  431.     void setId()
  432.     {
  433.         cout << "Enter ID : ";
  434.         cin >> id;
  435.     }
  436.  
  437.     int getId()
  438.     {
  439.         return id;
  440.     }
  441.  
  442.  
  443.  
  444.     void setPrice()
  445.     {
  446.         cout << "Enter price : ";
  447.         cin >> price;
  448.     }
  449.  
  450.     int getPrice()
  451.     {
  452.         return price;
  453.     }
  454.  
  455.     void setName()
  456.     {
  457.         cout << "Enter name : ";
  458.         cin >> name;
  459.     }
  460.  
  461.     char *getName()
  462.     {
  463.         return name;
  464.     }
  465.  
  466.  
  467. };
  468.  
  469. void writeProduct()
  470. {
  471.     fstream out("Product.txt", ios::out | ios::app);
  472.     Product p1;
  473.     char ch;
  474.     do
  475.     {
  476.         p1.setId();
  477.         p1.setName();
  478.         p1.setPrice();
  479.  
  480.         out.write((char *)&p1, sizeof(p1));
  481.         cout << "if you want to enter another data\tpress Y/N \n";
  482.         cin >> ch;
  483.     }
  484.     while (ch == 'Y' || ch == 'y');
  485.     out.close();
  486. }
  487.  
  488. void readProduct()
  489. {
  490.     ifstream in("Product.txt", ios::in);
  491.  
  492.     if (in.is_open())
  493.     {
  494.         Product p2;
  495.         cout << "Id\tName\tPrice\n";
  496.         while (!in.eof())
  497.         {
  498.             in.read((char *)&p2, sizeof(p2));
  499.             if (!in.eof())
  500.                 cout << p2.getId() << '\t' << p2.getName() << '\t' << p2.getPrice() << "\n";
  501.         }
  502.     }
  503.     else
  504.         cout << "Can't open the file \n";
  505.  
  506.     in.close();
  507. }
  508.  
  509. void searchProduct()
  510. {
  511.     ifstream in2("Product.txt", ios::in);
  512.  
  513.     if (in2.is_open())
  514.     {
  515.         Product p3;
  516.         bool found = false;
  517.         char str[10];
  518.  
  519.         cout << "Enter name to search for : ";
  520.         cin >> str;
  521.  
  522.         in2.read((char *)&p3, sizeof(p3));
  523.         while (!in2.eof())
  524.         {
  525.             if (strcmp(str, p3.getName()) == 0)
  526.             {
  527.                 cout << p3.getId() << '\t' << p3.getName() << '\t' << p3.getPrice() << "\n";
  528.                 found = true;
  529.             }
  530.  
  531.             in2.read((char *)&p3, sizeof(p3));
  532.         }
  533.  
  534.         if (!found)
  535.             cout << "Name not found\n";
  536.  
  537.         in2.close();
  538.     }
  539.     else
  540.         cout << "Cannot open this file\n";
  541. }
  542.  
  543. void updateProduct()
  544. {
  545.     Product p4;
  546.     fstream f("Product.txt", ios::in | ios::out);
  547.     if (f.is_open())
  548.     {
  549.         char str[20];
  550.         cout << "Enter the product name to update his record : ";
  551.         cin >> str;
  552.         bool found = false;
  553.         while (!f.eof())
  554.         {
  555.             f.read((char *)&p4, sizeof(p4));
  556.             if (strcmp(str, p4.getName()) == 0)
  557.             {
  558.                 cout << "Enter the new price for " << str << " : ";
  559.                 p4.setPrice();
  560.                 int curpos = f.tellg() , stusize = sizeof(p4);
  561.  
  562.                 f.seekp(curpos - stusize, ios::beg);
  563.                 f.write((char *)&p4, sizeof(p4));
  564.                 found = true;
  565.                 break;
  566.             }
  567.         }
  568.         f.close();
  569.         if (!found)
  570.             cout << "Product name does not exist.\n";
  571.     }
  572. }
  573.  
  574. void deleteRecord()
  575. {
  576.     fstream in("Product.txt", ios::in);
  577.     ofstream out("Product Temp.txt", ios::out);
  578.     if (in.is_open())
  579.     {
  580.         Product p5;
  581.  
  582.         int num;
  583.         cout << "Enter the id of product to delete his record : ";
  584.         cin >> num;
  585.         while (in.read((char *)&p5, sizeof(p5)))
  586.         {
  587.             if (num!=p5.getId())
  588.             {
  589.                 out.write((char *)&p5, sizeof(p5));
  590.             }
  591.         }
  592.         in.close();
  593.         out.close();
  594.         remove("Product.txt");
  595.         rename("Product Temp.txt", "Product.txt");
  596.     }
  597. }
  598.  
  599.  
  600. int main()
  601. {
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.     return 0;
  612. }
  613.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement