Browse Source

Create function fetchBalanceFromBalanceString

remotes/origin/feature/update-current-customer-account-balance
Shivam Chaudhary 2 years ago
parent
commit
73cd6f9621
  1. 29
      src/currentCustomerAccountBalance.c

29
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;
}
Loading…
Cancel
Save