From 42d4704b0f6d29e53ab5d5eb4cd3941a28a72c3b Mon Sep 17 00:00:00 2001 From: theskywinds Date: Mon, 2 Jun 2025 12:37:01 +0200 Subject: [PATCH] Added an algorithm to changeAccountPin() --- libs/AtmAdmin.h | 2 + src/AtmAdmin.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 58 +-------------------------- 3 files changed, 104 insertions(+), 57 deletions(-) diff --git a/libs/AtmAdmin.h b/libs/AtmAdmin.h index 0e301cf..303a406 100644 --- a/libs/AtmAdmin.h +++ b/libs/AtmAdmin.h @@ -6,6 +6,8 @@ namespace AtmAdmin { // startup() is inline because of how small it is. char startup(); + bool handleUserInformation(char choice, std::string& information); + std::string changeAccountPin(); } #endif //ATMADMIN_H diff --git a/src/AtmAdmin.cpp b/src/AtmAdmin.cpp index 6392b69..4ed66b1 100644 --- a/src/AtmAdmin.cpp +++ b/src/AtmAdmin.cpp @@ -1,6 +1,12 @@ #include +#include "Errorseal.h" +#include "AtmAdmin.h" + +#include +#include namespace AtmAdmin { + char startup() { char choice{}; std::cout << "1. Change PIN\n" << "2. Change account name\n" << "3. Change money\n" << "Q. Exit the program\n\n"; @@ -10,4 +16,99 @@ namespace AtmAdmin { return choice; } + + // Controls the flow of the program based on the user's input. + bool handleUserInformation(char choice, std::string& information) { + std::string tempInformation{}; + + switch (choice) { + case '1': + // Implement a proper function for entering a new pin. + // The function should have a somewhat complex algorithm that forces the pin to be secure and unique. + // It should also enforce 4 numbers. + + // The code below is a placeholder. + tempInformation = changeAccountPin(); + if (tempInformation != "invalid") { + information = tempInformation; + return true; + } + break; + case '2': + // Implement a function for entering a new username. + // The function should check if the name isn't already taken. If it is, return to start. + // It will also use an algorithm to check if it's a valid username + // This includes no spaces and no starting numbers. + // Ensure that the name is also converted to fully lowercase. + + // The code below is a placeholder + std::cout << "Enter your new username: "; + std::cin >> information; + break; + case '3': + // Simply a way to cheat, very straight-forward. + + // The code below is a placeholder + std::cout << "Enter the amount you wish to have: "; + std::cin >> information; + break; + case 'q': + return false; + default: + std::cout << "Invalid choice.\n"; + } + return true; + } + + std::string changeAccountPin() { + std::string newPin{}; + + // Allows for the quick and clean creation of error reports. + auto errorReport = [] (std::string_view error) { + std::cout << error << '\n'; + std::cout << "Press enter to continue..."; + std::cin.get(); + return "invalid"; + }; + + std::cout << "Enter new pin: "; + std::cin >> newPin; + Error::ignoreLine(); + + if (newPin.size() != 4) { + errorReport("The pin has to be 4 numbers long."); + return "invalid"; + } + + if (newPin[0] == '0') { + errorReport("The first number cannot be \'0\'"); + return "invalid"; + } + + // Checks if anything put into the pin is alphabetic. + for (size_t i = 0; i < 4; i++) { + if (isalpha(newPin[i])) { + errorReport("The pin cannot contain alphabet letters."); + return "invalid"; + } + } + + { // Start of bracket scope + std::string tempPin = newPin; + std::ranges::sort(tempPin.begin(), tempPin.end()); + + + if ( (tempPin[0] == tempPin[1] and tempPin[1] == tempPin[2]) // Checks if the first 3 numbers are the same. + or + (tempPin[1] == tempPin[2] and tempPin[2] == tempPin[3]) // Checks if the last 3 numbers are the same. + or + (tempPin[0] == tempPin[1] and tempPin[2] == tempPin[3])) // Checks if the first 2 and last 2 numbers are the same. + { + errorReport("Too many duplicate numbers."); + return "invalid"; + } + } // End of bracket scope + + return newPin; + } } diff --git a/src/main.cpp b/src/main.cpp index de0b917..5ca2ef4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,65 +1,9 @@ #include #include -#include "Errorseal.h" #include "AtmAdmin.h" -std::string changeAccountPin() { - std::string newPin{}; - std::cout << "Enter new pin: "; - std::cin >> newPin; - Error::ignoreLine(); - if (newPin.size() != 4) { - std::cout << "The pin has to be 4 digits long.\n"; - std::cout << "Press enter to continue..."; - std::cin.get(); - return "invalid"; - } - return newPin; -} - -bool handleUserInformation(char choice, std::string& information) { - std::string tempInformation{}; - - switch (choice) { - case '1': - // Implement a proper function for entering a new pin. - // The function should have a somewhat complex algorithm that forces the pin to be secure and unique. - // It should also enforce 4 numbers. - - // The code below is a placeholder. - tempInformation = changeAccountPin(); - if (tempInformation != "invalid") { - information = tempInformation; - return true; - } - break; - case '2': - // Implement a function for entering a new username. - // The function should check if the name isn't already taken. If it is, return to start. - // It will also use an algorithm to check if it's a valid username - // This includes no spaces and no starting numbers. - // Ensure that the name is also converted to fully lowercase. - - // The code below is a placeholder - std::cout << "Enter your new username: "; - std::cin >> information; - break; - case '3': - // Simply a way to cheat, very straight-forward. - - // The code below is a placeholder - std::cout << "Enter the amount you wish to have: "; - std::cin >> information; - break; - case 'q': - return false; - default: - std::cout << "Invalid choice.\n"; - } - return true; -} int main() { bool continueLoop{true}; @@ -74,7 +18,7 @@ int main() { std::string userInformation{}; // Always returns true unless 'Q' is entered - continueLoop = handleUserInformation(static_cast(std::tolower(userChoice)), userInformation); + continueLoop = AtmAdmin::handleUserInformation(static_cast(std::tolower(userChoice)), userInformation); std::cout << userInformation << '\n'; // DEBUG }