|
|
@ -1,7 +1,8 @@ |
|
|
|
#include "updateCustomerAccountBalance.h" |
|
|
|
#include "currentCustomerAccountBalance.c" |
|
|
|
#include "lineReplacer.h" |
|
|
|
|
|
|
|
void deleteLineFromFile(const char* file_name, int line, const char* new_line){ |
|
|
|
void replaceLineInFile(const char* file_name, int line, const char* new_line){ |
|
|
|
FILE* file = fopen(file_name, "r"); |
|
|
|
if (file == NULL) { |
|
|
|
printf("Error opening file!\n"); |
|
|
@ -39,10 +40,10 @@ void replaceBalanceInString(float replacementBalance, int currentLine) { |
|
|
|
char balance_as_string[MAX_LENGTH]; |
|
|
|
sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string |
|
|
|
strcat(newBalanceLine, balance_as_string); |
|
|
|
deleteLineFromFile("CustomerData.txt",currentLine,newBalanceLine); |
|
|
|
replaceLineInFile("CustomerData.txt",currentLine,newBalanceLine); |
|
|
|
} |
|
|
|
|
|
|
|
bool updateAvailableAccountBalance(int user_id, float changeInBalance){ |
|
|
|
bool updateAvailableAccountBalance(int user_id, float changeInBalance, bool positive){ |
|
|
|
|
|
|
|
bool keep_reading = true; |
|
|
|
float availableBalance = 0; |
|
|
@ -71,19 +72,19 @@ bool updateAvailableAccountBalance(int user_id, float changeInBalance){ |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
strcpy(balance_String, buffer); |
|
|
|
currentLine+=4; |
|
|
|
availableBalance = fetchBalanceFromBalanceString(balance_String); |
|
|
|
|
|
|
|
availableBalance = fetchBalanceFromBalanceString(balance_String); |
|
|
|
keep_reading = false; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
fclose(file); |
|
|
|
replaceBalanceInString(availableBalance+changeInBalance,currentLine); |
|
|
|
float newBalance = (positive) ? availableBalance+changeInBalance : availableBalance-changeInBalance; |
|
|
|
replaceBalanceInString(newBalance,currentLine); |
|
|
|
} |
|
|
|
|
|
|
|
int main(){ |
|
|
|
|
|
|
|
updateAvailableAccountBalance(1234,500); |
|
|
|
updateAvailableAccountBalance(1234,500, true); |
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|