diff --git a/build-script.sh b/build-script.sh index 13a9de2..fd72393 100755 --- a/build-script.sh +++ b/build-script.sh @@ -1,7 +1,7 @@ clear ceedling test:all cd src/ -gcc stringManipulation.c loginCustomer.c createCustomer.c mainMenu.c program.c -lm -o program.out +gcc stringManipulation.c error.c loginCustomer.c createCustomer.c mainMenu.c program.c -lm -o program.out ./program.out rm program.out cd .. diff --git a/project.yml b/project.yml index cc2e939..91cbc02 100644 --- a/project.yml +++ b/project.yml @@ -37,6 +37,7 @@ - src/createCustomer.* - src/customerLogin.* - src/stringManipulation.* + - src/error.* :support: - test/support :libraries: [] diff --git a/src/createCustomer.c b/src/createCustomer.c index 434c76f..d6226ee 100644 --- a/src/createCustomer.c +++ b/src/createCustomer.c @@ -30,8 +30,7 @@ void collectCustomerProperties() } } if(inputTooLong){ - printf("Forename too long. Aborting.\n"); - exit(-1); + errorMessage(-7); }else{ *(instance.forename+letterCounter) = '\0'; letterCounter = 0; @@ -46,8 +45,7 @@ void collectCustomerProperties() } } if(inputTooLong){ - printf("Surname too long. Aborting.\n"); - exit(-1); + errorMessage(-8); }else{ *(instance.surname+letterCounter) = '\0'; letterCounter = 0; @@ -62,8 +60,7 @@ void collectCustomerProperties() } } if(inputTooLong){ - printf("Password too long. Aborting.\n"); - exit(-1); + errorMessage(-9); } *(instance.password+letterCounter) = '\0'; letterCounter = 0; @@ -84,7 +81,6 @@ generateCheckString(referenceToCustomerInstance->ID, referenceToCustomerInstance fclose(customerData); } else{ - printf("Error when accessing the file.\n"); - exit(-1); + errorMessage(-6); } } diff --git a/src/createCustomer.h b/src/createCustomer.h index 25769d5..112d2ac 100644 --- a/src/createCustomer.h +++ b/src/createCustomer.h @@ -6,6 +6,7 @@ #include #include "customerProperties.h" #include "stringManipulation.h" +#include "error.h" int generateID(); void collectCustomerProperties(); void writeCustomerPropertiesToFile(customer_t *); diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..137b60c --- /dev/null +++ b/src/error.c @@ -0,0 +1,52 @@ +#include "error.h" + +int errorMessage(int errorCode) +{ + int returnValue=0; + switch(errorCode){ + case -1: + puts("Login not successful."); + returnValue = -1; + break; + case -2: + puts("Maximum number of attempts reached."); + returnValue = -2; + break; + case -3: + puts("No menu entry available for this number."); + returnValue = -3; + break; + case -4: + puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Aborting!"); + returnValue = -4; + exit(-1); + break; + case -5: + puts("You should be at least 18 years old to create a bank account!"); + returnValue = -5; + break; + case -6: + puts("Error when trying to open a file to create a customer account."); + returnValue = -6; + exit(-1); + break; + case -7: + puts("Forename too long. (length > 15 characters) Aborting!"); + returnValue = -7; + exit(-1); + break; + case -8: + puts("Surname too long. (length > 15 characters) Aborting!"); + returnValue = -8; + exit(-1); + break; + case -9: + puts("Password too long. (length > 20 characters) Aboring!"); + returnValue = -9; + exit(-1); + break; + default: + puts("Error code unknown."); + } + return returnValue; +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..867964a --- /dev/null +++ b/src/error.h @@ -0,0 +1,3 @@ +#include +#include +int errorMessage(int); diff --git a/src/loginCustomer.c b/src/loginCustomer.c index ace8fb6..74de573 100644 --- a/src/loginCustomer.c +++ b/src/loginCustomer.c @@ -45,14 +45,13 @@ void collectCustomerDataForLogin(int attempts) free(c.password); if(loginSuccessful ) { printf("Welcome!\n"); - //call menu() }else if(!loginSuccessful && attempts < MAX_LOGIN_ATTEMPTS){ printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts); collectCustomerDataForLogin(++attempts); }else{ printf("Maximum number of attempts reached. Program terminates.\n"); exit(-1); - //call error() + errorMessage(-2); } } @@ -64,9 +63,7 @@ bool loginCustomer(customer_t *c) char *lineFromCustomerFile = calloc(40,sizeof(char)); FILE *readCustomerFile = fopen("CustomerData.txt", "r"); if(readCustomerFile==NULL){ - printf("Could not find file. Aborting.\n"); - exit(-1); - //call error() + errorMessage(-4); } while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ if(strcmp(searchForThisString,lineFromCustomerFile)==0){ @@ -80,7 +77,7 @@ bool loginCustomer(customer_t *c) fclose(readCustomerFile); return foundCustomerEntryInFile; }else{ - printf("Login not successful.\n"); + errorMessage(-1); } fclose(readCustomerFile); return foundCustomerEntryInFile; diff --git a/src/loginCustomer.h b/src/loginCustomer.h index 9fd921b..9e5f1b0 100644 --- a/src/loginCustomer.h +++ b/src/loginCustomer.h @@ -6,6 +6,7 @@ #include /*#include "stringManipulation.h"*/ #include "createCustomer.h" +#include "error.h" #define MAX_LOGIN_ATTEMPTS 3 bool checkLogin(bool); void collectCustomerDataForLogin(int); diff --git a/src/mainMenu.c b/src/mainMenu.c index 60850ab..ba4d13b 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -62,9 +62,7 @@ void ageInput() else if((checkIfInteger(userInput)) && !(agePermission(age))) { - - printf("You should be at least 18 years old to create a bank account!\n"); - + errorMessage(-5); break; } diff --git a/src_2/Error.c b/src_2/Error.c deleted file mode 100644 index ba552a2..0000000 --- a/src_2/Error.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "Error.h" - -int error(int errorCode) -{ - int returnValue=0; - switch(errorCode){ - case -1: - puts("Login not successful."); - returnValue = -1; - break; - case -2: - puts("Maximum number of attempts reached."); - returnValue = -2; - break; - case -3: - puts("No menu entry available for this number."); - returnValue = -3; - break; - default: - puts("Error code unknown."); - } - return returnValue; -} diff --git a/src_2/Error.h b/src_2/Error.h deleted file mode 100644 index 5e83c4d..0000000 --- a/src_2/Error.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -int error(int); diff --git a/tests_2/test/test_Error.c b/test/test_Error.c similarity index 71% rename from tests_2/test/test_Error.c rename to test/test_Error.c index c4afd6d..d2aef22 100644 --- a/tests_2/test/test_Error.c +++ b/test/test_Error.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../src/error.c" void setUp(){} void tearDown(){} @@ -37,14 +37,14 @@ void test_error() /*act and assertions for invalid error codes*/ for(int i=0;i