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();
int letterCounter = 0;
int letterMaximum = 15;
int errorResult = 0;
char userInput=' ';
bool inputTooLong = false, foundComma = false;
printf("To create a new user, enter the information required below.\n");
@ -30,14 +31,16 @@ void collectCustomerProperties()
}
}
if(inputTooLong){
errorMessage(-7);
errorResult = errorMessage(-7);
if(errorResult==-7) exit(-1);
}
else{
*(instance.forename+letterCounter) = '\0';
letterCounter = 0;
}
if(!isLetterOfAlphabet(instance.forename)){
errorMessage(-10);
errorResult = errorMessage(-10);
exit(-1);
}
printf("Enter surname (max. 15 letters):\n");
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){
@ -49,13 +52,15 @@ void collectCustomerProperties()
}
}
if(inputTooLong){
errorMessage(-8);
errorResult = errorMessage(-8);
if(errorResult==-8) exit(-1);
}else{
*(instance.surname+letterCounter) = '\0';
letterCounter = 0;
}
if(!isLetterOfAlphabet(instance.surname)){
errorMessage(-11);
errorResult = errorMessage(-11);
if(errorResult==-11) exit(-1);
}
printf("Enter password (max. 20 letters):\n");
letterMaximum = 20;
@ -68,7 +73,8 @@ void collectCustomerProperties()
}
}
if(inputTooLong){
errorMessage(-9);
errorResult=errorMessage(-9);
if(errorResult==-9) exit(-1);
}else{
*(instance.password+letterCounter) = '\0';
}
@ -86,7 +92,8 @@ void collectCustomerProperties()
}
}
if(inputTooLong){
errorMessage(-12);
errorResult = errorMessage(-12);
if(errorResult==-12) exit(-1);
}else{
if(!foundComma){
*(balanceCharacters+letterCounter) = '.';
@ -109,12 +116,14 @@ void collectCustomerProperties()
void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance)
{
FILE *customerData = fopen("CustomerData.txt","a");
int errorResult = 0;
if(customerData!=NULL){
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);
fclose(customerData);
}
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;
break;
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;
exit(-1);
break;
case -5:
puts("You should be at least 18 years old to create a bank account!");
@ -28,37 +27,30 @@ int errorMessage(int errorCode)
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;
case -10:
puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!");
returnValue = -10;
exit(-1);
break;
case -11:
puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!");
returnValue = -11;
exit(-1);
break;
case -12:
puts("You entered too many digits.");
returnValue = -12;
exit(-1);
break;
default:
puts("Error code unknown.");

12
src/loginCustomer.c

@ -12,6 +12,7 @@ void collectCustomerDataForLogin(int attempts)
c.password = calloc(20+1, sizeof(char));
int digitCharacterFromUser, passwordCharacterFromUser;
int IDLengthCounter = 0, passwordLengthCounter = 0;
int errorResult = 0;
const int IDMaxLength = 16, passwordMaxLength = 21;
printf("Enter ID:\n");
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);
collectCustomerDataForLogin(++attempts);
}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);
char *searchForThisString = generateCheckString(IDAsNumber,c->password);
char *lineFromCustomerFile = calloc(40,sizeof(char));
int errorResult = 0;
FILE *readCustomerFile = fopen("CustomerData.txt", "r");
if(readCustomerFile==NULL){
errorMessage(-4);
errorResult = errorMessage(-4);
if(errorResult==-4) exit(-1);
}
while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){
if(strcmp(searchForThisString,lineFromCustomerFile)==0){
@ -77,7 +79,7 @@ bool loginCustomer(customer_t *c)
fclose(readCustomerFile);
return foundCustomerEntryInFile;
}else{
errorMessage(-1);
errorResult = errorMessage(-1);
}
fclose(readCustomerFile);
return foundCustomerEntryInFile;

Loading…
Cancel
Save