Advertisement
Zeinab_Hamdy

Untitled

Mar 15th, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 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 >> id ;
  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))
  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.  
  174.  
  175. };
  176.  
  177.  
  178. int main()
  179. {
  180.  
  181.  
  182.     Customer c1 ;
  183.  
  184.     ofstream out("data.txt", ios::out | ios::app );
  185.     int choise = 0;
  186.     cout << "1.new user sign up : \n2.old user log in : ";
  187.     cout <<"\nEnter your choice : ";
  188.     cin >> choise;
  189.     if(choise==1)  // new user (id , name , username, password)
  190.     {
  191.         c1.setID();
  192.         c1.setName();
  193.         c1.setUsername(1);
  194.         c1.setPassword();
  195.  
  196.         out.write( (char*)&c1, sizeof(c1));
  197.  
  198.     }
  199.     else
  200.     {
  201.  
  202.         c1.setUsername(2);
  203.         c1.setPassword();
  204.  
  205.         while(!c1.validLogin(c1.getUsername(), c1.getPassword()))
  206.         {
  207.             cout << "Invalid logging in \t please try again :\n";
  208.             c1.setUsername(2);
  209.             c1.setPassword();
  210.         }
  211.     }
  212.  
  213.     out.close();
  214.  
  215.  
  216.  
  217.     return 0;
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement