From 73cd6f96219d7366e073ad760b92c586c68c08dc Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 27 Jan 2023 12:20:03 +0100 Subject: [PATCH] Create function fetchBalanceFromBalanceString --- src/currentCustomerAccountBalance.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c index f11ea54..1f87d76 100644 --- a/src/currentCustomerAccountBalance.c +++ b/src/currentCustomerAccountBalance.c @@ -1,15 +1,28 @@ #include "currentCustomerAccountBalance.h" +float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]) { + float balance = 0; + char *token = strtok(balance_String, "="); // separates string to two parts + while (token != NULL) { + if (atoi(token) != 0) { + balance = atof(token); // converts string to float + break; + } + token = strtok(NULL, "="); + } + return balance; +} + float getAvailableAccountBalance(int user_id) { bool keep_reading = true; - float balance = 0; + float availableBalance = 0; char buffer[MAX_LENGTH]; char stringID[MAX_LENGTH] = "ID="; char user_id_as_string[MAX_LENGTH]; char balance_String[MAX_LENGTH]; - sprintf(user_id_as_string, "%d", user_id); + 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"); @@ -25,21 +38,13 @@ float getAvailableAccountBalance(int user_id) { fgets(buffer, MAX_LENGTH, file); fgets(buffer, MAX_LENGTH, file); strcpy(balance_String, buffer); - - char *token = strtok(balance_String, "="); - while (token != NULL) { - if (atoi(token) != 0) { - balance = atof(token); - break; - } - token = strtok(NULL, "="); - } + availableBalance = fetchBalanceFromBalanceString(balance_String); keep_reading = false; } } fclose(file); - return balance; + return availableBalance; } \ No newline at end of file