Browse Source

Merge branch 'feature/get-current-customer-account-balance' into feature/update-current-customer-account-balance

remotes/origin/feature/update-current-customer-account-balance
fdlt3817 2 years ago
parent
commit
6406909368
  1. 1
      .gitignore
  2. 2
      src/_file_information.h
  3. 41
      src/currentCustomerAccountBalance.c
  4. 5
      src/currentCustomerAccountBalance.h

1
.gitignore

@ -1 +1,2 @@
.DS_Store
.vscode

2
src/_file_information.h

@ -0,0 +1,2 @@
#define MAX_LENGTH 100
#define CUSTOMER_DATA_FILE "CustomerData.txt"

41
src/currentCustomerAccountBalance.c

@ -13,36 +13,49 @@ float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]) {
return balance;
}
float getAvailableAccountBalance(int user_id) {
float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]) {
float balance = 0;
bool keep_reading = true;
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); // converts user_id to string
strcat(stringID, user_id_as_string);
FILE *file = fopen("CustomerData.txt", "r");
while(keep_reading) {
fgets(buffer, MAX_LENGTH, file);
if (feof(file)) {
keep_reading = false;
}
else if(strstr(buffer, stringID)) {
for (int i = 0; i < 4; i++) {
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
}
strcpy(balance_String, buffer);
availableBalance = fetchBalanceFromBalanceString(balance_String);
balance = fetchBalanceFromBalanceString(balance_String);
keep_reading = false;
}
}
return balance;
}
float getAvailableAccountBalance(int user_id) {
float availableBalance = 0;
char stringID[MAX_LENGTH] = "ID=";
char user_id_as_string[MAX_LENGTH];
sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
strcat(stringID, user_id_as_string);
// Now stringID is "ID=user_id"
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
if(file == 0) {
printf("Error: customer data file cannot be opened!\n");
return 0;
}
else {
availableBalance = readFileAndGetAvailableBalance(file, stringID);
}
fclose(file);
return availableBalance;

5
src/currentCustomerAccountBalance.h

@ -3,5 +3,8 @@
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH 100
#include "_file_information.h"
float getAvailableAccountBalance(int user_id);
float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]);
float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]);
Loading…
Cancel
Save