Browse Source

Add new cases inside the switch of the function errorMessage(). Remove the call to the function exit() inside errorMessage(). It is now done by the function that calls it. Now the different return values of this function make sense.

remotes/origin/development
fdai7057 2 years ago
parent
commit
1d3092544f
  1. 23
      src/createCustomer.c
  2. 10
      src/error.c
  3. 12
      src/loginCustomer.c

23
src/createCustomer.c

@ -17,6 +17,7 @@ void collectCustomerProperties()
instance.ID = generateID(); instance.ID = generateID();
int letterCounter = 0; int letterCounter = 0;
int letterMaximum = 15; int letterMaximum = 15;
int errorResult = 0;
char userInput=' '; char userInput=' ';
bool inputTooLong = false, foundComma = false; bool inputTooLong = false, foundComma = false;
printf("To create a new user, enter the information required below.\n"); printf("To create a new user, enter the information required below.\n");
@ -30,14 +31,16 @@ void collectCustomerProperties()
} }
} }
if(inputTooLong){ if(inputTooLong){
errorMessage(-7);
errorResult = errorMessage(-7);
if(errorResult==-7) exit(-1);
} }
else{ else{
*(instance.forename+letterCounter) = '\0'; *(instance.forename+letterCounter) = '\0';
letterCounter = 0; letterCounter = 0;
} }
if(!isLetterOfAlphabet(instance.forename)){ if(!isLetterOfAlphabet(instance.forename)){
errorMessage(-10);
errorResult = errorMessage(-10);
exit(-1);
} }
printf("Enter surname (max. 15 letters):\n"); printf("Enter surname (max. 15 letters):\n");
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){
@ -49,13 +52,15 @@ void collectCustomerProperties()
} }
} }
if(inputTooLong){ if(inputTooLong){
errorMessage(-8);
errorResult = errorMessage(-8);
if(errorResult==-8) exit(-1);
}else{ }else{
*(instance.surname+letterCounter) = '\0'; *(instance.surname+letterCounter) = '\0';
letterCounter = 0; letterCounter = 0;
} }
if(!isLetterOfAlphabet(instance.surname)){ if(!isLetterOfAlphabet(instance.surname)){
errorMessage(-11);
errorResult = errorMessage(-11);
if(errorResult==-11) exit(-1);
} }
printf("Enter password (max. 20 letters):\n"); printf("Enter password (max. 20 letters):\n");
letterMaximum = 20; letterMaximum = 20;
@ -68,7 +73,8 @@ void collectCustomerProperties()
} }
} }
if(inputTooLong){ if(inputTooLong){
errorMessage(-9);
errorResult=errorMessage(-9);
if(errorResult==-9) exit(-1);
}else{ }else{
*(instance.password+letterCounter) = '\0'; *(instance.password+letterCounter) = '\0';
} }
@ -86,7 +92,8 @@ void collectCustomerProperties()
} }
} }
if(inputTooLong){ if(inputTooLong){
errorMessage(-12);
errorResult = errorMessage(-12);
if(errorResult==-12) exit(-1);
}else{ }else{
if(!foundComma){ if(!foundComma){
*(balanceCharacters+letterCounter) = '.'; *(balanceCharacters+letterCounter) = '.';
@ -109,12 +116,14 @@ void collectCustomerProperties()
void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance) void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance)
{ {
FILE *customerData = fopen("CustomerData.txt","a"); FILE *customerData = fopen("CustomerData.txt","a");
int errorResult = 0;
if(customerData!=NULL){ if(customerData!=NULL){
fprintf(customerData,"%s\nID=%d\nForename=%s\nSurname=%s\nPassword=%s\nBalance=%.4f€\n\n", fprintf(customerData,"%s\nID=%d\nForename=%s\nSurname=%s\nPassword=%s\nBalance=%.4f€\n\n",
generateCheckString(referenceToCustomerInstance->ID, referenceToCustomerInstance->password),referenceToCustomerInstance->ID,referenceToCustomerInstance->forename,referenceToCustomerInstance->surname,referenceToCustomerInstance->password, referenceToCustomerInstance->balance); generateCheckString(referenceToCustomerInstance->ID, referenceToCustomerInstance->password),referenceToCustomerInstance->ID,referenceToCustomerInstance->forename,referenceToCustomerInstance->surname,referenceToCustomerInstance->password, referenceToCustomerInstance->balance);
fclose(customerData); fclose(customerData);
} }
else{ else{
errorMessage(-6);
errorResult = errorMessage(-6);
if(errorResult==-6) exit(-1);
} }
} }

10
src/error.c

@ -17,9 +17,8 @@ int errorMessage(int errorCode)
returnValue = -3; returnValue = -3;
break; break;
case -4: case -4:
puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Aborting!");
puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Without users there is no file. Aborting!");
returnValue = -4; returnValue = -4;
exit(-1);
break; break;
case -5: case -5:
puts("You should be at least 18 years old to create a bank account!"); puts("You should be at least 18 years old to create a bank account!");
@ -28,37 +27,30 @@ int errorMessage(int errorCode)
case -6: case -6:
puts("Error when trying to open a file to create a customer account."); puts("Error when trying to open a file to create a customer account.");
returnValue = -6; returnValue = -6;
exit(-1);
break; break;
case -7: case -7:
puts("Forename too long. (length > 15 characters) Aborting!"); puts("Forename too long. (length > 15 characters) Aborting!");
returnValue = -7; returnValue = -7;
exit(-1);
break; break;
case -8: case -8:
puts("Surname too long. (length > 15 characters) Aborting!"); puts("Surname too long. (length > 15 characters) Aborting!");
returnValue = -8; returnValue = -8;
exit(-1);
break; break;
case -9: case -9:
puts("Password too long. (length > 20 characters) Aboring!"); puts("Password too long. (length > 20 characters) Aboring!");
returnValue = -9; returnValue = -9;
exit(-1);
break; break;
case -10: case -10:
puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!"); puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!");
returnValue = -10; returnValue = -10;
exit(-1);
break; break;
case -11: case -11:
puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!"); puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!");
returnValue = -11; returnValue = -11;
exit(-1);
break; break;
case -12: case -12:
puts("You entered too many digits."); puts("You entered too many digits.");
returnValue = -12; returnValue = -12;
exit(-1);
break; break;
default: default:
puts("Error code unknown."); puts("Error code unknown.");

12
src/loginCustomer.c

@ -12,6 +12,7 @@ void collectCustomerDataForLogin(int attempts)
c.password = calloc(20+1, sizeof(char)); c.password = calloc(20+1, sizeof(char));
int digitCharacterFromUser, passwordCharacterFromUser; int digitCharacterFromUser, passwordCharacterFromUser;
int IDLengthCounter = 0, passwordLengthCounter = 0; int IDLengthCounter = 0, passwordLengthCounter = 0;
int errorResult = 0;
const int IDMaxLength = 16, passwordMaxLength = 21; const int IDMaxLength = 16, passwordMaxLength = 21;
printf("Enter ID:\n"); printf("Enter ID:\n");
while((digitCharacterFromUser=getchar())!='\n'&&IDLengthCounter<IDMaxLength){ while((digitCharacterFromUser=getchar())!='\n'&&IDLengthCounter<IDMaxLength){
@ -49,9 +50,8 @@ void collectCustomerDataForLogin(int attempts)
printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts); printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts);
collectCustomerDataForLogin(++attempts); collectCustomerDataForLogin(++attempts);
}else{ }else{
printf("Maximum number of attempts reached. Program terminates.\n");
exit(-1);
errorMessage(-2);
errorResult = errorMessage(-2);
if(errorResult==-2) exit(-1);
} }
} }
@ -61,9 +61,11 @@ bool loginCustomer(customer_t *c)
unsigned int IDAsNumber = toUnsignedInteger(c->IDAsString); unsigned int IDAsNumber = toUnsignedInteger(c->IDAsString);
char *searchForThisString = generateCheckString(IDAsNumber,c->password); char *searchForThisString = generateCheckString(IDAsNumber,c->password);
char *lineFromCustomerFile = calloc(40,sizeof(char)); char *lineFromCustomerFile = calloc(40,sizeof(char));
int errorResult = 0;
FILE *readCustomerFile = fopen("CustomerData.txt", "r"); FILE *readCustomerFile = fopen("CustomerData.txt", "r");
if(readCustomerFile==NULL){ if(readCustomerFile==NULL){
errorMessage(-4);
errorResult = errorMessage(-4);
if(errorResult==-4) exit(-1);
} }
while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){
if(strcmp(searchForThisString,lineFromCustomerFile)==0){ if(strcmp(searchForThisString,lineFromCustomerFile)==0){
@ -77,7 +79,7 @@ bool loginCustomer(customer_t *c)
fclose(readCustomerFile); fclose(readCustomerFile);
return foundCustomerEntryInFile; return foundCustomerEntryInFile;
}else{ }else{
errorMessage(-1);
errorResult = errorMessage(-1);
} }
fclose(readCustomerFile); fclose(readCustomerFile);
return foundCustomerEntryInFile; return foundCustomerEntryInFile;

Loading…
Cancel
Save