From 0a8d9c9699831b6f5db0493fb026387c39e4e240 Mon Sep 17 00:00:00 2001 From: fdlt3817 Date: Tue, 7 Feb 2023 14:55:28 +0100 Subject: [PATCH] Created a fail case for non-existent customers --- src/updateCustomerAccountBalance.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 7cb7d3e..6e6fddc 100644 --- a/src/updateCustomerAccountBalance.c +++ b/src/updateCustomerAccountBalance.c @@ -17,6 +17,9 @@ void troubleShoot(int errorCode){ break; 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);; - replaceBalanceInString(newBalance,currentLine); + fclose(file); + + if(foundCustomer){ + replaceBalanceInString(newBalance,currentLine); + }else{ + troubleShoot(4); + } + return true; }