Advertisement
Zeinab_Hamdy

Untitled

Mar 15th, 2023
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 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.     // getter
  14.     int getId()
  15.     {
  16.         return id;
  17.     }
  18.  
  19.     char* getName()
  20.     {
  21.         return name;
  22.     }
  23.  
  24.     char* getPassword()
  25.     {
  26.         return password;
  27.     }
  28.  
  29.     char* getUsername()
  30.     {
  31.         return username;
  32.     }
  33.  
  34.  
  35.  
  36.     // search about id in file of customers
  37.     bool searchId(int val)
  38.     {
  39.  
  40.         ifstream inFile("data.txt", ios::in);
  41.  
  42.         if(inFile.is_open())
  43.         {
  44.             Customer c1 ;
  45.             while(!inFile.eof())
  46.             {
  47.                 inFile.read( (char *)&c1, sizeof(c1) );
  48.  
  49.                 if(c1.getId() == val) return true;
  50.             }
  51.  
  52.         }
  53.         else
  54.             cout <<"Cann't be opened the file \n" ;
  55.         inFile.close();
  56.  
  57.         return false;
  58.     }
  59.  
  60.  
  61.     bool validId(double val)
  62.     {
  63.         return val > 0 and int(val)==val and !searchId(val) ;
  64.     }
  65.  
  66.     void setID() // new user only
  67.     {
  68.         double x ;
  69.         cout << "Enter ID : ";
  70.         cin >> x;
  71.  
  72.  
  73.         while(!validId(x))
  74.         {
  75.             cout <<"Invalid Id , please try again \n";
  76.             cout << "Enter ID : ";
  77.             cin >> x;
  78.  
  79.         }
  80.         id = int(x);
  81.     }
  82.  
  83.  
  84.     void setName()
  85.     {
  86.         cout <<"Enter Name : ";
  87.         cin >> name;
  88.     }
  89.  
  90.     bool validUsername(char val[20])    // search if found same username in the file
  91.     {
  92.  
  93.         ifstream inFile("data.txt", ios::in);
  94.  
  95.         if(inFile.is_open())
  96.         {
  97.             Customer c1 ;
  98.             while(!inFile.eof())
  99.             {
  100.                 inFile.read( (char *)&c1, sizeof(c1) );
  101.  
  102.                 if(strcmp(c1.getUsername(), val)==0 ) return false;
  103.             }
  104.         }
  105.         else
  106.             cout <<"Cann't be opened the file \n" ;
  107.  
  108.         inFile.close();
  109.  
  110.         return true;
  111.     }
  112.  
  113.     void setUsername( int option )
  114.     {
  115.         if(option == 1) // new user -> check if valid username
  116.         {
  117.             char temp[20];
  118.             cout <<"Enter username : " ;
  119.             cin >> temp;
  120.  
  121.             while(!validUsername(temp))
  122.             {
  123.                 cout <<"Invalid username , please try again \n";
  124.                 cout << "Enter username : ";
  125.                 cin >> temp;
  126.             }
  127.  
  128.             for(int i=0; i< 20; i++) username[i]=temp[i];
  129.  
  130.         }
  131.  
  132.         else  // old user
  133.         {
  134.             cout << "Enter username : ";
  135.             cin >> username;
  136.         }
  137.     }
  138.  
  139.     void setPassword()
  140.     {
  141.         cout <<"Enter Password : ";
  142.         cin >> password;
  143.     }
  144.  
  145.  
  146.     bool validLogin( char user[20], char pass [20] )
  147.     {
  148.         ifstream inFile("data.txt", ios::in);
  149.  
  150.         if(inFile.is_open())
  151.         {
  152.             Customer c1 ;
  153.             while(!inFile.eof())
  154.             {
  155.                 inFile.read( (char *)&c1, sizeof(c1) );
  156.  
  157.                 if(strcmp(c1.getUsername(), user)==0
  158.                         and strcmp(c1.getPassword(), pass)==0)
  159.                 {
  160.                     cout << "Logged in Successfully \n Welcome \n";
  161.                     cout << "Id\tname\tusername\tpassword\n";
  162.                     cout << c1.getId() << '\t' << c1.getName() << '\t' <<
  163.                          c1.getUsername() << '\t' << c1.getPassword() << "\n";
  164.                     return true;
  165.                 }
  166.             }
  167.         }
  168.         else
  169.             cout <<"Cann't be opened the file \n" ;
  170.         return false;
  171.     }
  172.  
  173.     string decryted(string s)
  174.     {
  175.         for(int i=0 ; i < 20 ; i++ )
  176.             s[i]+='&';
  177.         return s;
  178.     }
  179.    
  180.     string incryted(string s)
  181.     {
  182.         for(int i=0 ; i < 20 ; i++ )
  183.             s[i]-='&';
  184.         return s;
  185.     }
  186.     void printCustomers()
  187.     {
  188.         ifstream inFile("data.txt", ios::in);
  189.         if(inFile.is_open())
  190.         {
  191.             Customer c1 ;
  192.             cout <<"ID\tName\tUsername\tpassword\n\n";
  193.             while(!inFile.eof())
  194.             {
  195.                 inFile.read((char *)&c1, sizeof(c1));
  196.  
  197.  
  198.                 string temp  = c1.getPassword();
  199.  
  200.                 if(!inFile.eof())
  201.                     cout <<c1.getId() <<'\t' << c1.getName() <<'\t' <<
  202.                          c1.getUsername() << '\t' << decryted(temp) << "\n";
  203.             }
  204.         }
  205.         else cout <<"Cann't opened this file\n";
  206.  
  207.         inFile.close();
  208.  
  209.     }
  210.  
  211. };
  212.  
  213.  
  214. int main()
  215. {
  216.  
  217.  
  218.     Customer c1 ;
  219.     c1.printCustomers();
  220.  
  221.     ofstream out("data.txt", ios::out | ios::app );
  222.     int choise = 0;
  223.     cout << "1.new user sign up : \n2.old user log in : ";
  224.     cout <<"\nEnter your choice : ";
  225.     cin >> choise;
  226.     if(choise==1)  // new user (id , name , username, password)
  227.     {
  228.         c1.setID();
  229.         c1.setName();
  230.         c1.setUsername(1);
  231.         c1.setPassword();
  232.  
  233.         out.write( (char*)&c1, sizeof(c1));
  234.  
  235.     }
  236.     else
  237.     {
  238.  
  239.         c1.setUsername(2);
  240.         c1.setPassword();
  241.  
  242.         while(!c1.validLogin(c1.getUsername(), c1.getPassword()))
  243.         {
  244.             cout << "Invalid logging in \t please try again :\n";
  245.             c1.setUsername(2);
  246.             c1.setPassword();
  247.         }
  248.     }
  249.  
  250.     out.close();
  251.  
  252.  
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement