Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <vector>
- #include <sstream>
- #include <string.h>
- #include <algorithm>
- #include <mysql.h>
- using namespace std;
- int validate_login(MYSQL *con,string uname,string pass,int ch)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- if(ch==1)
- query="SELECT *from ADMIN WHERE NAME= \""+uname+"\" AND PASSWORD= \""+pass+"\"";
- else
- query="SELECT *from TECHNICIAN WHERE TNAME= \""+uname+"\" AND TPASS= \""+pass+"\"";
- //cout<<query;
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- result=mysql_store_result(con);
- if(mysql_num_rows(result)>0)
- return 1;
- else
- return 0;
- }
- class Employee
- {
- protected:
- string name;
- string pass;
- public:
- Employee(string uname,string upass)
- {
- name=uname;
- pass=upass;
- }
- string getName() {return name;}
- string getPass() {return pass;}
- };
- class admin:public Employee
- {
- public:
- admin(string uname,string upass):Employee(uname,upass)
- {
- }
- void insert_tech(MYSQL *con,string uname,string pass)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- query="INSERT INTO TECHNICIAN(TNAME,TPASS) VALUE( \" " + uname+ "\",\""+pass+"\")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else cout<<"New Technician Added\n";
- }
- void insert_feature(MYSQL *con,string fname)
- {
- string query;
- query="INSERT INTO FEATURES (FNAME) VALUE( \"" + fname+"\")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else cout<<"New Feature Added\n";
- }
- void insert_activity(MYSQL *con,string aname)
- {
- string query;
- query="INSERT INTO ACTIVITIES (ANAME) VALUE( \"" + aname+ "\")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else cout<<"New Activity Added\n";
- }
- void link_feature(MYSQL *con,string tid,string fid)
- {
- string query;
- query="INSERT INTO TECH_FEAT (TID,FID) VALUE("+ tid+ ","+fid+")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else cout<<"New Feature Added to Technician\n";
- }
- void display_tech(MYSQL *con)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- int numfields,i;
- query="SELECT TID, TNAME FROM TECHNICIAN";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numfields=mysql_num_fields(result);
- while(row=mysql_fetch_row(result)){
- for(i = 0; i < numfields; i++){
- cout<<row[i]<<" ";
- }
- cout<<endl;
- }
- }
- }
- void display_feature(MYSQL *con)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- int numfields,i;
- query="SELECT FID, FNAME FROM FEATURES";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numfields=mysql_num_fields(result);
- while(row=mysql_fetch_row(result)){
- for(i = 0; i < numfields; i++){
- cout<<row[i]<<" ";
- }
- cout<<endl;
- }
- }
- }
- void display_feature_id(MYSQL *con,string fid)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- int numfields,i;
- query="SELECT FNAME FROM FEATURES WHERE FID="+fid;
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numfields=mysql_num_fields(result);
- while(row=mysql_fetch_row(result)){
- cout<<row[0]<<" ";
- cout<<endl;
- }
- }
- }
- void display_activity(MYSQL *con)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- int numfields,i;
- query="SELECT AID, ANAME FROM ACTIVITIES";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numfields=mysql_num_fields(result);
- while(row=mysql_fetch_row(result)){
- for(i = 0; i < numfields; i++){
- cout<<row[i]<<" ";
- }
- cout<<endl;
- }
- }
- }
- void link_activity(MYSQL *con,string tid,string aid,string date)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- int numrows,i;
- query="SELECT FID FROM ACT_FEAT AS T1 WHERE AID="+aid+" AND NOT EXISTS (SELECT * FROM TECH_FEAT WHERE FID=T1.FID AND TID="+tid+")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numrows=mysql_num_rows(result);
- if(numrows>0)
- {
- cout<<"Sorry!The following features are missing for the user:\n";
- while(row=mysql_fetch_row(result)){
- display_feature_id(con,string(row[0]));
- }
- }
- else
- {
- query="INSERT INTO TECH_ACT VALUE("+tid+","+aid+",\""+date+"\")";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else cout<<"Inserted new Activity for Technician";
- }
- }
- }
- };
- class tech:public Employee
- {
- public:
- tech(string uname,string upass):Employee(uname,upass)
- {
- }
- void show_act(MYSQL *con,string tname)
- {
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- string query;
- string tid;
- int numrows;
- query="SELECT TID FROM TECHNICIAN WHERE TNAME=\""+tname+"\"";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- row=mysql_fetch_row(result);
- tid=row[0];
- query="SELECT ANAME FROM TECH_ACT AS T1,ACTIVITIES AS T2 WHERE T1.AID=T2.AID AND T1.TID="+tid+" AND DOA=CURDATE()";
- if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
- else
- {
- result=mysql_store_result(con);
- numrows=mysql_num_rows(result);
- if(numrows==0)cout<<"No Activities Today\n";
- while(row=mysql_fetch_row(result)){
- cout<<row[0]<<endl;
- }
- }
- }
- }
- };
- int main()
- {
- MYSQL *con=mysql_init(NULL);
- MYSQL_RES *result=new MYSQL_RES;
- MYSQL_ROW row;
- int ch,ch2,chx;
- string uname;
- string pass;
- string date;
- string name;
- string tid,fid,aid;
- int check;
- if(mysql_real_connect(con, "localhost", "root", "toa", "CORP" , 0, NULL, 0))cout<<mysql_error(con);
- cout<<"Who are you?\n\t1.ADMINISTRATOR\n\t2.TECHNICIAN\n";
- cin>>ch;
- cout<<"Username:";
- cin>>uname;
- cout<<"Password:";
- cin>>pass;
- check=validate_login(con,uname,pass,ch);
- if(check)
- {
- if(ch==1)
- {
- //Creating object of Admin class
- admin adm(uname,pass);
- cout<<"\n\t1.Add new Technician\n\t2.Add new Feature\n\t3.Add new Activity\n\t4.Assign Feautures to Technicians\n\t5.Assign Activities to Technicians\n\t6.Log Out\n";
- cout<<"Enter your choice\n";
- cin>>ch2;
- switch(ch2)
- {
- case 1: cout<<"Technician Name: ";
- cin>>name;
- cout<<"Password: ";
- cin>>pass;
- adm.insert_tech(con,name,pass);
- break;
- case 2: cout<<"Feature Name: ";
- cin>>name;
- adm.insert_feature(con,name);
- break;
- case 3: cout<<"Activity Name: ";
- cin>>name;
- adm.insert_activity(con,name);
- break;
- case 4: adm.display_tech(con);
- cout<<"Technician id: ";
- cin>>tid;
- adm.display_feature(con);
- cin>>fid;
- adm.link_feature(con,tid,fid);
- break;
- case 5: adm.display_tech(con);
- cout<<"Technician id: ";
- cin>>tid;
- adm.display_activity(con);
- cin>>aid;
- cout<<"Enter date of activity\n";
- cin>>date;
- adm.link_activity(con,tid,aid,date);
- break;
- }
- }
- else if(ch==2)
- {
- //Creating technician object
- tech tch(uname,pass);
- cout<<"\n\t1.Show today's Activities\n\t2.Log out\n\t";
- cout<<"Enter your choice: ";
- cin>>ch2;
- switch(ch2)
- {
- case 1:tch.show_act(con,tch.getName());
- break;
- }
- }
- }
- else
- cout<<"Sorry!Incorrect credentials\n";
- }
Add Comment
Please, Sign In to add comment