diff --git a/src/CustomerData.txt b/src/CustomerData.txt index 05ec80e..d5c417b 100644 --- a/src/CustomerData.txt +++ b/src/CustomerData.txt @@ -3,4 +3,4 @@ ID=1234 forename=Max Surname=Mustermann password=example -balance=1000 \ No newline at end of file +balance=2000 \ No newline at end of file diff --git a/src/a.exe b/src/a.exe index 4083994..f28e6b3 100644 Binary files a/src/a.exe and b/src/a.exe differ diff --git a/src/lineReplacer.h b/src/lineReplacer.h new file mode 100644 index 0000000..999855c --- /dev/null +++ b/src/lineReplacer.h @@ -0,0 +1,6 @@ +#include +#include +#include +#include + +void replaceLineInFile(const char* file_name, int line, const char* new_line); //replaces the line at "line" on the file "file_name", with the new line "new_line". \ No newline at end of file diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 6ee64e9..764c317 100644 --- a/src/updateCustomerAccountBalance.c +++ b/src/updateCustomerAccountBalance.c @@ -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; } diff --git a/src/updateCustomerAccountBalance.h b/src/updateCustomerAccountBalance.h index 3a4d6da..4af3f89 100644 --- a/src/updateCustomerAccountBalance.h +++ b/src/updateCustomerAccountBalance.h @@ -4,6 +4,8 @@ #include #define MAX_LENGTH 100 -bool updateAvailableAccountBalance(int user_id, float changeInBalance); -void deleteLineFromFile(const char* file_name, int line, const char* new_line); + + +bool updateAvailableAccountBalance(int user_id, float changeInBalance, bool positive); + void replaceBalanceInString(float replacementBalance, int currentLine); \ No newline at end of file