Advertisement
Zeinab_Hamdy

Untitled

Mar 18th, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.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.     void setPrice()
  443.     {
  444.         cout << "Enter price : ";
  445.         cin >> price;
  446.     }
  447.  
  448.     int getPrice()
  449.     {
  450.         return price;
  451.     }
  452.  
  453.     void setName()
  454.     {
  455.         cout << "Enter name : ";
  456.         cin >> name;
  457.     }
  458.  
  459.     char *getName()
  460.     {
  461.         return name;
  462.     }
  463. };
  464.  
  465. void writeproduct()
  466. {
  467.     fstream out("product.txt", ios::out | ios::app);
  468.     Product p1;
  469.     char ch;
  470.     do
  471.     {
  472.         p1.setId();
  473.         p1.setName();
  474.         p1.setPrice();
  475.  
  476.         out.write((char *)&p1, sizeof(p1));
  477.         cout << "if you want to enter another data\tpress Y/N \n";
  478.         cin >> ch;
  479.     }
  480.     while (ch == 'Y' || ch == 'y');
  481.     out.close();
  482. }
  483.  
  484. void readproduct()
  485. {
  486.     Product p2;
  487.     ifstream in;
  488.     in.open("product.txt", ios::in);
  489.     if (in.is_open())
  490.     {
  491.         cout << "Id\tName\tPrice\n";
  492.         while (!in.eof())
  493.         {
  494.             in.read((char *)&p2, sizeof(p2));
  495.             if (!in.eof())
  496.                 cout << p2.getId() << '\t' << p2.getName() << '\t' << p2.getPrice() << "\n";
  497.         } // in.close();
  498.     }
  499.     else
  500.     {
  501.         cout << "Can't open the file \n";
  502.     }
  503. }
  504.  
  505. void searchproduct()
  506. {
  507.  
  508.     Product p3; // creating new third object
  509.     char str[10];
  510.     bool found = false;
  511.     cout << "Enter name to search for :";
  512.     cin >> str;
  513.     fstream in2;                      // check this
  514.     in2.open("product.txt", ios::in); // check this
  515.     if (in2.is_open())
  516.     {
  517.         in2.read((char *)&p3, sizeof(p3)); // read
  518.         while (!in2.eof())
  519.         {
  520.             // check
  521.             if (strcmp(str, p3.getName()) == 0)
  522.             {
  523.  
  524.                 cout << p3.getId() << '\t' << p3.getName() << '\t' << p3.getPrice() << "\n"; // print
  525.                 found = true;
  526.             }
  527.             in2.read((char *)&p3, sizeof(p3)); // read
  528.         }
  529.         if (!found)
  530.             cout << "Name not found\n";
  531.         in2.close();
  532.     }
  533.     else
  534.         cout << "Cannot open this file"
  535.              << "\n";
  536. }
  537.  
  538. void updateproduct()
  539. {
  540.     Product p4;
  541.     fstream f("product.txt", ios::in | ios::out);
  542.     if (f.is_open())
  543.     {
  544.         char str[20];
  545.         cout << "Enter the product name to update his record ";
  546.         cin >> str;
  547.         bool found = false;
  548.         while (!f.eof())
  549.         {
  550.             f.read((char *)&p4, sizeof(p4));
  551.             if (strcmp(str, p4.getName()) == 0)
  552.             {
  553.                 cout << "Enter the new price for " << str << " ";
  554.                 p4.setPrice();
  555.                 int curpos = f.tellg();
  556.                 int stusize = sizeof(p4);
  557.                 f.seekp(curpos - stusize, ios::beg);
  558.                 f.write((char *)&p4, sizeof(p4));
  559.                 found = true;
  560.                 break;
  561.             }
  562.         }
  563.         f.close();
  564.         if (!found)
  565.             cout << "Product name does not exist.\n";
  566.     }
  567. }
  568.  
  569. void deleterecord()
  570. {
  571.     fstream in("product.txt", ios::in);
  572.     ofstream out("Product Temp.txt", ios::out);
  573.     if (in.is_open())
  574.     {
  575.         Product p5;
  576.         char str[20];
  577.         int num;
  578.         cout << "Enter the id of product to delete his record ";
  579.         cin >> num;
  580.         while (in.read((char *)&p5, sizeof(p5)))
  581.         {
  582.             if (num!=p5.getId())
  583.             {
  584.                 out.write((char *)&p5, sizeof(p5));
  585.             }
  586.         }
  587.         in.close();
  588.         out.close();
  589.         remove("product.txt");
  590.         rename("Product Temp.txt", "product.txt");
  591.     }
  592. }
  593. int main()
  594. {
  595.  
  596.  
  597.     Employee c1 ;
  598.  
  599.     cout <<"Uesrs  Employee:\n\n";
  600.     c1.printEmployee();
  601.     cout << "\n\n";
  602.  
  603.     ofstream out("Employee.txt", ios::out | ios::app );
  604.     int choise = 0;
  605.     cout << "1.new user sign up : \n2.old user log in : ";
  606.     cout <<"\nEnter your choice : ";
  607.     cin >> choise;
  608.     if(choise==1)  // new user (id , name , username, password)
  609.     {
  610.         c1.setID();
  611.         c1.setName();
  612.         c1.setUsername(1);
  613.         c1.setPassword();
  614.  
  615.         out.write( (char*)&c1, sizeof(c1));
  616.  
  617.     }
  618.     else
  619.     {
  620.  
  621.         c1.setUsername(2);
  622.         c1.setPassword();
  623.  
  624.         while(!c1.validLogin(c1.getUsername(), c1.getPassword()))
  625.         {
  626.             cout << "Invalid logging in \t please try again :\n";
  627.             c1.setUsername(2);
  628.             c1.setPassword();
  629.         }
  630.     }
  631.  
  632.     out.close();
  633.  
  634.  
  635.  
  636.  
  637.     return 0;
  638. }
  639.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement