|
|
@ -0,0 +1,95 @@ |
|
|
|
#include "updateCustomerAccountBalance.h" |
|
|
|
#include "currentCustomerAccountBalance.c" |
|
|
|
|
|
|
|
void deleteLineFromFile(int line){ |
|
|
|
FILE *fptr1, *fptr2; |
|
|
|
char file1[] ="CustomerData.txt"; |
|
|
|
char file2[] ="TempData.txt"; |
|
|
|
char curr; |
|
|
|
int del=line, line_number = 0; |
|
|
|
fptr1 = fopen(file1,"r"); |
|
|
|
fptr2 = fopen(file2, "w"); |
|
|
|
curr = getc(fptr1); |
|
|
|
if(curr!=EOF) {line_number =1;} |
|
|
|
while(1){ |
|
|
|
if(del != line_number){ |
|
|
|
putc(curr, fptr2); |
|
|
|
curr = getc(fptr1); |
|
|
|
if(curr =='\n') line_number++; |
|
|
|
if(curr == EOF) break; |
|
|
|
}else{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
fclose(fptr1); |
|
|
|
remove(file1); |
|
|
|
fclose(fptr2); |
|
|
|
rename(file2,"CustomerData.txt"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void replaceBalanceInString(float replacementBalance, int currentLine) { |
|
|
|
deleteLineFromFile(currentLine-1); |
|
|
|
printf("deleting from line %i. balance will be replaced with %f", currentLine, replacementBalance); |
|
|
|
char newBalanceLine[MAX_LENGTH] = "balance="; |
|
|
|
char balance_as_string[MAX_LENGTH]; |
|
|
|
sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string |
|
|
|
strcat(newBalanceLine, balance_as_string); |
|
|
|
printf(newBalanceLine); |
|
|
|
|
|
|
|
char buffer[MAX_LENGTH]; |
|
|
|
FILE *file = fopen("CustomerData.txt", "w"); |
|
|
|
|
|
|
|
for(int i=0;i<currentLine;i++){ |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
} |
|
|
|
fprintf(file, newBalanceLine); |
|
|
|
fclose(file); |
|
|
|
} |
|
|
|
|
|
|
|
bool updateAvailableAccountBalance(int user_id, float changeInBalance){ |
|
|
|
|
|
|
|
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]; |
|
|
|
int currentLine=0; |
|
|
|
|
|
|
|
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); |
|
|
|
currentLine++; |
|
|
|
if (feof(file)) { |
|
|
|
keep_reading = false; |
|
|
|
} |
|
|
|
else if(strstr(buffer, stringID)) { //found the customer |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
fgets(buffer, MAX_LENGTH, file); |
|
|
|
strcpy(balance_String, buffer); |
|
|
|
currentLine+=4; |
|
|
|
availableBalance = fetchBalanceFromBalanceString(balance_String); |
|
|
|
replaceBalanceInString(availableBalance+changeInBalance,currentLine); |
|
|
|
keep_reading = false; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
fclose(file); |
|
|
|
} |
|
|
|
|
|
|
|
int main(){ |
|
|
|
|
|
|
|
updateAvailableAccountBalance(1234,500); |
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|