diff --git a/.gitignore b/.gitignore index 496ee2c..2608ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.vscode \ No newline at end of file diff --git a/src/CustomerData.txt b/src/CustomerData.txt new file mode 100644 index 0000000..911b18e --- /dev/null +++ b/src/CustomerData.txt @@ -0,0 +1,20 @@ +1234=example +ID=1234 +forename=Max +Surname=Mustermann +password=example +balance=5000 + +1327=example +ID=1327 +forename=Max +Surname=Mustermann +password=example +balance=1500 + +1666=example +ID=1666 +forename=Max +Surname=Mustermann +password=example +balance=240 \ No newline at end of file diff --git a/src/CustomerProperties.h b/src/CustomerProperties.h new file mode 100644 index 0000000..2bbab62 --- /dev/null +++ b/src/CustomerProperties.h @@ -0,0 +1,4 @@ +typedef struct Customer{ + char *ID, *forename, *surname, *password; + float balance; +}customer_t; diff --git a/src/LoginCustomer.c b/src/LoginCustomer.c new file mode 100644 index 0000000..5c2a7a4 --- /dev/null +++ b/src/LoginCustomer.c @@ -0,0 +1,97 @@ +#include "LoginCustomer.h" + +bool checkLogin(bool loginSuccessful) +{ + return (loginSuccessful) ? true : false; +} + +void collectCustomerDataForLogin(int attempts) +{ + customer_t c; + c.ID = calloc(15+1,sizeof(char)); + c.password = calloc(15+1, sizeof(char)); + char digitCharacterFromUser, passwordCharacterFromUser; + int IDLengthCounter = 0, passwordLengthCounter = 0; + const int IDMaxLength = 16, passwordMaxLength = 16; + + printf("Enter ID:\n"); + while(((digitCharacterFromUser=getchar()) != '\n')&&IDLengthCounter='0'&&digitCharacterFromUser<='9'){ + *(c.ID+IDLengthCounter) = digitCharacterFromUser; + } + else{ + printf("Character entered is not a digit. Aborting.\n"); + exit(-1); + } + ++IDLengthCounter; + } + *(c.ID+IDLengthCounter) = '\0'; + + if(IDLengthCounter>=IDMaxLength){ + printf("ID entered is too long. Aborting.\n"); + exit(-1); + } + + printf("Enter password:\n"); + while((passwordCharacterFromUser=getchar())!='\n'&&passwordLengthCounter=passwordMaxLength){ + printf("Password entered is too long. Aborting.\n"); + exit(-1); + } + customer_t *ptr = &c; + bool loginSuccessful = loginCustomer(ptr); + if(loginSuccessful ) { + printf("Welcome to the menu!\n"); + //call menu() + }else if(!loginSuccessful && attempts < MAX_LOGIN_ATTEMPTS){ + printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts); + collectCustomerDataForLogin(++attempts); + }else{ + printf("Maximum number of attempts reached. Program terminates.\n"); + exit(-1); + //call error() + } + +} + +bool loginCustomer(customer_t *c) +{ + bool foundCustomerEntryInFile = false; + char *searchForThisString = generateCheckString(c->ID,c->password); + char *lineFromCustomerFile = calloc(40,sizeof(char)); + FILE *readCustomerFile = fopen("CustomerData.txt", "r"); + if(readCustomerFile==NULL){ + printf("Could not find file. Aborting.\n"); + exit(-1); + //call error() + } + while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ + if(strcmp(searchForThisString,lineFromCustomerFile)==0){ + foundCustomerEntryInFile = true; + break; + } + } + + if(checkLogin(foundCustomerEntryInFile)){ + printf("Login successful.\n"); + return foundCustomerEntryInFile; + }else{ + printf("Login not successful.\n"); + } + fclose(readCustomerFile); + return foundCustomerEntryInFile; +} + +char *generateCheckString(char *ID, char *password){ + int checkStringLength = strlen(ID) + 1 + strlen(password) + 1; + char *checkString = calloc(checkStringLength, sizeof(char)); + checkString = strcat(ID,"="); + checkString = strcat(checkString,password); + *(checkString+checkStringLength) = '\0'; + return checkString; +} diff --git a/src/LoginCustomer.h b/src/LoginCustomer.h new file mode 100644 index 0000000..809765b --- /dev/null +++ b/src/LoginCustomer.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include "CustomerProperties.h" +#define MAX_LOGIN_ATTEMPTS 3 +char *generateCheckString(char *, char*); +bool checkLogin(bool); +void collectCustomerDataForLogin(int); +bool loginCustomer(customer_t *); diff --git a/src/_file_information.h b/src/_file_information.h new file mode 100644 index 0000000..1cb5200 --- /dev/null +++ b/src/_file_information.h @@ -0,0 +1,2 @@ +#define MAX_LENGTH 100 +#define CUSTOMER_DATA_FILE "CustomerData.txt" \ No newline at end of file diff --git a/src/a.exe b/src/a.exe new file mode 100644 index 0000000..7bbd2e2 Binary files /dev/null and b/src/a.exe differ diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c new file mode 100644 index 0000000..a91e306 --- /dev/null +++ b/src/currentCustomerAccountBalance.c @@ -0,0 +1,63 @@ +#include "currentCustomerAccountBalance.h" + +float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]) { + float balance = 0; + char *token = strtok(balance_String, "="); // separates string to two parts + while (token != NULL) { + if (atoi(token) != 0) { + balance = atof(token); // converts string to float + break; + } + token = strtok(NULL, "="); + } + return balance; +} + +float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]) { + float balance = 0; + bool keep_reading = true; + char buffer[MAX_LENGTH]; + char balance_String[MAX_LENGTH]; + + while(keep_reading) { + fgets(buffer, MAX_LENGTH, file); + if (feof(file)) { + keep_reading = false; + } + else if(strstr(buffer, stringID)) { + for (int i = 0; i < 4; i++) { + fgets(buffer, MAX_LENGTH, file); + } + strcpy(balance_String, buffer); + balance = fetchBalanceFromBalanceString(balance_String); + keep_reading = false; + } + + } + return balance; +} + +float getAvailableAccountBalance(int user_id) { + float availableBalance = 0; + char stringID[MAX_LENGTH] = "ID="; + char user_id_as_string[MAX_LENGTH]; + + sprintf(user_id_as_string, "%d", user_id); // converts user_id to string + strcat(stringID, user_id_as_string); + // Now stringID is "ID=user_id" + + FILE *file = fopen(CUSTOMER_DATA_FILE, "r"); + if(file == 0) { + printf("Error: customer data file cannot be opened!\n"); + return 0; + } + else { + availableBalance = readFileAndGetAvailableBalance(file, stringID); + } + + + fclose(file); + + return availableBalance; + +} \ No newline at end of file diff --git a/src/currentCustomerAccountBalance.h b/src/currentCustomerAccountBalance.h new file mode 100644 index 0000000..2795450 --- /dev/null +++ b/src/currentCustomerAccountBalance.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include + +#include "_file_information.h" + +float getAvailableAccountBalance(int user_id); +float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]); +float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]); \ No newline at end of file diff --git a/src/lineReplacer.h b/src/lineReplacer.h new file mode 100644 index 0000000..999855c --- /dev/null +++ b/src/lineReplacer.h @@ -0,0 +1,6 @@ +#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 diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c new file mode 100644 index 0000000..2040a6d --- /dev/null +++ b/src/updateCustomerAccountBalance.c @@ -0,0 +1,81 @@ +#include "updateCustomerAccountBalance.h" +#include "currentCustomerAccountBalance.c" +#include "lineReplacer.h" + +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"); + return; + } + char current_string[1024]; + int current_line = 1; + char *temp_file_name = "temp.txt"; + FILE* temp_file = fopen(temp_file_name, "w"); + if (temp_file == NULL) { + printf("Error creating temp file!\n"); + fclose(file); + return; + } + while (fgets(current_string, sizeof(current_string), file) != NULL) { + if (current_line == line) { + fprintf(temp_file, "%s", new_line); + fputs("\n", temp_file); + } else { + fprintf(temp_file, "%s", current_string); + } + current_line++; + } + fclose(file); + fclose(temp_file); + if(remove(file_name)!=0){ + printf("could not remove the original file!"); + } // Remove the original file + if(rename(temp_file_name, file_name)!=0){ + printf("could not rename!"); + } // Rename the temp file to the original file +} + +void replaceBalanceInString(float replacementBalance, int currentLine) { + char newBalanceLine[MAX_LENGTH] = "balance="; + char balance_as_string[MAX_LENGTH]; + sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string + strcat(newBalanceLine, balance_as_string); + replaceLineInFile("CustomerData.txt",currentLine,newBalanceLine); +} + +bool updateAvailableAccountBalance(int user_id, float newBalance){ + + bool keep_reading = true; + + 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", user_id); // converts user_id to string + strcat(stringID, user_id_as_string); + + FILE *file = fopen("CustomerData.txt", "r+"); + printf(stringID); + while(keep_reading) { + fgets(buffer, MAX_LENGTH, file); + currentLine++; + if (feof(file)) { + keep_reading = false; + } + else if(strstr(buffer, stringID)) { //found the customer + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + strcpy(balance_String, buffer); + currentLine+=4; + keep_reading = false; + } + + } + fclose(file);; + replaceBalanceInString(newBalance,currentLine); +} diff --git a/src/updateCustomerAccountBalance.h b/src/updateCustomerAccountBalance.h new file mode 100644 index 0000000..29f5b75 --- /dev/null +++ b/src/updateCustomerAccountBalance.h @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +#define MAX_LENGTH 100 + + +bool updateAvailableAccountBalance(int user_id, float newBalance); + +void replaceBalanceInString(float replacementBalance, int currentLine); diff --git a/team.md b/team.md deleted file mode 100644 index 02580e9..0000000 --- a/team.md +++ /dev/null @@ -1,9 +0,0 @@ -# Bankmanagement-System - -- Can Hacioglu, Fdlt3817 -- Atharva Kishor Naik, fdai7514 -- Julius Philipp Engel, fdai7057 -- Shivam Chaudhary, fdlt3781 -- Mohamed Yahya Dahi, fdai6618 -- Haytham Daoula, fdai7207 - diff --git a/tests/test_LoginCustomer.c b/tests/test_LoginCustomer.c new file mode 100644 index 0000000..610a847 --- /dev/null +++ b/tests/test_LoginCustomer.c @@ -0,0 +1,21 @@ +#include +#include "LoginCustomer.h" +void setUp(){}; +void tearDown(){}; +void test_checkLogin() +{ + /*arrange*/ + bool expected_test_values_compute_to_true[] = {4==4,true==true, 1==1, false==false, 'z'=='z', '='=='=',0x1A==0x1A}; + int length_1 = sizeof(expected_test_values_compute_to_true)/sizeof(bool); + + bool expected_test_values_compute_to_false[] = {4!=4,true==false,1==0,false==true,'z'=='x','!'==')',0x1A==0x2B}; + int length_2 = sizeof(expected_test_values_compute_to_false)/sizeof(bool); + + /*act and assertions*/ + for(int i=0;i<7;++i) { + TEST_ASSERT_TRUE(checkLogin(expected_test_values_compute_to_true[i])); + } + for(int i=0;i<7;++i){ + TEST_ASSERT_FALSE(checkLogin(expected_test_values_compute_to_false[i])); + } +}