Browse Source

Create function withdrawSpecificAmount

remotes/origin/feature/loan-eligibility
fdlt3817 2 years ago
parent
commit
7479c4a348
  1. 1
      .gitignore
  2. 5
      src/currencyExchange.h
  3. 3
      src/currentCustomerAccountBalance.c
  4. 5
      src/depositMoney.h
  5. 5
      src/lineReplacer.h
  6. 14
      src/updateCustomerAccountBalance.c
  7. 28
      src/withdrawMoney.c
  8. 8
      src/withdrawMoney.h

1
.gitignore

@ -1,2 +1,3 @@
.DS_Store
.vscode
src/a.out

5
src/currencyExchange.h

@ -1,3 +1,6 @@
#ifndef CURRENCYEXCHANGE_H_
#define CURRENCYEXCHANGE_H_
#include <stdio.h>
#define USD_RATE_OF_ONE_EURO 1.07
@ -7,3 +10,5 @@
#define CURRENCY_CODE_GBP 2
float convert(float euro, int newCurrencyCode);
#endif

3
src/currentCustomerAccountBalance.c

@ -1,5 +1,6 @@
#include "currentCustomerAccountBalance.h"
#include "_file_information.h"
//#include "_file_information.h"
float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]) {
float balance = 0;

5
src/depositMoney.h

@ -1,4 +1,5 @@
#ifndef DEPOSITMONEY_H_
#define DEPOSITMONEY_H_
#include <stdio.h>
#include "CustomerProperties.h"
@ -9,3 +10,5 @@
bool depositMoney(int customerID);
void askToTryAgain(bool afterError, int customerID);
bool depositSpecificAmount(int customerID, float amount);
#endif

5
src/lineReplacer.h

@ -1,6 +1,11 @@
#ifndef LINEREPLACER_H_
#define LINEREPLACER_H_
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
void replaceLineInFile(const char* file_name, int line, const char* new_line); //replaces the line at "line" on the file "file_name", with the new line "new_line".
#endif

14
src/updateCustomerAccountBalance.c

@ -7,7 +7,7 @@ void troubleShoot(int errorCode){
switch(errorCode){
case 0:
printf("Requested file could not be opened. Are you sure it exists?");
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?");
@ -27,7 +27,7 @@ void troubleShoot(int errorCode){
void replaceLineInFile(const char* file_name, int line, const char* new_line){
FILE* file = fopen(file_name, "r");
if (file == NULL) {
printf("Error opening file!\n");
troubleShoot(0);
return;
}
char current_string[1024];
@ -35,7 +35,7 @@ void replaceLineInFile(const char* file_name, int line, const char* new_line){
char *temp_file_name = "temp.txt";
FILE* temp_file = fopen(temp_file_name, "w");
if (temp_file == NULL) {
printf("Error creating temp file!\n");
troubleShoot(1);
fclose(file);
return;
}
@ -51,10 +51,10 @@ void replaceLineInFile(const char* file_name, int line, const char* new_line){
fclose(file);
fclose(temp_file);
if(remove(file_name)!=0){
printf("could not remove the original file!");
troubleShoot(2);
} // Remove the original file
if(rename(temp_file_name, file_name)!=0){
printf("could not rename!");
troubleShoot(3);
} // Rename the temp file to the original file
}
@ -80,7 +80,7 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
FILE *file = fopen(CUSTOMER_DATA_FILE, "r+");
if (file == NULL) {
printf("Error: Could not open file CustomerData.txt\n");
troubleShoot(0);
return false;
}
while(keep_reading) {
@ -107,6 +107,8 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
if(customer_found){
replaceBalanceInString(newBalance,currentLine);
return true;
}else{
troubleShoot(4);
}
return false;

28
src/withdrawMoney.c

@ -1,6 +1,6 @@
#include "withdrawMoney.h"
#include "currentCustomerAccountBalance.c"
#include "updateCustomerAccountBalance.c"
#include "currentCustomerAccountBalance.c"
void notifyCustomer(float amountToWithdraw, float remainingAccountBalance) {
char returnToHomeInput;
@ -21,7 +21,7 @@ float initiateWithdraw(float amountToWithdraw, float availableAccountBalance) {
return remainingAccountBalance;
}
void withdraw(int user_id) {
bool withdraw(int user_id) {
float amountToWithdraw;
char tryDifferentAmount;
float remainingAccountBalance;
@ -37,9 +37,11 @@ void withdraw(int user_id) {
updateSuccess = updateAvailableAccountBalance(user_id, remainingAccountBalance);
if( updateSuccess ) {
notifyCustomer(amountToWithdraw, remainingAccountBalance);
return true;
}
else {
printf("Some error occured! Sorry for the inconvenience caused.\n");
return false;
}
}
@ -51,6 +53,7 @@ void withdraw(int user_id) {
}
else {
//showAllMenuEntries();
return false;
}
}
}
@ -58,4 +61,25 @@ void withdraw(int user_id) {
printf("Invalid Input! Please try again.\n");
withdraw(user_id);
}
return false;
}
bool withdrawSpecificAmount(int user_id, float amountToWithdraw) {
float remainingAccountBalance;
float availableAccountBalance = getAvailableAccountBalance(user_id);
if (amountToWithdraw > 0) {
if (amountToWithdraw <= availableAccountBalance) {
remainingAccountBalance = initiateWithdraw(amountToWithdraw, availableAccountBalance);
if(updateAvailableAccountBalance(user_id, remainingAccountBalance)){
return true;
}
}
}
return false;
}

8
src/withdrawMoney.h

@ -1,6 +1,12 @@
#ifndef WITHDRAWMONEY_H_
#define WITHDRAWMONEY_H_
#include <stdio.h>
#include <stdbool.h>
void withdraw(int user_id);
bool withdraw(int user_id);
float initiateWithdraw(float amountToWithdraw, float availableAccountBalance);
void notifyCustomer(float amountToWithdraw, float remainingAccountBalance);
bool withdrawSpecificAmount(int user_id, float amountToWithdraw);
#endif
Loading…
Cancel
Save