Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class Customer
- {
- int id ;
- char name[20], password[20], username[20];
- public :
- // getter
- int getId()
- {
- return id;
- }
- char* getName()
- {
- return name;
- }
- char* getPassword()
- {
- return password;
- }
- char* getUsername()
- {
- return username;
- }
- // search about id in file of customers
- bool searchId(int val)
- {
- ifstream inFile("data.txt", ios::in);
- if(inFile.is_open())
- {
- Customer c1 ;
- while(!inFile.eof())
- {
- inFile.read( (char *)&c1, sizeof(c1) );
- if(c1.getId() == val) return true;
- }
- }
- else
- cout <<"Cann't be opened the file \n" ;
- inFile.close();
- return false;
- }
- bool validId(double val)
- {
- return val > 0 and int(val)==val and !searchId(val) ;
- }
- void setID() // new user only
- {
- double x ;
- cout << "Enter ID : ";
- cin >> x;
- while(!validId(x))
- {
- cout <<"Invalid Id , please try again \n";
- cout << "Enter ID : ";
- cin >> x;
- }
- id = int(x);
- }
- void setName()
- {
- cout <<"Enter Name : ";
- cin >> name;
- }
- bool validUsername(char val[20]) // search if found same username in the file
- {
- ifstream inFile("data.txt", ios::in);
- if(inFile.is_open())
- {
- Customer c1 ;
- while(!inFile.eof())
- {
- inFile.read( (char *)&c1, sizeof(c1) );
- if(strcmp(c1.getUsername(), val)==0 ) return false;
- }
- }
- else
- cout <<"Cann't be opened the file \n" ;
- inFile.close();
- return true;
- }
- void setUsername( int option )
- {
- if(option == 1) // new user -> check if valid username
- {
- char temp[20];
- cout <<"Enter username : " ;
- cin >> temp;
- while(!validUsername(temp))
- {
- cout <<"Invalid username , please try again \n";
- cout << "Enter username : ";
- cin >> temp;
- }
- for(int i=0; i< 20; i++) username[i]=temp[i];
- }
- else // old user
- {
- cout << "Enter username : ";
- cin >> username;
- }
- }
- void setPassword()
- {
- cout <<"Enter Password : ";
- cin >> password;
- }
- bool validLogin( char user[20], char pass [20] )
- {
- ifstream inFile("data.txt", ios::in);
- if(inFile.is_open())
- {
- Customer c1 ;
- while(!inFile.eof())
- {
- inFile.read( (char *)&c1, sizeof(c1) );
- if(strcmp(c1.getUsername(), user)==0
- and strcmp(c1.getPassword(), pass)==0)
- {
- cout << "Logged in Successfully \n Welcome \n";
- cout << "Id\tname\tusername\tpassword\n";
- cout << c1.getId() << '\t' << c1.getName() << '\t' <<
- c1.getUsername() << '\t' << c1.getPassword() << "\n";
- return true;
- }
- }
- }
- else
- cout <<"Cann't be opened the file \n" ;
- return false;
- }
- string decryted(string s)
- {
- for(int i=0 ; i < 20 ; i++ )
- s[i]+='&';
- return s;
- }
- string incryted(string s)
- {
- for(int i=0 ; i < 20 ; i++ )
- s[i]-='&';
- return s;
- }
- void printCustomers()
- {
- ifstream inFile("data.txt", ios::in);
- if(inFile.is_open())
- {
- Customer c1 ;
- cout <<"ID\tName\tUsername\tpassword\n\n";
- while(!inFile.eof())
- {
- inFile.read((char *)&c1, sizeof(c1));
- string temp = c1.getPassword();
- if(!inFile.eof())
- cout <<c1.getId() <<'\t' << c1.getName() <<'\t' <<
- c1.getUsername() << '\t' << decryted(temp) << "\n";
- }
- }
- else cout <<"Cann't opened this file\n";
- inFile.close();
- }
- };
- int main()
- {
- Customer c1 ;
- cout <<"Uesrs:\n\n";
- c1.printCustomers();
- cout << "\n\n";
- ofstream out("data.txt", ios::out | ios::app );
- int choise = 0;
- cout << "1.new user sign up : \n2.old user log in : ";
- cout <<"\nEnter your choice : ";
- cin >> choise;
- if(choise==1) // new user (id , name , username, password)
- {
- c1.setID();
- c1.setName();
- c1.setUsername(1);
- c1.setPassword();
- out.write( (char*)&c1, sizeof(c1));
- }
- else
- {
- c1.setUsername(2);
- c1.setPassword();
- while(!c1.validLogin(c1.getUsername(), c1.getPassword()))
- {
- cout << "Invalid logging in \t please try again :\n";
- c1.setUsername(2);
- c1.setPassword();
- }
- }
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement