Browse Source

Created a fail case for non-existent customers

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

17
src/updateCustomerAccountBalance.c

@ -18,6 +18,9 @@ void troubleShoot(int errorCode){
case 3:
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;
}
}
@ -61,12 +64,13 @@ void replaceBalanceInString(float replacementBalance, int currentLine) {
char balance_as_string[MAX_LENGTH];
sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to 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 keep_reading = true;
bool foundCustomer = false;
char *buffer = 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
strcat(stringID, user_id_as_string);
FILE *file = fopen("CustomerData.txt", "r");
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
//printf(stringID);
while(keep_reading) {
fgets(buffer, MAX_LENGTH, file);
@ -93,10 +97,17 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
}
strcpy(balance_String, buffer);
keep_reading = false;
foundCustomer=true;
}
}
fclose(file);;
fclose(file);
if(foundCustomer){
replaceBalanceInString(newBalance,currentLine);
}else{
troubleShoot(4);
}
return true;
}
Loading…
Cancel
Save