Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define LINESIZE 200
  7. #define SIZE 40
  8. #define repricing_col 4
  9. #define opportunity_col 9
  10.  
  11.  
  12. int login_input(char* connected_as);
  13. int login(char* user, char* password);
  14. void make_array(char*** array, char* database_name, int database_columns);
  15.  
  16. /*EMPLOYEE MENU*/
  17. void employee_menu(char* connected_as); // Print Employee-Menu
  18. void print_database(char* db_name); // Open the Opportunity-Table Database and print it
  19. int search_existing_customer(); // return 0 / 1 if customer dose exist / dose not exist in the database
  20. void update_existing_customer(); // if customer dose existing in the database >> call this function
  21. void assign_new_customer(char* connected_as); // if customer dose not existing in the database >> call this function
  22. void todo_list_database(); // TODO-List Database
  23. void self_reports(char* name); // Employee Self-Reports
  24.  
  25. /*MANAGER MENU*/
  26. void manager_menu(char* connected_as);
  27. void print_database(char* db_name); // Open the Opportunity-Table Database and print it
  28. int search_existing_customer(); // return 0 / 1 if customer dose exist / dose not exist in the database
  29. void update_existing_customer(); // if customer dose existing in the database >> call this function
  30. void assign_new_customer(char* connected_as); // if customer dose not existing in the database >> call this function
  31. void todo_list_database(); // TODO-List Database
  32. void all_reports_menu(); // All Reports Menu to choose specific report
  33. void repricing_products(); // Repricing products in the database
  34.  
  35. /*All REPORTS MENU FOR MANAGER ONLY!*/
  36. void report_by_name(); // { scanf char* name->calls self_reports function }
  37. void new_customers_last_month(); // from Opportunity Table (MAYBE WE HAVE TO ADD 'DATE OF JOIN' IN THE CUSTOMER DETAILS)
  38. void new_customers_last_year();// from Opportunity Table (MAYBE WE HAVE TO ADD 'DATE OF JOIN' IN THE CUSTOMER DETAILS)
  39. void successful_deals_last_month(); // from Opportunity Table, Status: "Win"
  40. void successful_deals_last_year(); // from Opportunity Table, Status: "Win"
  41.  
  42.  
  43. int main() {
  44. // connected_as = (char*)malloc(SIZE * sizeof(char));
  45. // int log = login_input(connected_as);
  46. char* connected_as = NULL;
  47. char*** test = { "" };
  48. make_array(test, "opportunity_table.csv", 9);
  49. int log = 0;
  50. printf("check: 1 - employee /// 2 - manager: ");
  51. scanf("%d", &log);
  52. if (log == 1)
  53. employee_menu(connected_as);
  54. if (log == 2)
  55. manager_menu(connected_as);
  56. return 0;
  57. }
  58.  
  59. /*NOT COMPLITED!*/
  60. void all_reports_menu(char* connected_as) {
  61. int report_choice;
  62.  
  63. printf("%30.s");
  64. printf("**************************************************\n");
  65. printf("%30.s");
  66. printf("**************** ALL REPORTS MENU ****************\n");
  67. printf("%30.s");
  68. printf("**************************************************\n");
  69. printf("%30.s");
  70. printf("** 1. Report By Name of Employee **\n");
  71. printf("%30.s");
  72. printf("** 2. Last Month New Customers **\n");
  73. printf("%30.s");
  74. printf("** 3. Last Year New Customers **\n");
  75. printf("%30.s");
  76. printf("** 4. Last Month Successful Deals **\n");
  77. printf("%30.s");
  78. printf("** 5. Last Year Successful Deals **\n");
  79. printf("%30.s");
  80. printf("** 0. Go Back To Previous Menu **\n");
  81. printf("%30.s");
  82. printf("**************************************************\n\n");
  83.  
  84. printf("Enter Your Choice: ");
  85. scanf("%d", &report_choice);
  86.  
  87. while (report_choice < 0 || report_choice > 5 && (report_choice <= '/' || report_choice >= ':')) {
  88. printf("Wrong choice - Enter Your Choice: ");
  89. fseek(stdin, 0, SEEK_END);
  90. scanf("%d", &report_choice);
  91. }
  92. switch (report_choice) {
  93. case 1:
  94. /// report_by_name();
  95. break;
  96. case 2:
  97. /// new_customers_last_month();
  98. break;
  99. case 3:
  100. /// new_customers_last_year();
  101. break;
  102. case 4:
  103. /// successful_deals_last_month();
  104. break;
  105. case 5:
  106. /// successful_deals_last_year();
  107. break;
  108. case 0:
  109. manager_menu(connected_as);
  110. break;
  111. default:
  112.  
  113. break;
  114. }
  115. }
  116.  
  117. void manager_menu(char* connected_as) {
  118. int manager_choice;
  119. int customer_ID = 0;
  120. char*** database_array = NULL;
  121.  
  122.  
  123. printf("%30.s");
  124. printf("**************************************************\n");
  125. printf("%30.s");
  126. printf("****************** MANAGER MENU ******************\n");
  127. printf("%30.s");
  128. printf("**************************************************\n");
  129. printf("%30.s");
  130. printf("** 1. Print Opportunity Table **\n");
  131. printf("%30.s");
  132. printf("** 2. Search existing customer **\n");
  133. printf("%30.s");
  134. printf("** 3. TO-DO List **\n");
  135. printf("%30.s");
  136. printf("** 4. All Reports **\n");
  137. printf("%30.s");
  138. printf("** 5. Print Price List **\n");
  139. printf("%30.s");
  140. printf("** 6. Repricing Products **\n");
  141. printf("%30.s");
  142. printf("** 0. Exit **\n");
  143. printf("%30.s");
  144. printf("**************************************************\n\n");
  145.  
  146. printf("Enter Your Choice: ");
  147. scanf("%d", &manager_choice);
  148. while (manager_choice < 0 || manager_choice > 6 && (manager_choice <= '/' || manager_choice >= ':'))
  149. {
  150. printf("Wrong choice - Enter Your Choice: ");
  151. fseek(stdin, 0, SEEK_END);
  152. scanf("%d", &manager_choice);
  153. }
  154. switch (manager_choice) {
  155. case 0:
  156. printf("Good Bye");
  157. exit(0);
  158. break;
  159. case 1:
  160. print_database("opportunity_table.csv");
  161. break;
  162. case 2:
  163. /// printf("Enter customer's ID: ");
  164. /// scanf("%d", &customer_ID);
  165. /// if (search_existing_customer(customer_ID)) {
  166. /// update_customer_details(customer_ID);
  167. /// }
  168. /// assign_new_customer(char* connected_as);
  169. break;
  170. case 3:
  171. /// todo_list_database();
  172. break;
  173. case 4:
  174. all_reports_menu(connected_as);
  175. break;
  176. case 5:
  177. print_database("data_base_repricing.csv");
  178. break;
  179. case 6:
  180. /// repricing_products();
  181. break;
  182. default:
  183. break;
  184. }
  185.  
  186. }
  187.  
  188. void employee_menu() {
  189. int employee_choice;
  190. int customer_ID = 0;
  191. char*** database_array = NULL;
  192.  
  193. printf("%30.s");
  194. printf("***************************************************\n");
  195. printf("%30.s");
  196. printf("****************** EMPLOYEE MENU ******************\n");
  197. printf("%30.s");
  198. printf("***************************************************\n");
  199. printf("%30.s");
  200. printf("** 1. Opportunity Table **\n");
  201. printf("%30.s");
  202. printf("** 2. Search existing customer **\n");
  203. printf("%30.s");
  204. printf("** 3. TO-DO List **\n");
  205. printf("%30.s");
  206. printf("** 4. Self Reports **\n");
  207. printf("%30.s");
  208. printf("** 5. Print Price List **\n");
  209. printf("%30.s");
  210. printf("** 0. Exit **\n");
  211. printf("%30.s");
  212. printf("***************************************************\n\n");
  213.  
  214. printf("Enter Your Choice: ");
  215. scanf("%d", &employee_choice);
  216.  
  217. while (employee_choice < 0 || employee_choice > 5 && (employee_choice <= '/' || employee_choice >= ':')) {
  218. printf("Wrong choice - Enter Your Choice: ");
  219. fseek(stdin, 0, SEEK_END);
  220. scanf("%d", &employee_choice);
  221. }
  222. switch (employee_choice) {
  223. case 0:
  224. printf("Good Bye");
  225. exit(0);
  226. break;
  227. case 1:
  228. print_database("opportunity_table.csv");
  229. break;
  230. case 2:
  231. /// printf("Enter customer's ID: ");
  232. /// scanf("%d", &customer_ID);
  233. /// if (search_existing_customer(customer_ID)) {
  234. /// update_customer_details(customer_ID);
  235. /// }
  236. /// assign_new_customer(char* connected_as);
  237. break;
  238. case 3:
  239. /// todo_list_database();
  240. break;
  241. case 4:
  242. /// self_reports(/*employee name*/);
  243. break;
  244. case 5:
  245. print_database("data_base_repricing.csv");
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251.  
  252.  
  253. /*THOSE FUNCTIONS WORKS FINE!*/
  254. void print_database(char* db_name)
  255. {
  256. int i = 0;
  257. int numofrows = 0;
  258. char line[LINESIZE] = "";
  259. char *item = NULL;
  260. char *comma = NULL;
  261. int field = 0;
  262. char info_matrix[50][LINESIZE] = { "" };
  263. FILE* fp = fopen(db_name, "r");
  264.  
  265. if (!fp) {
  266. printf("File failed to open!\n");
  267. exit(1);
  268. }
  269.  
  270. char t = ' ';
  271. for (int u = 0; u < 3; ++u)
  272. fscanf(fp, "%c", &t);
  273.  
  274. while (fgets(line, LINESIZE, fp)) {
  275. strcpy(info_matrix[i], line);
  276. ++i;
  277. ++numofrows;
  278. }
  279.  
  280. if (db_name == "opportunity_table.csv") {
  281. int width[] = { 25,16,26,16,23,18,23,23,23 };
  282. for (int loop = 0; loop < numofrows; ++loop) {
  283. int field = 0;
  284. item = info_matrix[loop];
  285. while (*item && (comma = strpbrk(item, ",\n"))) {//pointer to each comma and the newline
  286. printf("%-*.*s", width[field], comma - item, item);
  287. item = comma + 1;//skip the comma or newline
  288. field++;
  289. }
  290. printf("\n");
  291. }
  292. }
  293.  
  294. if (db_name == "data_base_repricing.csv") {
  295. int width[] = { 25,16,26,13 };
  296.  
  297. for (int loop = 0; loop < numofrows; ++loop) {
  298. int field = 0;
  299. item = info_matrix[loop];
  300. while (*item && (comma = strpbrk(item, ",\n"))) {//pointer to each comma and the newline
  301. printf("%-*.*s", width[field], comma - item, item);
  302. item = comma + 1;//skip the comma or newline
  303. field++;
  304. }
  305. printf("\n");
  306. }
  307. }
  308. printf("\n\n");
  309. }
  310.  
  311. void make_array(char*** db_array, char* db_name, int db_columns) {
  312. int i = 0, j = 3, k = 0, l = 0, m = 0;
  313. char temp[250];
  314. int linesnum = 0;
  315. FILE* fp;
  316. char line[LINESIZE];
  317. fp = fopen(db_name, "r");
  318. if (!fp) {
  319. printf("File failed to open!\n");
  320. exit(1);
  321. }
  322. while (fgets(line, LINESIZE, fp))
  323. linesnum++;
  324. db_array = (char *)malloc(sizeof(char*)*linesnum);
  325. rewind(fp);
  326. for (i; i < linesnum; ++i) {
  327. db_array[i] = (char*)malloc(sizeof(char*)*db_columns);
  328. }
  329. while (fgets(line, LINESIZE, fp)) {
  330.  
  331. for (i = 0; i < db_columns; ++i) {
  332.  
  333. while (line[j] != ','&& line[j] != '\n')
  334. {
  335. temp[k] = line[j];
  336. ++k;
  337. ++j;
  338. }
  339. ++j;
  340. temp[k] = '\0';
  341. k = 0;
  342. db_array[l][i] = malloc(strlen(temp) + 1);
  343. strcpy(db_array[l][i], temp);
  344. }
  345. l++;
  346. j = 0;
  347. }
  348.  
  349. for (i = 0; i < linesnum; ++i) {
  350. for (j = 0; j < db_columns; ++j)
  351. printf("%s\t", db_array[i][j]);
  352. printf("\n");
  353. }
  354. fclose(fp);
  355. }
  356.  
  357. int login(char* user, char* password) {
  358. /*
  359. The function gets char* of username and password.
  360. The function returns: 1 if the user is a employee, 2 if manager,0 if there is a wrong login details.
  361. there needs to be a csv file where each row conatins 3 clumns: username,password and employee type.
  362. */
  363. FILE* fp;
  364. int i = 0;
  365. int j = 0;
  366. char line[LINESIZE];
  367. char fUser[LINESIZE];
  368. char fPassword[LINESIZE];
  369. char type[LINESIZE];
  370. fp = fopen("data_base_log_in.csv", "r");
  371. if (!fp) {
  372. printf("File failed to open!\n");
  373. exit(1);
  374. }
  375. while (fgets(line, LINESIZE, fp)) {//gets a row each time every clumn separated by comas
  376. i = 0;
  377. j = 0;
  378. while (line[i] != ',')
  379. {
  380. if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || (line[i] >= '0'&&line[i] <= '9')) {//gets username and clears junk
  381. fUser[j] = line[i];
  382. ++j;
  383. }
  384. ++i;
  385. }
  386. fUser[j] = '\0';
  387. if (strcmp(user, fUser) == 0)
  388. {
  389. j = 0;
  390. ++i;
  391. while (line[i] != ',') {//gets password from line
  392. fPassword[j] = line[i];
  393. ++j;
  394. ++i;
  395. }
  396. fPassword[j] = '\0';
  397.  
  398. if (strcmp(password, fPassword) == 0) {
  399. j = 0;
  400. ++i;
  401. while (line[i] != '\n') {//gets type from line
  402. type[j] = line[i];
  403. ++j;
  404. ++i;
  405. }
  406. type[j] = '\0';
  407. if (strcmp(type, "manager") == 0) {
  408. return 2;
  409. }
  410. if (strcmp(type, "employee") == 0) {
  411. return 1;
  412. }
  413. }
  414. }
  415. }
  416. return 0;
  417. }
  418.  
  419. int login_input(char* connected_as) {
  420. int log = 0;
  421. char user[LINESIZE];
  422. char password[LINESIZE];
  423. do
  424. {
  425. puts("Enter username:");
  426. gets(user);
  427. puts("Enter password:");
  428. gets(password);
  429. log = login(user, password);
  430. if (log == 0)
  431. puts("Wrong username or password try again");
  432. } while (log == 0);
  433. strcpy(connected_as, user);
  434. return log;
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement