You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.8 KiB

  1. #include "employeeLogin.c"
  2. #include "createEmployeeAccount.h"
  3. bool isValidEmployeeID(const char* employeeId, int maximumStringLength)
  4. {
  5. int employeeIdLength = strlen(employeeId);
  6. for(int i=0;i<employeeIdLength;i++)
  7. {
  8. if(employeeId[i]==' ')
  9. {
  10. return false;
  11. }
  12. }
  13. return employeeIdLength <= maximumStringLength ;
  14. }
  15. bool isValidPassword( char *password, int minimumLength)
  16. {
  17. /*created a pointer(*stringpointer) which helps loop through characters of the string password*/
  18. char *stringpointer = password;
  19. bool letterFound = false, symbolFound = false, numberFound = false;
  20. while(*stringpointer!='\0')
  21. {
  22. if(isalpha(* stringpointer))
  23. {
  24. letterFound = true;
  25. }
  26. else if(isdigit(* stringpointer))
  27. {
  28. numberFound = true;
  29. }
  30. else if(ispunct(* stringpointer))
  31. {
  32. symbolFound = true;
  33. }
  34. if( strlen(password) >= minimumLength && letterFound && numberFound && symbolFound)
  35. {
  36. return true;
  37. }
  38. ++stringpointer;
  39. }
  40. return false;
  41. }
  42. int StringLengthCounter(char* string)
  43. {
  44. int characterCounter = 0;
  45. int i = 0;
  46. while(string[i] !='\0')
  47. {
  48. characterCounter++;
  49. ++i;
  50. }
  51. string[characterCounter] = '\0';
  52. return characterCounter;
  53. }
  54. bool verifyPassword(char* enteredPassword,char* passwordConfirmation)
  55. {
  56. if(strcmp(enteredPassword,passwordConfirmation)==0)
  57. {
  58. return true;
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. bool createNewEmployee(char* employeeId, char* employeePassword)
  66. {
  67. FILE* employeesFile;
  68. employeesFile = fopen("src/employeesCredentialsList.txt","a");
  69. if(employeesFile == NULL)
  70. {
  71. printf("Error: could not find the list of Employees");
  72. return false;
  73. }
  74. fprintf(employeesFile,"\n%s %s\n",employeeId,employeePassword);
  75. fclose(employeesFile);
  76. /*used the checkEmployeeCredentials to check if the new id and password are created successfully*/
  77. return checkEmployeeCredentials(employeeId, employeePassword);
  78. }
  79. void getNewEmployeeCredentials()
  80. {
  81. const int Length = 21;
  82. char employeeId[Length];
  83. const int minimumPasswordLength = 5;
  84. char employeePassword[Length];
  85. char passwordVerfication[Length];
  86. printf("please enter your wished Id :\n");
  87. /*Added the regular expression [^\n] so that the string keep on getting read until a newline '\n' is found*/
  88. scanf(" %[^\n]s",employeeId);
  89. employeeId[Length] = '\0';
  90. printf("\nplease enter your wished Password :\n");
  91. scanf("%s",employeePassword);
  92. employeePassword[strlen(employeePassword)] = '\0';
  93. printf("\nplease confirm your Password :\n");
  94. scanf("%s",passwordVerfication);
  95. passwordVerfication[strlen(employeePassword)] = '\0';
  96. if(verifyPassword(passwordVerfication,employeePassword))
  97. {
  98. if(isValidEmployeeID(employeeId,Length) && isValidPassword(employeePassword,minimumPasswordLength))
  99. {
  100. createNewEmployee(employeeId,employeePassword) ? printf("\n\n Account created successfully !\n\n") : printf("\n\n Could not create the Account please contact an employee of clearance 1 !\n\n");
  101. }
  102. else if(isValidEmployeeID(employeeId,Length) && !isValidPassword(employeePassword,minimumPasswordLength))
  103. {
  104. printf("Error : the entered password should be at least 5 characters long and should contain at least 1 digit, 1 alphabet and 1 symbol!");
  105. }
  106. else
  107. {
  108. printf("Error : the entered ID should contain a maximum of 20 letters");
  109. }
  110. }
  111. else
  112. {
  113. printf("the Verification password and the entered password dont match");
  114. }
  115. }