From 0abe0442747c81de05a334b05718c627ec552457 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 27 Jan 2023 15:54:10 +0100 Subject: [PATCH 1/7] Refactoring: add function for making code readable --- src/currentCustomerAccountBalance.c | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c index 1f87d76..9d2e365 100644 --- a/src/currentCustomerAccountBalance.c +++ b/src/currentCustomerAccountBalance.c @@ -13,20 +13,12 @@ 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)) { @@ -38,11 +30,33 @@ float getAvailableAccountBalance(int user_id) { 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("CustomerData.txt", "r"); + if(file == 0) { + printf("Erro: file cannot be opened!"); + return 0; + } + else { + availableBalance = readFileAndGetAvailableBalance(file, stringID); + } + + fclose(file); return availableBalance; From 8e0854cfc08a5e223bf8f081fb2718a52aee504a Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Sat, 28 Jan 2023 12:15:17 +0100 Subject: [PATCH 2/7] Refactoring: minimize code lines for easy understanding --- src/currentCustomerAccountBalance.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c index 9d2e365..9f415f9 100644 --- a/src/currentCustomerAccountBalance.c +++ b/src/currentCustomerAccountBalance.c @@ -25,10 +25,9 @@ float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]) { keep_reading = false; } else if(strstr(buffer, stringID)) { - fgets(buffer, MAX_LENGTH, file); - fgets(buffer, MAX_LENGTH, file); - fgets(buffer, MAX_LENGTH, file); - fgets(buffer, MAX_LENGTH, file); + for (int i = 0; i < 4; i++) { + fgets(buffer, MAX_LENGTH, file); + } strcpy(balance_String, buffer); balance = fetchBalanceFromBalanceString(balance_String); keep_reading = false; @@ -49,7 +48,7 @@ float getAvailableAccountBalance(int user_id) { FILE *file = fopen("CustomerData.txt", "r"); if(file == 0) { - printf("Erro: file cannot be opened!"); + printf("Error: customer data file cannot be opened!"); return 0; } else { From 09f153678101862aee97403481e6ac5b6f7cb6ab Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Sat, 28 Jan 2023 12:32:02 +0100 Subject: [PATCH 3/7] Add missing headers in currentCustomerAccountBalance.h --- src/currentCustomerAccountBalance.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/currentCustomerAccountBalance.h b/src/currentCustomerAccountBalance.h index 76725fe..0b4a148 100644 --- a/src/currentCustomerAccountBalance.h +++ b/src/currentCustomerAccountBalance.h @@ -4,4 +4,7 @@ #include #define MAX_LENGTH 100 -float getAvailableAccountBalance(int user_id); \ No newline at end of file + +float getAvailableAccountBalance(int user_id); +float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]); +float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]); \ No newline at end of file From 7147ad4ab10b72543b185271da49be6663388be9 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Wed, 1 Feb 2023 21:20:10 +0100 Subject: [PATCH 4/7] Add file to save information about customerData.txt --- src/_file_information.h | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/_file_information.h diff --git a/src/_file_information.h b/src/_file_information.h new file mode 100644 index 0000000..1cb5200 --- /dev/null +++ b/src/_file_information.h @@ -0,0 +1,2 @@ +#define MAX_LENGTH 100 +#define CUSTOMER_DATA_FILE "CustomerData.txt" \ No newline at end of file From 33ea6b25e75876cc8e15355c4661b39568076c61 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Wed, 1 Feb 2023 21:22:37 +0100 Subject: [PATCH 5/7] Update header files --- src/currentCustomerAccountBalance.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/currentCustomerAccountBalance.h b/src/currentCustomerAccountBalance.h index 0b4a148..2795450 100644 --- a/src/currentCustomerAccountBalance.h +++ b/src/currentCustomerAccountBalance.h @@ -3,7 +3,7 @@ #include #include -#define MAX_LENGTH 100 +#include "_file_information.h" float getAvailableAccountBalance(int user_id); float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]); From 2f729d8ff0500162c77bd1248d54528366d87ae9 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Wed, 1 Feb 2023 21:23:43 +0100 Subject: [PATCH 6/7] Refactoring: Use created constant name for file --- src/currentCustomerAccountBalance.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c index 9f415f9..a91e306 100644 --- a/src/currentCustomerAccountBalance.c +++ b/src/currentCustomerAccountBalance.c @@ -46,9 +46,9 @@ float getAvailableAccountBalance(int user_id) { strcat(stringID, user_id_as_string); // Now stringID is "ID=user_id" - FILE *file = fopen("CustomerData.txt", "r"); + FILE *file = fopen(CUSTOMER_DATA_FILE, "r"); if(file == 0) { - printf("Error: customer data file cannot be opened!"); + printf("Error: customer data file cannot be opened!\n"); return 0; } else { From b3688bbd1c3391bd76420fa5ea6ef1af85a7148d Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Wed, 1 Feb 2023 21:24:02 +0100 Subject: [PATCH 7/7] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 496ee2c..2608ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.vscode \ No newline at end of file