From ac4b64166e22e13d84cbcb54ae071125302817b5 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Mon, 30 Jan 2023 19:44:01 +0100 Subject: [PATCH 1/3] implement the function menuInput(), in order for the user to choose a banking option. --- src/mainMenu.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++- src/mainMenu.h | 1 + 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index eca4c34..fdb0aa8 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -54,12 +54,13 @@ void ageInput() if((checkIfInteger(userInput))&& (agePermission(age))) { - //age = strtol(userInput,&userInputPointer,10); printf("Access granted!\n\n\n\n"); showMenu(); + menuInput(); + break; } @@ -84,6 +85,57 @@ void ageInput() } } +void menuInput() +{ + + char choiceInput[20]; + + char* choiceInputPointer; + + int selection; + + scanf("%s",choiceInput); + + while(true) + { + selection = strtol(choiceInput,&choiceInputPointer,10); + + if(chooseOption(selection) == true && checkIfInteger(choiceInput) == true) + { + switch(selection) + { + + case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + + break; + + case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); + + break; + + case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); + + break; + + case 4 : printf("\e[1;1H\e[2J"); + + printf("\nsee you next time !\n\n"); + + break; + + } + break; + } + + else + { + printf("Input invalid! try again!\n"); + + scanf("%s",choiceInput); + } + } + +} void showMenu() diff --git a/src/mainMenu.h b/src/mainMenu.h index 6974d21..0824d42 100644 --- a/src/mainMenu.h +++ b/src/mainMenu.h @@ -12,6 +12,7 @@ bool chooseOption(int choiceInput); void ageInput(); void showMenu(); +void menuInput(); #endif From 0e8d805605d65c8352f05aefa025ef31386a9a69 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Mon, 30 Jan 2023 20:01:49 +0100 Subject: [PATCH 2/3] refactoring: made code more readable and enhanced the menuInput() and chooseOption() functions. --- src/mainMenu.c | 70 +++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index fdb0aa8..7b60ecf 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -24,15 +24,7 @@ bool checkIfInteger(char* userInput) bool chooseOption(int choiceInput) { - if(choiceInput < 1 || choiceInput > 4) - { - return false; - } - - else - { - return true; - } + return !(choiceInput < 1 || choiceInput > 4); } @@ -50,6 +42,8 @@ void ageInput() while (true) { + /*the userInput string is changed to a number with the strtol function*/ + age = strtol(userInput,&userInputPointer,10); if((checkIfInteger(userInput))&& (agePermission(age))) @@ -85,56 +79,40 @@ void ageInput() } } + void menuInput() { - char choiceInput[20]; - char* choiceInputPointer; - int selection; - scanf("%s",choiceInput); + scanf("%s", choiceInput); + selection = strtol(choiceInput, &choiceInputPointer, 10); - while(true) + while (!checkIfInteger(choiceInput) || !chooseOption(selection)) { - selection = strtol(choiceInput,&choiceInputPointer,10); - - if(chooseOption(selection) == true && checkIfInteger(choiceInput) == true) - { - switch(selection) - { + printf("Input invalid! try again!\n"); - case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + scanf("%s", choiceInput); - break; - - case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); - - break; - - case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); - - break; - - case 4 : printf("\e[1;1H\e[2J"); - - printf("\nsee you next time !\n\n"); - - break; - - } - break; - } + selection = strtol(choiceInput, &choiceInputPointer, 10); + } + + switch(selection) + { + case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + break; - else - { - printf("Input invalid! try again!\n"); + case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); + break; - scanf("%s",choiceInput); - } - } + case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); + break; + case 4 : printf("\e[1;1H\e[2J"); + printf("\nsee you next time !\n\n"); + break; + } } From eee36c1fda7192a609446d0b0e8e9d785544a21d Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Tue, 31 Jan 2023 14:30:31 +0100 Subject: [PATCH 3/3] refactoring: removed redundant code in the unit tests of the chooseOption() function. --- test/test_mainMenu.c | 47 +++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c index ea65cc2..af93eaf 100644 --- a/test/test_mainMenu.c +++ b/test/test_mainMenu.c @@ -134,25 +134,23 @@ void test_validChoiceInput(void) int validInput[4]; - bool validInputResult[4]; + bool validInputExpected = true; - /*Act*/ + for(int i = 0; i < 4; i++) { validInput[i] = i + 1; } + /*Act and Asssert*/ + for(int i = 0; i < 4; i++) { - validInputResult[i] = chooseOption(validInput[i]); - } - /*Assert*/ + bool validInputResult = chooseOption(validInput[i]); - for(int i = 0; i < 4; i++) - { - TEST_ASSERT_TRUE(validInputResult[i]); + TEST_ASSERT_EQUAL(validInputExpected,validInputResult); } } @@ -165,25 +163,24 @@ void test_invalidChoiceInput_firstCase(void) int invalidInput[100]; - bool invalidInputResult[100]; + bool invalidInputExpected = false; - /*Act*/ + - for(int i = -100; i < 0; i++) + for(int i = -100; i < 1; i++) { invalidInput[i+100] = i; } + /*Act and Assert*/ + for(int i = 0; i < 100; i++) { - invalidInputResult[i] = chooseOption(invalidInput[i]); - } - /*Assert*/ + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); - for(int i = 0; i < 100; i++) - { - TEST_ASSERT_FALSE(invalidInputResult[i]); } } @@ -196,27 +193,23 @@ void test_invalidChoiceInput_secondCase(void) int invalidInput[100]; - bool invalidInputResult[100]; - - /*Act*/ + bool invalidInputExpected = false; for(int i = 0; i < 100; i++) { invalidInput[i] = i + 5; } - for(int i = 0; i < 100; i++) - { - invalidInputResult[i] = chooseOption(invalidInput[i]); - } - - /*Assert*/ + /*Act and Assert*/ for(int i = 0; i < 100; i++) { - TEST_ASSERT_FALSE(invalidInputResult[i]); + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); } + }