Guest User

Untitled

a guest
Jul 18th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <string.h>
  5. #include <algorithm>
  6. #include <mysql.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12.  
  13. int validate_login(MYSQL *con,string uname,string pass,int ch)
  14. {
  15. MYSQL_RES *result=new MYSQL_RES;
  16. MYSQL_ROW row;
  17. string query;
  18. if(ch==1)
  19. query="SELECT *from ADMIN WHERE NAME= \""+uname+"\" AND PASSWORD= \""+pass+"\"";
  20.  
  21. else
  22. query="SELECT *from TECHNICIAN WHERE TNAME= \""+uname+"\" AND TPASS= \""+pass+"\"";
  23.  
  24. //cout<<query;
  25. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  26. result=mysql_store_result(con);
  27. if(mysql_num_rows(result)>0)
  28. return 1;
  29. else
  30. return 0;
  31.  
  32. }
  33.  
  34. class Employee
  35. {
  36. protected:
  37. string name;
  38. string pass;
  39.  
  40. public:
  41. Employee(string uname,string upass)
  42. {
  43. name=uname;
  44. pass=upass;
  45. }
  46. string getName() {return name;}
  47. string getPass() {return pass;}
  48. };
  49.  
  50.  
  51. class admin:public Employee
  52. {
  53. public:
  54. admin(string uname,string upass):Employee(uname,upass)
  55. {
  56. }
  57.  
  58. void insert_tech(MYSQL *con,string uname,string pass)
  59. {
  60. MYSQL_RES *result=new MYSQL_RES;
  61. MYSQL_ROW row;
  62. string query;
  63. query="INSERT INTO TECHNICIAN(TNAME,TPASS) VALUE( \" " + uname+ "\",\""+pass+"\")";
  64. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  65. else cout<<"New Technician Added\n";
  66. }
  67.  
  68.  
  69. void insert_feature(MYSQL *con,string fname)
  70. {
  71. string query;
  72. query="INSERT INTO FEATURES (FNAME) VALUE( \"" + fname+"\")";
  73. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  74. else cout<<"New Feature Added\n";
  75. }
  76.  
  77.  
  78. void insert_activity(MYSQL *con,string aname)
  79. {
  80. string query;
  81. query="INSERT INTO ACTIVITIES (ANAME) VALUE( \"" + aname+ "\")";
  82. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  83. else cout<<"New Activity Added\n";
  84. }
  85.  
  86. void link_feature(MYSQL *con,string tid,string fid)
  87. {
  88. string query;
  89. query="INSERT INTO TECH_FEAT (TID,FID) VALUE("+ tid+ ","+fid+")";
  90. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  91. else cout<<"New Feature Added to Technician\n";
  92. }
  93.  
  94. void display_tech(MYSQL *con)
  95. {
  96. MYSQL_RES *result=new MYSQL_RES;
  97. MYSQL_ROW row;
  98. string query;
  99. int numfields,i;
  100. query="SELECT TID, TNAME FROM TECHNICIAN";
  101. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  102. else
  103. {
  104. result=mysql_store_result(con);
  105. numfields=mysql_num_fields(result);
  106. while(row=mysql_fetch_row(result)){
  107. for(i = 0; i < numfields; i++){
  108. cout<<row[i]<<" ";
  109. }
  110.  
  111. cout<<endl;
  112. }
  113. }
  114. }
  115.  
  116.  
  117. void display_feature(MYSQL *con)
  118. {
  119. MYSQL_RES *result=new MYSQL_RES;
  120. MYSQL_ROW row;
  121. string query;
  122. int numfields,i;
  123. query="SELECT FID, FNAME FROM FEATURES";
  124. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  125. else
  126. {
  127. result=mysql_store_result(con);
  128. numfields=mysql_num_fields(result);
  129. while(row=mysql_fetch_row(result)){
  130. for(i = 0; i < numfields; i++){
  131. cout<<row[i]<<" ";
  132. }
  133.  
  134. cout<<endl;
  135. }
  136. }
  137. }
  138.  
  139.  
  140. void display_feature_id(MYSQL *con,string fid)
  141. {
  142. MYSQL_RES *result=new MYSQL_RES;
  143. MYSQL_ROW row;
  144. string query;
  145. int numfields,i;
  146. query="SELECT FNAME FROM FEATURES WHERE FID="+fid;
  147. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  148. else
  149. {
  150. result=mysql_store_result(con);
  151. numfields=mysql_num_fields(result);
  152. while(row=mysql_fetch_row(result)){
  153.  
  154. cout<<row[0]<<" ";
  155. cout<<endl;
  156. }
  157. }
  158. }
  159.  
  160.  
  161. void display_activity(MYSQL *con)
  162. {
  163. MYSQL_RES *result=new MYSQL_RES;
  164. MYSQL_ROW row;
  165. string query;
  166. int numfields,i;
  167. query="SELECT AID, ANAME FROM ACTIVITIES";
  168. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  169. else
  170. {
  171. result=mysql_store_result(con);
  172. numfields=mysql_num_fields(result);
  173. while(row=mysql_fetch_row(result)){
  174. for(i = 0; i < numfields; i++){
  175. cout<<row[i]<<" ";
  176. }
  177.  
  178. cout<<endl;
  179. }
  180. }
  181. }
  182.  
  183.  
  184. void link_activity(MYSQL *con,string tid,string aid,string date)
  185. {
  186. MYSQL_RES *result=new MYSQL_RES;
  187. MYSQL_ROW row;
  188. string query;
  189. int numrows,i;
  190. 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+")";
  191. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  192. else
  193. {
  194. result=mysql_store_result(con);
  195. numrows=mysql_num_rows(result);
  196. if(numrows>0)
  197. {
  198. cout<<"Sorry!The following features are missing for the user:\n";
  199. while(row=mysql_fetch_row(result)){
  200.  
  201. display_feature_id(con,string(row[0]));
  202. }
  203. }
  204. else
  205. {
  206. query="INSERT INTO TECH_ACT VALUE("+tid+","+aid+",\""+date+"\")";
  207. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  208. else cout<<"Inserted new Activity for Technician";
  209.  
  210.  
  211. }
  212. }
  213. }
  214.  
  215.  
  216.  
  217. };
  218.  
  219.  
  220. class tech:public Employee
  221. {
  222. public:
  223. tech(string uname,string upass):Employee(uname,upass)
  224. {
  225. }
  226.  
  227. void show_act(MYSQL *con,string tname)
  228. {
  229. MYSQL_RES *result=new MYSQL_RES;
  230. MYSQL_ROW row;
  231. string query;
  232. string tid;
  233. int numrows;
  234. query="SELECT TID FROM TECHNICIAN WHERE TNAME=\""+tname+"\"";
  235. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  236. else
  237. {
  238. result=mysql_store_result(con);
  239. row=mysql_fetch_row(result);
  240. tid=row[0];
  241. query="SELECT ANAME FROM TECH_ACT AS T1,ACTIVITIES AS T2 WHERE T1.AID=T2.AID AND T1.TID="+tid+" AND DOA=CURDATE()";
  242. if(mysql_query(con,query.c_str()))cout<<mysql_error(con);
  243. else
  244. {
  245.  
  246. result=mysql_store_result(con);
  247. numrows=mysql_num_rows(result);
  248. if(numrows==0)cout<<"No Activities Today\n";
  249. while(row=mysql_fetch_row(result)){
  250. cout<<row[0]<<endl;
  251. }
  252. }
  253.  
  254. }
  255.  
  256.  
  257.  
  258.  
  259. }
  260. };
  261.  
  262.  
  263. int main()
  264. {
  265. MYSQL *con=mysql_init(NULL);
  266. MYSQL_RES *result=new MYSQL_RES;
  267. MYSQL_ROW row;
  268.  
  269. int ch,ch2,chx;
  270. string uname;
  271. string pass;
  272. string date;
  273. string name;
  274. string tid,fid,aid;
  275. int check;
  276.  
  277. if(mysql_real_connect(con, "localhost", "root", "toa", "CORP" , 0, NULL, 0))cout<<mysql_error(con);
  278.  
  279. cout<<"Who are you?\n\t1.ADMINISTRATOR\n\t2.TECHNICIAN\n";
  280. cin>>ch;
  281.  
  282. cout<<"Username:";
  283. cin>>uname;
  284. cout<<"Password:";
  285. cin>>pass;
  286.  
  287. check=validate_login(con,uname,pass,ch);
  288. if(check)
  289. {
  290. if(ch==1)
  291. {
  292. //Creating object of Admin class
  293. admin adm(uname,pass);
  294. 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";
  295. cout<<"Enter your choice\n";
  296. cin>>ch2;
  297. switch(ch2)
  298. {
  299. case 1: cout<<"Technician Name: ";
  300. cin>>name;
  301. cout<<"Password: ";
  302. cin>>pass;
  303. adm.insert_tech(con,name,pass);
  304. break;
  305.  
  306. case 2: cout<<"Feature Name: ";
  307. cin>>name;
  308. adm.insert_feature(con,name);
  309. break;
  310.  
  311. case 3: cout<<"Activity Name: ";
  312. cin>>name;
  313. adm.insert_activity(con,name);
  314. break;
  315.  
  316. case 4: adm.display_tech(con);
  317. cout<<"Technician id: ";
  318. cin>>tid;
  319. adm.display_feature(con);
  320. cin>>fid;
  321. adm.link_feature(con,tid,fid);
  322. break;
  323.  
  324. case 5: adm.display_tech(con);
  325. cout<<"Technician id: ";
  326. cin>>tid;
  327. adm.display_activity(con);
  328. cin>>aid;
  329. cout<<"Enter date of activity\n";
  330. cin>>date;
  331. adm.link_activity(con,tid,aid,date);
  332. break;
  333. }
  334.  
  335.  
  336. }
  337.  
  338. else if(ch==2)
  339. {
  340. //Creating technician object
  341. tech tch(uname,pass);
  342. cout<<"\n\t1.Show today's Activities\n\t2.Log out\n\t";
  343. cout<<"Enter your choice: ";
  344. cin>>ch2;
  345. switch(ch2)
  346. {
  347. case 1:tch.show_act(con,tch.getName());
  348. break;
  349.  
  350.  
  351. }
  352.  
  353.  
  354.  
  355. }
  356.  
  357. }
  358. else
  359. cout<<"Sorry!Incorrect credentials\n";
  360.  
  361. }
Add Comment
Please, Sign In to add comment