diff --git a/.gitignore b/.gitignore index 496ee2c..dce188f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.swp diff --git a/build-project.sh b/build-project.sh old mode 100644 new mode 100755 index c75f560..3aac8dc --- a/build-project.sh +++ b/build-project.sh @@ -1,5 +1,13 @@ +trap 'echo "Interrupted"; rm main; cp employeeLogin.c.bak employeeLogin.c; rm employeeLogin.c.bak; cd ..; rm -r build/; exit' SIGINT clear +ceedling test:all cd src/ -gcc main.c -./a.out -rm a.out \ No newline at end of file +cp employeeLogin.c employeeLogin.c.bak +sed -i 's/src\///g' employeeLogin.c +gcc main.c mainMenu.c -o main +./main +rm main +cp employeeLogin.c.bak employeeLogin.c +rm employeeLogin.c.bak +cd .. +rm -r build/ diff --git a/src/employeeList.txt b/src/employeeList.txt new file mode 100644 index 0000000..94986be --- /dev/null +++ b/src/employeeList.txt @@ -0,0 +1,12 @@ +Atharva Atharvafdai7514 + +Can BlooMaskfdlt3817 + +Haytham TimoDLfdai7207 + +Julius Insertcatfdai7057 + +Mohamed MDfdai6618 + +Shivam Schivam007fdlt3781 + diff --git a/src/employeeLogin.c b/src/employeeLogin.c new file mode 100644 index 0000000..3182746 --- /dev/null +++ b/src/employeeLogin.c @@ -0,0 +1,132 @@ +#include "mainMenu.h" +#include "employeeLogin.h" + +bool employeesAccess(char* employeesAccessCode) +{ + + return !(strcmp(employeesAccessCode,employeeAccessKey)); + +} + +int checkEmployeeCredentials(char *inputUsername, char *inputPassword) +{ + + char* listUsername = malloc(credentialLength * sizeof(char*)); + char* listPassword = malloc(credentialLength * sizeof(char*)); + + FILE* employeeList = fopen("src/employeeList.txt","r"); + + if(employeeList == NULL ) + { + printf("file does not exist"); + exit(1); + } + else + { + /*loop that checks if the two strings seperated by space exist in the employee list*/ + + while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF) + { + + if (!(strcmp(inputUsername, listUsername)) && !(strcmp(inputPassword, listPassword))) + { + fclose(employeeList); + return 1; + } + else if(!(strcmp(inputUsername, listUsername)) && strcmp(inputPassword, listPassword)) + { + fclose(employeeList); + return 2; + } + } + fclose(employeeList); + return 0; + } + + free(inputUsername); + free(inputPassword); +} + +void getEmployeeAccessCode() +{ + char accessCode[10]; + + int remainingAttempts = 10; + + printf("\n\nPlease enter the Access key : "); + + while(remainingAttempts > 0) + { + scanf("%s",accessCode); + + if(employeesAccess(accessCode)) + { + printf("\n\nAccess granted!\n\n"); + + loginAsEmployee(); + break; + + } + + else + { + printf("\n\nAccess key didnt match! try again !\n\n"); + --remainingAttempts; + } + + if(remainingAttempts == 0) + { + printf("you've reached the maximum number of tries!\nplease contact an employee of a high clearance(2 or higher) \n\n"); + } + } + +} + +void getEmployeeCredentials(char* inputUsername,char* inputPassword) +{ + printf("Enter username: "); + scanf("%s", inputUsername); + printf("Enter password: "); + scanf("%s", inputPassword); +} + +void loginAsEmployee() +{ + int counter=3; + char* username = malloc(credentialLength * sizeof(char*)); + char* password = malloc(credentialLength * sizeof(char*)); + + while(counter>0) + { + getEmployeeCredentials(username, password); + + int checkCredentials = checkEmployeeCredentials(username,password); + + + if(checkCredentials == 0) + { + printf("\n\nUser not found\n\n"); + } + else if(checkCredentials == 2) + { + printf("\n\nWrong Informations !\nyou have %d tries left\n\n",counter-1); + --counter; + } + else + { + printf("\n\nUser Approved\n\n"); + break; + } + } + + if(counter==0) + { + + printf("you used up all of the tries! account locked\nPlease contact an employee of higher clearance !\n\n"); + + } + + free(username); + free(password); + +} diff --git a/src/employeeLogin.h b/src/employeeLogin.h new file mode 100644 index 0000000..d6f3314 --- /dev/null +++ b/src/employeeLogin.h @@ -0,0 +1,18 @@ +#ifndef LOGINEMPLOYEE_H_ +#define LOGINEMPLOYEE_H_ + +#define employeeAccessKey "DF9E9A8B5E" +#define credentialLength 20 + +#include + +bool employeesAccess(char* employeesAccessCode); + +int checkEmployeeCredentials(char* username , char* password); + +void getEmployeeCredentials(char* username, char* password); +void loginAsEmployee(); + +#endif + + diff --git a/src/main.c b/src/main.c index a6b9b24..7032d5b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,9 @@ -#include +#include "mainMenu.h" -int main() { +int main() +{ + + ageInput(); return 0; -} \ No newline at end of file +} diff --git a/src/mainMenu.c b/src/mainMenu.c new file mode 100644 index 0000000..b2d8243 --- /dev/null +++ b/src/mainMenu.c @@ -0,0 +1,133 @@ +#include "mainMenu.h" + +#include "employeeLogin.c" + +bool agePermission(int age) +{ + + return age >= 18; + +} + +bool checkIfInteger(char* userInput) +{ + + char *endPointer; + + /*the endPointer points to the first non-integer character signaled to stop conversion*/ + + strtol(userInput, &endPointer, 10); + + return !(endPointer == userInput || *endPointer != '\0'); + +} + +bool chooseOption(int choiceInput) +{ + + return !(choiceInput < 1 || choiceInput > 4); + +} + +void ageInput() +{ + + char* userInput = malloc(20*sizeof(char*)); + char* userInputPointer; + + long age; + + printf("\nPlease specify your age : "); + scanf("%s",userInput); + + + while (true) + { + /*the userInput string is changed to a number with the strtol function*/ + + age = strtol(userInput,&userInputPointer,10); + + if((checkIfInteger(userInput))&& (agePermission(age))) + { + + printf("Access granted!\n\n\n\n"); + + showMenu(); + + menuInput(); + + break; + + } + + else if((checkIfInteger(userInput)) && !(agePermission(age))) + { + + printf("You should be at least 18 years old to create a bank account!\n"); + + break; + + } + + else + { + printf("input invalid! try again!\n"); + + scanf("%s",userInput); + + } + + +} +} + +void menuInput() +{ + char choiceInput[20]; + char* choiceInputPointer; + int selection; + + scanf("%s", choiceInput); + selection = strtol(choiceInput, &choiceInputPointer, 10); + + while (!checkIfInteger(choiceInput) || !chooseOption(selection)) + { + printf("Input invalid! try again!\n"); + + scanf("%s", choiceInput); + + selection = strtol(choiceInput, &choiceInputPointer, 10); + } + + switch(selection) + { + case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + break; + + case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); + break; + + case 3 : getEmployeeAccessCode(); + break; + + case 4 : printf("\e[1;1H\e[2J"); + printf("\nsee you next time !\n\n"); + break; + } +} + + +void showMenu() +{ + + printf("\t\t\t\t\t\t\t Welcome to Bank Manager!"); + printf("\n\n\n\n\t\t\t\t\t\tPlease select one of the following functions!"); + printf("\n\n\n\n\t\t\t\t\t\t ->Login as an existing costumer."); + printf("\n\n\t\t\t\t\t\t ->Register as a new costumer."); + printf("\n\n\t\t\t\t\t\t ->Login as an Employee."); + printf("\n\n\t\t\t\t\t\t\t\t ->Exit.\n"); + printf("\n\n\n\n\n Selection :\n"); + +} + + diff --git a/src/mainMenu.h b/src/mainMenu.h new file mode 100644 index 0000000..17d81bc --- /dev/null +++ b/src/mainMenu.h @@ -0,0 +1,20 @@ +#ifndef MAINMENU_H_ +#define MAINMENU_H_ + +#include +#include +#include +#include + +#define credentialLength 20 + +bool agePermission(int age); +bool checkIfInteger(char* userInput); +bool chooseOption(int choiceInput); + +void ageInput(); +void showMenu(); +void menuInput(); + +#endif + diff --git a/test/support/.gitkeep b/test/support/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/test_employeeLogin.c b/test/test_employeeLogin.c new file mode 100644 index 0000000..8860af5 --- /dev/null +++ b/test/test_employeeLogin.c @@ -0,0 +1,147 @@ +#ifdef TEST + +#include "unity.h" + +#include "employeeLogin.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_SuccessfulLoginEmployee_(void) +{ + + //test case : 0 + + /*Arrange*/ + + char* validEmployeesCredentials[][2] = { + {"Atharva", "Atharvafdai7514"}, + {"Can", "BlooMaskfdlt3817"}, + {"Haytham", "TimoDLfdai7207"}, + {"Julius", "Insertcatfdai7057"}, + {"Mohamed", "MDfdai6618"}, + {"Shivam", "Schivam007fdlt3781"} + }; + /*Act and Assert*/ + + int expected[] = {1,1,1,1,1,1}; + + for(int i=0; i<6; i++) + { + + int result = checkEmployeeCredentials(validEmployeesCredentials[i][0], validEmployeesCredentials[i][1]); + + TEST_ASSERT_EQUAL_INT(expected[i],result); + + } + + + + +} + +void test_WrongInfosLoginEmployee(void) +{ + //test case : 1 + /*Arrange*/ + + char* wrongEmployeesCredentials[][2] = { + {"Atharva", "doe"}, + {"Can", "Bar"}, + {"Haytham", "buzz"}, + {"Julius", "fizz"}, + {"Mohamed", "muster"}, + {"Shivam", "TimoDL"} + }; + + /*Act and Assert*/ + + int expected[] = {2,2,2,2,2,2}; + + for(int i=0; i<6; i++) + { + + int result = checkEmployeeCredentials(wrongEmployeesCredentials[i][0], wrongEmployeesCredentials[i][1]); + + TEST_ASSERT_EQUAL_INT(expected[i],result); + + } + +} + + + + +void test_MissingLoginEmployee(void) +{ + + //test case : 2 + /*Arrange*/ + + char* missingEmployeesCredentials[][2] = { + {"John", "doe"}, + {"Jane", "Doe"}, + {"Foo", "Bar"}, + {"Fizz", "Buzz"}, + {"Musterman", "Mustermanpassword"}, + {"Musterfrau", "Musterfraupassword"} + }; + + int expected[] = {0,0,0,0,0,0}; + + /*Act and Assert*/ + + for(int i=0; i<6; i++) + { + + int result = checkEmployeeCredentials(missingEmployeesCredentials[i][0], missingEmployeesCredentials[i][1]); + + TEST_ASSERT_EQUAL_INT(expected[i],result); + + } + + +} + +void test_validEmployeeAccessCode(void) +{ + //test case 0 + + /*Arrange*/ + + char validAccesscode[11] = "DF9E9A8B5E"; + + /*Act*/ + bool validAccessCodeResult = employeesAccess(validAccesscode); + + /*Assert*/ + + TEST_ASSERT_TRUE(validAccessCodeResult); + +} + +void test_invalidEmployeeAccessCode(void) +{ + //test case 1 + + /*Arrange*/ + + char* invalidAccessCode[] = {"15","foo","fizz","buzz","fizzbuzz","test","bankmanagement"}; + bool invalidCodeExpectation = false; + + /*Act and assert*/ + + for(int i=0;i<7;i++) + { + bool invalidCodeResults = employeesAccess(invalidAccessCode[i]); + TEST_ASSERT_EQUAL(invalidCodeExpectation,invalidCodeResults); + } + +} + +#endif // TEST diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c new file mode 100644 index 0000000..4883180 --- /dev/null +++ b/test/test_mainMenu.c @@ -0,0 +1,219 @@ +#ifdef TEST + +#include "unity.h" + +#include "mainMenu.h" + +void setUp(void) +{ + +} + +void tearDown(void) +{ + +} + +void test_agePermissionValidAge(void) +{ + //Test case : 0 + + /*Arrange*/ + + int Age = 18; + + bool validAgeResult[83]; + + /*Act*/ + + for(int i =0; i < 83; i++){ + + validAgeResult[i]= agePermission(Age + i); + } + + + /*Assert*/ + + for(int i=0; i < 83; i++){ + + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + +} + +void test_agePermissionInvalidAge(void) +{ + + //Test case : 1 + + /*Arrange*/ + + int invalidAge[117]; + + bool invalidAgeResult[117]; + + + for(int i =-100; i < 18; i++) + { + + invalidAge[i+100]= i; + } + + /*Act*/ + + for(int i=0; i < 117; i++) + { + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + } + + /*Assert*/ + + for(int i=0; i < 117; i++) + { + + TEST_ASSERT_FALSE(invalidAgeResult[i]); + + } + +} + +void test_IsInteger(void) +{ + + //test case 0 + + /*Arrange*/ + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerExpected = true; + + /*Act and Assert*/ + + for(int i=0; i < 19; i++) + { + bool inputIsIntegerResult = checkIfInteger(inputIsInteger[i]); + + TEST_ASSERT_EQUAL(inputIsIntegerExpected,inputIsIntegerResult); + + } + +} + +void test_IsNotInteger(void) +{ + //test case 1 + + /*Arrange*/ + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + bool inputIsNotIntegerExpected = false; + + /*Act and Assert*/ + + + for(int i=0; i<19; i++) + { + bool inputIsNotIntegerResult = checkIfInteger(inputIsNotInteger[i]); + + TEST_ASSERT_EQUAL(inputIsNotIntegerExpected,inputIsNotIntegerResult); + + } + + +} +void test_validChoiceInput(void) +{ + //test case 0 + + /*Arrange*/ + + int validInput[4]; + + bool validInputExpected = true; + + + + for(int i = 0; i < 4; i++) + { + validInput[i] = i + 1; + } + + /*Act and Asssert*/ + + for(int i = 0; i < 4; i++) + { + + bool validInputResult = chooseOption(validInput[i]); + + TEST_ASSERT_EQUAL(validInputExpected,validInputResult); + } + +} + +void test_invalidChoiceInput_firstCase(void) +{ + // test case 1 + + /*Arrange*/ + + int invalidInput[100]; + + bool invalidInputExpected = false; + + + + for(int i = -100; i < 1; i++) + { + invalidInput[i+100] = i; + } + + /*Act and Assert*/ + + for(int i = 0; i < 100; i++) + { + + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); + + } + +} + +void test_invalidChoiceInput_secondCase(void) +{ + // test case 2 + + /*Arrange*/ + + int invalidInput[100]; + + bool invalidInputExpected = false; + + for(int i = 0; i < 100; i++) + { + invalidInput[i] = i + 5; + } + + /*Act and Assert*/ + + for(int i = 0; i < 100; i++) + { + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); + } + + +} + + + + + +#endif // TEST