Browse Source

Expanded the function to work with negative balance changes.

remotes/origin/feature/update-current-customer-account-balance
fdlt3817 2 years ago
parent
commit
d9925a3a94
  1. 2
      src/CustomerData.txt
  2. BIN
      src/a.exe
  3. 6
      src/lineReplacer.h
  4. 13
      src/updateCustomerAccountBalance.c
  5. 6
      src/updateCustomerAccountBalance.h

2
src/CustomerData.txt

@ -3,4 +3,4 @@ ID=1234
forename=Max forename=Max
Surname=Mustermann Surname=Mustermann
password=example password=example
balance=1000
balance=2000

BIN
src/a.exe

6
src/lineReplacer.h

@ -0,0 +1,6 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
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".

13
src/updateCustomerAccountBalance.c

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

6
src/updateCustomerAccountBalance.h

@ -4,6 +4,8 @@
#include <string.h> #include <string.h>
#define MAX_LENGTH 100 #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); void replaceBalanceInString(float replacementBalance, int currentLine);
Loading…
Cancel
Save