Browse Source

Created a fail case for non-existent customers

remotes/origin/feature/transfer-money
fdlt3817 2 years ago
parent
commit
0a8d9c9699
  1. 19
      src/updateCustomerAccountBalance.c

19
src/updateCustomerAccountBalance.c

@ -17,6 +17,9 @@ void troubleShoot(int errorCode){
break; break;
case 3: case 3:
printf("Renaming of a file failed. Are you sure the bank management system has the required authorization to rename files?"); printf("Renaming of a file failed. Are you sure the bank management system has the required authorization to rename files?");
break;
case 4:
printf("Could not find the customer. Please contact customer support.");
break; break;
} }
} }
@ -61,12 +64,13 @@ void replaceBalanceInString(float replacementBalance, int currentLine) {
char balance_as_string[MAX_LENGTH]; char balance_as_string[MAX_LENGTH];
sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to string sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to string
strcat(newBalanceLine, balance_as_string); strcat(newBalanceLine, balance_as_string);
replaceLineInFile("CustomerData.txt",currentLine,newBalanceLine);
replaceLineInFile(CUSTOMER_DATA_FILE,currentLine,newBalanceLine);
} }
bool updateAvailableAccountBalance(int user_id, float newBalance){ bool updateAvailableAccountBalance(int user_id, float newBalance){
bool keep_reading = true; bool keep_reading = true;
bool foundCustomer = false;
char *buffer = malloc(MAX_LENGTH * sizeof(char)); char *buffer = malloc(MAX_LENGTH * sizeof(char));
char *stringID = malloc(MAX_LENGTH * sizeof(char)); char *stringID = malloc(MAX_LENGTH * sizeof(char));
@ -78,7 +82,7 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
sprintf(user_id_as_string, "%d", user_id); // converts user_id to string sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
strcat(stringID, user_id_as_string); strcat(stringID, user_id_as_string);
FILE *file = fopen("CustomerData.txt", "r");
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
//printf(stringID); //printf(stringID);
while(keep_reading) { while(keep_reading) {
fgets(buffer, MAX_LENGTH, file); fgets(buffer, MAX_LENGTH, file);
@ -93,10 +97,17 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
} }
strcpy(balance_String, buffer); strcpy(balance_String, buffer);
keep_reading = false; keep_reading = false;
foundCustomer=true;
} }
} }
fclose(file);;
replaceBalanceInString(newBalance,currentLine);
fclose(file);
if(foundCustomer){
replaceBalanceInString(newBalance,currentLine);
}else{
troubleShoot(4);
}
return true; return true;
} }
Loading…
Cancel
Save