diff --git a/src/CustomerData.txt b/src/CustomerData.txt index cd3f176..106adfc 100644 --- a/src/CustomerData.txt +++ b/src/CustomerData.txt @@ -3,14 +3,14 @@ ID=1234 forename=Max Surname=Mustermann password=example -balance=2340 +balance=2300 1327=example ID=1327 forename=Max Surname=Mustermann password=example -balance=20 +balance=160 1666=example ID=1666 diff --git a/src/sendmoney.c b/src/sendmoney.c new file mode 100644 index 0000000..0980976 --- /dev/null +++ b/src/sendmoney.c @@ -0,0 +1,55 @@ +#include "sendMoney.h" +#include "depositMoney.c" +#include "withdrawMoney.c" + +bool sendMoney(int customerID){ + float availableAccountBalance=getAvailableAccountBalance(customerID); + float amountToSend; + int recID; + printf("\nYou have %.2f€ in your account. How much would you like to send?\n",availableAccountBalance); + scanf("%f",&amountToSend); + if(amountToSend>0 && amountToSend1000){ + bool recExists=checkCustomerExists(recID); + if(recExists){ + if(recID==customerID){ + printf("\nYou cannot send money to yourself. Aborting. \n"); + return false; + } + else{ + if(withdrawSpecificAmount(customerID, amountToSend)){ + if(depositSpecificAmount(recID, amountToSend)){ + printf("\nYou have successfuly transfered %.2f€ to [%d]",amountToSend,recID); + return true; + } + else{ + printf("\nSomething went wrong with the transfer. Please contact staff."); + } + } + } + + }else{ + if(withdrawSpecificAmount(customerID, amountToSend)){ + printf("\nYou have successfuly transfered %.2f€ to [%d]",amountToSend,recID); + return true; + } + } + } + else + { + printf("\nThe ID you have entered is not valid. Aborting. \n"); + } + + }else{ + //error + return false; + } + return false; +} + +// int main(){ +// sendMoney(1234); +// return 0; +// } \ No newline at end of file diff --git a/src/sendmoney.h b/src/sendmoney.h new file mode 100644 index 0000000..1484dca --- /dev/null +++ b/src/sendmoney.h @@ -0,0 +1,10 @@ +#ifndef SENDMONEY_H +#define SENDMONEY_H +#include +#include +#include +#include + +bool sendMoney(int customerID); + +#endif // SENDMONEY_H diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 794cbde..900928e 100644 --- a/src/updateCustomerAccountBalance.c +++ b/src/updateCustomerAccountBalance.c @@ -112,6 +112,51 @@ bool updateAvailableAccountBalance(int user_id, float newBalance){ } return false; +} +bool checkCustomerExists(int customerID){ + bool keep_reading = true; + bool customer_found=false; + char buffer[MAX_LENGTH]; + char stringID[MAX_LENGTH] = "ID="; + char user_id_as_string[MAX_LENGTH]; + char balance_String[MAX_LENGTH]; + int currentLine=0; + + sprintf(user_id_as_string, "%d", customerID); // converts user_id to string + strcat(stringID, user_id_as_string); + + FILE *file = fopen(CUSTOMER_DATA_FILE, "r+"); + if (file == NULL) { + return false; + } + while(keep_reading) { + fgets(buffer, MAX_LENGTH, file); + currentLine++; + if (feof(file)) { + keep_reading = false; + } + else if(strstr(buffer, stringID)) { //found the customer + + for (int i = 0; i < 4; i++) { + fgets(buffer, MAX_LENGTH, file); + } + + strcpy(balance_String, buffer); + currentLine+=4; + keep_reading = false; + customer_found=true; + } + + } + + fclose(file);; + if(customer_found){ + return true; + }else{ + return false; + } + return false; + } //traditional testing section diff --git a/src/updateCustomerAccountBalance.h b/src/updateCustomerAccountBalance.h index 9a931b3..17f34ae 100644 --- a/src/updateCustomerAccountBalance.h +++ b/src/updateCustomerAccountBalance.h @@ -9,7 +9,7 @@ bool updateAvailableAccountBalance(int user_id, float newBalance); - +bool checkCustomerExists(int customerID); void replaceBalanceInString(float replacementBalance, int currentLine); #endif \ No newline at end of file diff --git a/tests/test_sendmoney.c b/tests/test_sendmoney.c new file mode 100644 index 0000000..66be905 --- /dev/null +++ b/tests/test_sendmoney.c @@ -0,0 +1,69 @@ +#ifdef TEST +#include +#include + +void setUp(void) +{ +} + +void tearDown(void) +{ +} +// hier wird die Methode checkAccount getestet +void test_sendmoney_NeedToImplement(void) +{ + // Arrage + + int accountNumber = 1234567; + int accountNumber2 = 5555557; + + // hier soll false sein + int accountNumber3 = 53323; + int accountNumber4 = 34342; + bool expected; + bool expected2; + bool expected3; + bool expected4; + // Act + + expected = checkAccount(accountNumber); + expected2 = checkAccount(accountNumber2); + expected3 = checkAccount(accountNumber3); + expected4 = checkAccount(accountNumber4); + + // Assert + + TEST_ASSERT_TRUE(expected); + TEST_ASSERT_TRUE(expected2); + TEST_ASSERT_FALSE(expected3); + TEST_ASSERT_FALSE(expected4); +} + +// hier wird die Methode getAvailableAccountBalance()getestet +void test_getAvailableAccountBalance(void) +{ + + // Arrage + + double expected1 = 24.0; + int length = 2; + + // Act + + double result = getAvailableAccountBalance(length); + + // Assert + TEST_ASSERT_EQUAL(expected1, result); +} +// hier wird die Methode getAmount() getestet +void test_getAmount(void) +{ + // Arrage + float expected = 500.1; + + float result = getAmount(expected); + + TEST_ASSERT_EQUAL(expected, result); +} + +#endif // TEST