From 3e7a7d3c89873307b7cf5bc02f28bae148a24956 Mon Sep 17 00:00:00 2001 From: fdai7057 Date: Sat, 21 Jan 2023 15:02:45 +0100 Subject: [PATCH] Implementation of functions LoginAsCustomer() and Login() --- src/CustomerData.txt | 6 +++ src/CustomerProperties.h | 4 ++ src/LoginCustomer.c | 92 ++++++++++++++++++++++++++++++++++++++++ src/LoginCustomer.h | 7 +++ 4 files changed, 109 insertions(+) create mode 100644 src/CustomerData.txt create mode 100644 src/CustomerProperties.h diff --git a/src/CustomerData.txt b/src/CustomerData.txt new file mode 100644 index 0000000..3ac070f --- /dev/null +++ b/src/CustomerData.txt @@ -0,0 +1,6 @@ +1234=example +ID=1234 +forename=Max +Surname=Mustermann +password=example +balance=0 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 index c9b956e..5c2a7a4 100644 --- a/src/LoginCustomer.c +++ b/src/LoginCustomer.c @@ -1,5 +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 index 7f0f8f9..809765b 100644 --- a/src/LoginCustomer.h +++ b/src/LoginCustomer.h @@ -1,3 +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 *);