diff --git a/.gitignore b/.gitignore index 2608ec2..9c741e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -.vscode \ No newline at end of file +.vscode +src/a.out diff --git a/src/currencyExchange.h b/src/currencyExchange.h index 6629fcc..a99847c 100644 --- a/src/currencyExchange.h +++ b/src/currencyExchange.h @@ -1,3 +1,6 @@ +#ifndef CURRENCYEXCHANGE_H_ +#define CURRENCYEXCHANGE_H_ + #include #define USD_RATE_OF_ONE_EURO 1.07 @@ -6,4 +9,6 @@ #define CURRENCY_CODE_USD 1 #define CURRENCY_CODE_GBP 2 -float convert(float euro, int newCurrencyCode); \ No newline at end of file +float convert(float euro, int newCurrencyCode); + +#endif \ No newline at end of file diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c index d306175..e7335a0 100644 --- a/src/currentCustomerAccountBalance.c +++ b/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; diff --git a/src/depositMoney.h b/src/depositMoney.h index 4661e00..2e5ca27 100644 --- a/src/depositMoney.h +++ b/src/depositMoney.h @@ -1,4 +1,5 @@ - +#ifndef DEPOSITMONEY_H_ +#define DEPOSITMONEY_H_ #include #include "CustomerProperties.h" @@ -8,4 +9,6 @@ bool depositMoney(int customerID); void askToTryAgain(bool afterError, int customerID); -bool depositSpecificAmount(int customerID, float amount); \ No newline at end of file +bool depositSpecificAmount(int customerID, float amount); + +#endif \ No newline at end of file diff --git a/src/lineReplacer.h b/src/lineReplacer.h index 999855c..c3ee57b 100644 --- a/src/lineReplacer.h +++ b/src/lineReplacer.h @@ -1,6 +1,11 @@ +#ifndef LINEREPLACER_H_ +#define LINEREPLACER_H_ + #include #include #include #include -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". \ No newline at end of file +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 \ No newline at end of file diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 32c4673..794cbde 100644 --- a/src/updateCustomerAccountBalance.c +++ b/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; diff --git a/src/withdrawMoney.c b/src/withdrawMoney.c index 6361124..fd955c9 100644 --- a/src/withdrawMoney.c +++ b/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); } -} \ No newline at end of file + 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; +} + diff --git a/src/withdrawMoney.h b/src/withdrawMoney.h index 4310dba..972bf74 100644 --- a/src/withdrawMoney.h +++ b/src/withdrawMoney.h @@ -1,6 +1,12 @@ +#ifndef WITHDRAWMONEY_H_ +#define WITHDRAWMONEY_H_ + #include #include -void withdraw(int user_id); +bool withdraw(int user_id); float initiateWithdraw(float amountToWithdraw, float availableAccountBalance); -void notifyCustomer(float amountToWithdraw, float remainingAccountBalance); \ No newline at end of file +void notifyCustomer(float amountToWithdraw, float remainingAccountBalance); +bool withdrawSpecificAmount(int user_id, float amountToWithdraw); + +#endif \ No newline at end of file diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c index 821f56a..66452b5 100644 --- a/tests/test_currencyExchange.c +++ b/tests/test_currencyExchange.c @@ -13,35 +13,35 @@ void tearDown(void) void test_convert(void) { - /* Arrange */ + /* Arrange */ - int length = 10; - float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899}; - - float expectedUSD[length]; - float expectedGBP[length]; + int length = 10; + float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899}; + + 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]); + } } diff --git a/tests/test_currentCustomerAccountBalance.c b/tests/test_currentCustomerAccountBalance.c index 50888db..1d4cedd 100644 --- a/tests/test_currentCustomerAccountBalance.c +++ b/tests/test_currentCustomerAccountBalance.c @@ -14,71 +14,71 @@ void tearDown(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]); + } - + } 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) { - /* 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) { - /* 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); - - TEST_ASSERT_TRUE(result < max); // Pass if function is successfully called and a float value (balance) is returned + 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 }