You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
4.9 KiB
169 lines
4.9 KiB
#include "updateCustomerAccountBalance.h"
|
|
#include "lineReplacer.h"
|
|
#include "_file_information.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 or that the program has the required permissions?");
|
|
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[1024];
|
|
int current_line = 1;
|
|
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", 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, "%g", 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 customer_found=false;
|
|
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(CUSTOMER_DATA_FILE, "r+");
|
|
if (file == NULL) {
|
|
troubleShoot(0);
|
|
return false;
|
|
}
|
|
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);
|
|
}
|
|
|
|
strcpy(balance_String, buffer);
|
|
currentLine+=4;
|
|
keep_reading = false;
|
|
customer_found=true;
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);;
|
|
if(customer_found){
|
|
replaceBalanceInString(newBalance,currentLine);
|
|
return true;
|
|
}else{
|
|
troubleShoot(4);
|
|
}
|
|
return false;
|
|
|
|
}
|
|
bool checkCustomerExists(int customerID){
|
|
bool keep_reading = true;
|
|
bool customer_found=false;
|
|
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", customerID); // converts user_id to string
|
|
strcat(stringID, user_id_as_string);
|
|
|
|
FILE *file = fopen(CUSTOMER_DATA_FILE, "r+");
|
|
if (file == NULL) {
|
|
return false;
|
|
}
|
|
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);
|
|
}
|
|
|
|
strcpy(balance_String, buffer);
|
|
currentLine+=4;
|
|
keep_reading = false;
|
|
customer_found=true;
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);;
|
|
if(customer_found){
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
//traditional testing section
|
|
/*
|
|
int main(int argc, char *argv[])
|
|
{
|
|
updateAvailableAccountBalance(1327,70);
|
|
return 0;
|
|
}
|
|
*/
|