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. 15
      src/updateCustomerAccountBalance.c
  5. 6
      src/updateCustomerAccountBalance.h

2
src/CustomerData.txt

@ -3,4 +3,4 @@ ID=1234
forename=Max
Surname=Mustermann
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".

15
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;
}

6
src/updateCustomerAccountBalance.h

@ -4,6 +4,8 @@
#include <string.h>
#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);
Loading…
Cancel
Save