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
  9. 42
      tests/test_currencyExchange.c
  10. 70
      tests/test_currentCustomerAccountBalance.c

1
.gitignore

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

5
src/currencyExchange.h

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

3
src/currentCustomerAccountBalance.c

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

5
src/depositMoney.h

@ -1,4 +1,5 @@
#ifndef DEPOSITMONEY_H_
#define DEPOSITMONEY_H_
#include <stdio.h> #include <stdio.h>
#include "CustomerProperties.h" #include "CustomerProperties.h"
@ -9,3 +10,5 @@
bool depositMoney(int customerID); bool depositMoney(int customerID);
void askToTryAgain(bool afterError, int customerID); void askToTryAgain(bool afterError, int customerID);
bool depositSpecificAmount(int customerID, float amount); 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 <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.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". 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){ switch(errorCode){
case 0: 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; break;
case 1: 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?"); 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){ void replaceLineInFile(const char* file_name, int line, const char* new_line){
FILE* file = fopen(file_name, "r"); FILE* file = fopen(file_name, "r");
if (file == NULL) { if (file == NULL) {
printf("Error opening file!\n");
troubleShoot(0);
return; return;
} }
char current_string[1024]; 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"; char *temp_file_name = "temp.txt";
FILE* temp_file = fopen(temp_file_name, "w"); FILE* temp_file = fopen(temp_file_name, "w");
if (temp_file == NULL) { if (temp_file == NULL) {
printf("Error creating temp file!\n");
troubleShoot(1);
fclose(file); fclose(file);
return; return;
} }
@ -51,10 +51,10 @@ void replaceLineInFile(const char* file_name, int line, const char* new_line){
fclose(file); fclose(file);
fclose(temp_file); fclose(temp_file);
if(remove(file_name)!=0){ if(remove(file_name)!=0){
printf("could not remove the original file!");
troubleShoot(2);
} // Remove the original file } // Remove the original file
if(rename(temp_file_name, file_name)!=0){ if(rename(temp_file_name, file_name)!=0){
printf("could not rename!");
troubleShoot(3);
} // Rename the temp file to the original file } // 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+"); FILE *file = fopen(CUSTOMER_DATA_FILE, "r+");
if (file == NULL) { if (file == NULL) {
printf("Error: Could not open file CustomerData.txt\n");
troubleShoot(0);
return false; return false;
} }
while(keep_reading) { while(keep_reading) {
@ -107,6 +107,8 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){
if(customer_found){ if(customer_found){
replaceBalanceInString(newBalance,currentLine); replaceBalanceInString(newBalance,currentLine);
return true; return true;
}else{
troubleShoot(4);
} }
return false; return false;

28
src/withdrawMoney.c

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

42
tests/test_currencyExchange.c

@ -13,35 +13,35 @@ void tearDown(void)
void test_convert(void) { void test_convert(void) {
/* Arrange */
/* Arrange */
int length = 10;
float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899};
int length = 10;
float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899};
float expectedUSD[length];
float expectedGBP[length];
float expectedUSD[length];
float expectedGBP[length];
float resultUSD[length];
float resultGBP[length];
float resultUSD[length];
float resultGBP[length];
for (int i = 0; i < length; i++) {
expectedUSD[i] = euro[i] * USD_RATE_OF_ONE_EURO;
expectedGBP[i] = euro[i] * GBP_RATE_OF_ONE_EURO;
}
for (int i = 0; i < length; i++) {
expectedUSD[i] = euro[i] * USD_RATE_OF_ONE_EURO;
expectedGBP[i] = euro[i] * GBP_RATE_OF_ONE_EURO;
}
/* Act */
/* Act */
for (int i = 0; i < length; i++) {
resultUSD[i] = convert(euro[i], CURRENCY_CODE_USD);
resultGBP[i] = convert(euro[i], CURRENCY_CODE_GBP);
}
for (int i = 0; i < length; i++) {
resultUSD[i] = convert(euro[i], CURRENCY_CODE_USD);
resultGBP[i] = convert(euro[i], CURRENCY_CODE_GBP);
}
/* Assert*/
/* Assert*/
for (int i = 0; i < length; i++) {
TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]);
TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]);
}
for (int i = 0; i < length; i++) {
TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]);
TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]);
}
} }

70
tests/test_currentCustomerAccountBalance.c

@ -14,36 +14,36 @@ void tearDown(void)
void test_fetchBalanceFromBalanceString(void) { void test_fetchBalanceFromBalanceString(void) {
/* Arrange */
/* Arrange */
char balanceString[5][100] = {
"balance=0",
"balance=100",
"balance=200",
"balance=300",
"balance=400"
};
char balanceString[5][100] = {
"balance=0",
"balance=100",
"balance=200",
"balance=300",
"balance=400"
};
/* Act */
/* Act */
float balance = 0;
float result[5];
float expected[5];
float balance = 0;
float result[5];
float expected[5];
for (int i = 0; i < 5; i++) {
result[i] = fetchBalanceFromBalanceString(balanceString[i]);
}
for (int i = 0; i < 5; i++) {
result[i] = fetchBalanceFromBalanceString(balanceString[i]);
}
/* Assert */
/* Assert */
for (int i = 0; i < 5; i++) {
expected[i] = balance;
balance += 100;
}
for (int i = 0; i < 5; i++) {
expected[i] = balance;
balance += 100;
}
for (int i =0; i < 5; i++) {
TEST_ASSERT_EQUAL_FLOAT(expected[i],result[i]);
}
for (int i =0; i < 5; i++) {
TEST_ASSERT_EQUAL_FLOAT(expected[i],result[i]);
}
} }
@ -51,34 +51,34 @@ void test_fetchBalanceFromBalanceString(void) {
void test_checkFileOpen(void) { void test_checkFileOpen(void) {
/* Act and assert */
/* Act and assert */
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
TEST_ASSERT_TRUE(file);
TEST_ASSERT_TRUE(file);
fclose(file);
fclose(file);
} }
void test_failOpenFile(void) { void test_failOpenFile(void) {
/* Act and assert */
/* Act and assert */
FILE *file = fopen("false_file_name", "r");
FILE *file = fopen("false_file_name", "r");
TEST_ASSERT_FALSE(file);
TEST_ASSERT_FALSE(file);
} }
void test_getAvailableAccountBalance(void) { void test_getAvailableAccountBalance(void) {
/* Act and assert */
/* Act and assert */
int user_id = 1234; // Random user_id (because idea is to read the file and get a float value)
float max = FLT_MAX;
int result = getAvailableAccountBalance(user_id);
int user_id = 1234; // Random user_id (because idea is to read the file and get a float value)
float max = FLT_MAX;
int result = getAvailableAccountBalance(user_id);
TEST_ASSERT_TRUE(result < max); // Pass if function is successfully called and a float value (balance) is returned
TEST_ASSERT_TRUE(result < max); // Pass if function is successfully called and a float value (balance) is returned
} }

Loading…
Cancel
Save