|
|
#include "updateCustomerAccountBalance.h"
#include "currentCustomerAccountBalance.c"
#include "lineReplacer.h"
void troubleShoot(int errorCode){ printf("Error! The requested operation was terminated because of an issue. Here are some details about the error:\n---------------\n");
switch(errorCode){ case 0: printf("Requested file could not be opened. Are you sure it exists?"); break; case 1: printf("A temporary file could not be generated. Are you sure the bank management system has the required authorization to create new files?"); break; case 2: printf("Replacement of the old file failed. Are you sure the bank management system has the required authorization to delete files?"); break; case 3: printf("Renaming of a file failed. Are you sure the bank management system has the required authorization to rename files?"); break; case 4: printf("Could not find the customer. Please contact customer support."); break; } }
void replaceLineInFile(const char* file_name, int line, const char* new_line){ FILE* file = fopen(file_name, "r"); if (file == NULL) { troubleShoot(0); return; } char current_string[100]; int current_line = 1; char *temp_file_name = calloc(8+1, sizeof(char)); temp_file_name = "temp.txt"; FILE* temp_file = fopen(temp_file_name, "w"); if (temp_file == NULL) { troubleShoot(1); fclose(file); return; } while (fgets(current_string, sizeof(current_string),file) != NULL) { if (current_line == line) { fprintf(temp_file, "%s\n", new_line); //fputs("\n", temp_file);
} else { fprintf(temp_file, "%s", current_string); } current_line++; } fclose(file); fclose(temp_file); if(remove(file_name)!=0){ troubleShoot(2); } // Remove the original file
if(rename(temp_file_name, file_name)!=0){ troubleShoot(3); } // Rename the temp file to the original file
}
void replaceBalanceInString(float replacementBalance, int currentLine) { char newBalanceLine[MAX_LENGTH] = "balance="; char balance_as_string[MAX_LENGTH]; sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to string
strcat(newBalanceLine, balance_as_string); replaceLineInFile(CUSTOMER_DATA_FILE,currentLine,newBalanceLine); }
bool updateAvailableAccountBalance(int user_id, float newBalance){
bool keep_reading = true; bool foundCustomer = false;
char *buffer = malloc(MAX_LENGTH * sizeof(char)); char *stringID = malloc(MAX_LENGTH * sizeof(char)); stringID = "ID="; char *user_id_as_string =malloc(MAX_LENGTH * sizeof(char)); char *balance_String = malloc(MAX_LENGTH * sizeof(char)); 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(CUSTOMER_DATA_FILE, "r"); //printf(stringID);
while(keep_reading) { fgets(buffer, MAX_LENGTH, file); currentLine++; if (feof(file)) { keep_reading = false; } else if(strstr(buffer, stringID)) { //found the customer
for (int i = 0; i < 4; i++) { fgets(buffer, MAX_LENGTH, file); currentLine++; } strcpy(balance_String, buffer); keep_reading = false; foundCustomer=true; }
} fclose(file);
if(foundCustomer){ replaceBalanceInString(newBalance,currentLine); }else{ troubleShoot(4); } return true; }
|