diff --git a/build-project.sh b/build-project.sh index ea712c3..949b9be 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,6 +1,7 @@ clear ceedling test:all cd src/ -gcc -o main main.c mainMenu.c -./main -rm main +gcc main.c createCustomer.c -o prog +./prog +rm prog +cd .. diff --git a/project.yml b/project.yml index e253248..640cb8f 100644 --- a/project.yml +++ b/project.yml @@ -31,12 +31,12 @@ :paths: :test: - - +:test/** - - -:test/support + - +:tests/** + - -:tests/support :source: - src/** :support: - - test/support + - tests/support :libraries: [] :defines: diff --git a/src/createCustomer.c b/src/createCustomer.c new file mode 100644 index 0000000..dbc78d6 --- /dev/null +++ b/src/createCustomer.c @@ -0,0 +1,93 @@ +#include "createCustomer.h" +/*Code written by Julius Philipp Engel, fdai7057*/ +int generateID() +{ + srand(clock()); + const int MIN = 1000000, MAX = 9000001; + int pseudoRandomIDForCustomer = (rand() % MAX) + MIN; + return pseudoRandomIDForCustomer; +} + +void collectCustomerProperties() +{ + customer_t instance; + instance.forename = calloc(15+1,sizeof(char)); + instance.surname = calloc(15+1,sizeof(char)); + instance.password = calloc(20+1,sizeof(char)); + instance.ID = generateID(); + int letterCounter = 0; + int letterMaximum = 15; + char userInput=' '; + bool inputTooLong = false; + printf("To create a new user, enter the information required below.\n"); + printf("Enter forename (max. 15 letters):\n"); + while(letterCounter=letterMaximum){ + inputTooLong = true; + break; + } + } + if(inputTooLong){ + printf("Forename too long. Aborting.\n"); + exit(-1); + }else{ + *(instance.forename+letterCounter) = '\0'; + letterCounter = 0; + } + printf("Enter surname (max. 15 letters):\n"); + while(letterCounter=letterMaximum){ + inputTooLong = true; + break; + } + } + if(inputTooLong){ + printf("Surname too long. Aborting.\n"); + exit(-1); + }else{ + *(instance.surname+letterCounter) = '\0'; + letterCounter = 0; + } + printf("Enter password (max. 20 letters):\n"); + while(letterCounter=letterMaximum){ + inputTooLong = true; + break; + } + } + if(inputTooLong){ + printf("Password too long. Aborting.\n"); + exit(-1); + } + *(instance.password+letterCounter) = '\0'; + letterCounter = 0; + printf("Enter balance:\n"); + scanf("%f",&instance.balance); + + printf("Account successfully created. Your ID is: %d. Note it somewhere!\n",instance.ID); + customer_t *referenceToCustomerInstance = &instance; + writeCustomerPropertiesToFile(referenceToCustomerInstance); +} + +void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance) +{ + FILE *customerData = fopen("CustomerData.txt","a"); + if(customerData!=NULL){ + /*calling generateCheckString() at this place in future*/ + fprintf(customerData,"ID=%d\nForename=%s\nSurname=%s\nPassword=%s\nBalance=%.4f€\n\n", + referenceToCustomerInstance->ID,referenceToCustomerInstance->forename, + referenceToCustomerInstance->surname,referenceToCustomerInstance->password, + referenceToCustomerInstance->balance); + fclose(customerData); + } + else{ + printf("Error when accessing the file.\n"); + exit(-1); + } +} diff --git a/src/createCustomer.h b/src/createCustomer.h new file mode 100644 index 0000000..bd58756 --- /dev/null +++ b/src/createCustomer.h @@ -0,0 +1,8 @@ +#include +#include +#include +#include +#include "customerProperties.h" +int generateID(); +void collectCustomerProperties(); +void writeCustomerPropertiesToFile(customer_t *); diff --git a/src/customerProperties.h b/src/customerProperties.h new file mode 100644 index 0000000..0463c2d --- /dev/null +++ b/src/customerProperties.h @@ -0,0 +1,7 @@ +typedef struct Customer +{ + unsigned int ID; + char *password; + char *forename, *surname; + float balance; +}customer_t; diff --git a/src/main.c b/src/main.c index b37734e..84a49ac 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,4 @@ -#include "mainMenu.h" - -int main() { - - ageInput(); - - return 0; +#include "createCustomer.h" +int main(){ + collectCustomerProperties(); } diff --git a/src/mainMenu.c b/src/mainMenu.c index 7b60ecf..78611ea 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -1,4 +1,4 @@ -#include "mainMenu.h" +#include"mainMenu.h" bool agePermission(int age) @@ -52,6 +52,8 @@ void ageInput() printf("Access granted!\n\n\n\n"); showMenu(); + + menuInput(); menuInput(); @@ -129,4 +131,3 @@ void showMenu() } - diff --git a/src/mainMenu.h b/src/mainMenu.h index 0824d42..4907629 100644 --- a/src/mainMenu.h +++ b/src/mainMenu.h @@ -11,6 +11,7 @@ bool checkIfInteger(char* userInput); bool chooseOption(int choiceInput); void ageInput(); +void menuInput(); void showMenu(); void menuInput(); diff --git a/tests/support/.gitkeep b/tests/support/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_CreateCustomer.c b/tests/test_CreateCustomer.c new file mode 100644 index 0000000..5cf0933 --- /dev/null +++ b/tests/test_CreateCustomer.c @@ -0,0 +1,26 @@ +#include +#include +#include "createCustomer.h" + +void setUp(){} +void tearDown(){} + +void test_generateID(){ + + const int test_values = USHRT_MAX; + + /*initialize blocks by calling generateID()*/ + int *numbers = calloc(test_values, sizeof(int)); + for(int i=0;i