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.

113 lines
3.7 KiB

  1. #include "updateCustomerAccountBalance.h"
  2. #include "currentCustomerAccountBalance.c"
  3. #include "lineReplacer.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?");
  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[100];
  31. int current_line = 1;
  32. char *temp_file_name = calloc(8+1, sizeof(char));
  33. temp_file_name = "temp.txt";
  34. FILE* temp_file = fopen(temp_file_name, "w");
  35. if (temp_file == NULL) {
  36. troubleShoot(1);
  37. fclose(file);
  38. return;
  39. }
  40. while (fgets(current_string, sizeof(current_string),file) != NULL) {
  41. if (current_line == line) {
  42. fprintf(temp_file, "%s\n", new_line);
  43. //fputs("\n", temp_file);
  44. } else {
  45. fprintf(temp_file, "%s", current_string);
  46. }
  47. current_line++;
  48. }
  49. fclose(file);
  50. fclose(temp_file);
  51. if(remove(file_name)!=0){
  52. troubleShoot(2);
  53. } // Remove the original file
  54. if(rename(temp_file_name, file_name)!=0){
  55. troubleShoot(3);
  56. } // Rename the temp file to the original file
  57. }
  58. void replaceBalanceInString(float replacementBalance, int currentLine) {
  59. char newBalanceLine[MAX_LENGTH] = "balance=";
  60. char balance_as_string[MAX_LENGTH];
  61. sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to string
  62. strcat(newBalanceLine, balance_as_string);
  63. replaceLineInFile(CUSTOMER_DATA_FILE,currentLine,newBalanceLine);
  64. }
  65. bool updateAvailableAccountBalance(int user_id, float newBalance){
  66. bool keep_reading = true;
  67. bool foundCustomer = false;
  68. char *buffer = malloc(MAX_LENGTH * sizeof(char));
  69. char *stringID = malloc(MAX_LENGTH * sizeof(char));
  70. stringID = "ID=";
  71. char *user_id_as_string =malloc(MAX_LENGTH * sizeof(char));
  72. char *balance_String = malloc(MAX_LENGTH * sizeof(char));
  73. int currentLine=0;
  74. sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
  75. strcat(stringID, user_id_as_string);
  76. FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
  77. //printf(stringID);
  78. while(keep_reading) {
  79. fgets(buffer, MAX_LENGTH, file);
  80. currentLine++;
  81. if (feof(file)) {
  82. keep_reading = false;
  83. }
  84. else if(strstr(buffer, stringID)) { //found the customer
  85. for (int i = 0; i < 4; i++) {
  86. fgets(buffer, MAX_LENGTH, file);
  87. currentLine++;
  88. }
  89. strcpy(balance_String, buffer);
  90. keep_reading = false;
  91. foundCustomer=true;
  92. }
  93. }
  94. fclose(file);
  95. if(foundCustomer){
  96. replaceBalanceInString(newBalance,currentLine);
  97. }else{
  98. troubleShoot(4);
  99. }
  100. return true;
  101. }