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

  1. #include "updateCustomerAccountBalance.h"
  2. #include "lineReplacer.h"
  3. #include "_file_information.h"
  4. void troubleShoot(int errorCode){
  5. printf("Error! The requested operation was terminated because of an issue. Here are some details about the error:\n---------------\n");
  6. switch(errorCode){
  7. case 0:
  8. printf("Requested file could not be opened. Are you sure it exists or that the program has the required permissions?");
  9. break;
  10. case 1:
  11. printf("A temporary file could not be generated. Are you sure the bank management system has the required authorization to create new files?");
  12. break;
  13. case 2:
  14. printf("Replacement of the old file failed. Are you sure the bank management system has the required authorization to delete files?");
  15. break;
  16. case 3:
  17. printf("Renaming of a file failed. Are you sure the bank management system has the required authorization to rename files?");
  18. break;
  19. case 4:
  20. printf("Could not find the customer. Please contact customer support.");
  21. break;
  22. }
  23. }
  24. void replaceLineInFile(const char* file_name, int line, const char* new_line){
  25. FILE* file = fopen(file_name, "r");
  26. if (file == NULL) {
  27. troubleShoot(0);
  28. return;
  29. }
  30. char current_string[1024];
  31. int current_line = 1;
  32. char *temp_file_name = "temp.txt";
  33. FILE* temp_file = fopen(temp_file_name, "w");
  34. if (temp_file == NULL) {
  35. troubleShoot(1);
  36. fclose(file);
  37. return;
  38. }
  39. while (fgets(current_string, sizeof(current_string), file) != NULL) {
  40. if (current_line == line) {
  41. fprintf(temp_file, "%s", new_line);
  42. fputs("\n", temp_file);
  43. } else {
  44. fprintf(temp_file, "%s", current_string);
  45. }
  46. current_line++;
  47. }
  48. fclose(file);
  49. fclose(temp_file);
  50. if(remove(file_name)!=0){
  51. troubleShoot(2);
  52. } // Remove the original file
  53. if(rename(temp_file_name, file_name)!=0){
  54. troubleShoot(3);
  55. } // Rename the temp file to the original file
  56. }
  57. void replaceBalanceInString(float replacementBalance, int currentLine) {
  58. char newBalanceLine[MAX_LENGTH] = "balance=";
  59. char balance_as_string[MAX_LENGTH];
  60. sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string
  61. strcat(newBalanceLine, balance_as_string);
  62. replaceLineInFile(CUSTOMER_DATA_FILE,currentLine,newBalanceLine);
  63. }
  64. bool updateAvailableAccountBalance(int user_id, float newBalance){
  65. bool keep_reading = true;
  66. bool customer_found=false;
  67. char buffer[MAX_LENGTH];
  68. char stringID[MAX_LENGTH] = "ID=";
  69. char user_id_as_string[MAX_LENGTH];
  70. char balance_String[MAX_LENGTH];
  71. int currentLine=0;
  72. sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
  73. strcat(stringID, user_id_as_string);
  74. FILE *file = fopen(CUSTOMER_DATA_FILE, "r+");
  75. if (file == NULL) {
  76. troubleShoot(0);
  77. return false;
  78. }
  79. while(keep_reading) {
  80. fgets(buffer, MAX_LENGTH, file);
  81. currentLine++;
  82. if (feof(file)) {
  83. keep_reading = false;
  84. }
  85. else if(strstr(buffer, stringID)) { //found the customer
  86. for (int i = 0; i < 4; i++) {
  87. fgets(buffer, MAX_LENGTH, file);
  88. }
  89. strcpy(balance_String, buffer);
  90. currentLine+=4;
  91. keep_reading = false;
  92. customer_found=true;
  93. }
  94. }
  95. fclose(file);;
  96. if(customer_found){
  97. replaceBalanceInString(newBalance,currentLine);
  98. return true;
  99. }else{
  100. troubleShoot(4);
  101. }
  102. return false;
  103. }
  104. bool checkCustomerExists(int customerID){
  105. bool keep_reading = true;
  106. bool customer_found=false;
  107. char buffer[MAX_LENGTH];
  108. char stringID[MAX_LENGTH] = "ID=";
  109. char user_id_as_string[MAX_LENGTH];
  110. char balance_String[MAX_LENGTH];
  111. int currentLine=0;
  112. sprintf(user_id_as_string, "%d", customerID); // converts user_id to string
  113. strcat(stringID, user_id_as_string);
  114. FILE *file = fopen(CUSTOMER_DATA_FILE, "r+");
  115. if (file == NULL) {
  116. return false;
  117. }
  118. while(keep_reading) {
  119. fgets(buffer, MAX_LENGTH, file);
  120. currentLine++;
  121. if (feof(file)) {
  122. keep_reading = false;
  123. }
  124. else if(strstr(buffer, stringID)) { //found the customer
  125. for (int i = 0; i < 4; i++) {
  126. fgets(buffer, MAX_LENGTH, file);
  127. }
  128. strcpy(balance_String, buffer);
  129. currentLine+=4;
  130. keep_reading = false;
  131. customer_found=true;
  132. }
  133. }
  134. fclose(file);;
  135. if(customer_found){
  136. return true;
  137. }else{
  138. return false;
  139. }
  140. return false;
  141. }
  142. //traditional testing section
  143. /*
  144. int main(int argc, char *argv[])
  145. {
  146. updateAvailableAccountBalance(1327,70);
  147. return 0;
  148. }
  149. */