diff --git a/README.md b/README.md index d90868b..cd30527 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This program will be terminal-only. ## Current plans - [x] Allow the user to choose different options (Changing pin, name, etc) -- [ ] Make a prototype. Allow variables to be stored & displayed. +- [x] Make a prototype. Allow variables to be stored & displayed. - [ ] Intergrate it into XML. For now, only allow PIN & money to be changed. - [ ] Add a way to create new users. - [ ] (MAYBE) Add a way to delete users. diff --git a/src/AtmAdmin.cpp b/src/AtmAdmin.cpp index 56b2109..14ab24a 100644 --- a/src/AtmAdmin.cpp +++ b/src/AtmAdmin.cpp @@ -1,11 +1,19 @@ #include #include #include +#include #include "AtmAdmin.h" #include "Errorseal.h" namespace AtmAdmin { + // Used to make certain functions more concise. + enum Choice { + PIN, + NAME, + MONEY, + }; + 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"; @@ -16,29 +24,65 @@ namespace AtmAdmin { return choice; } + void confirmUserInformation (std::string& information, const std::string& tempInformation, enum Choice choice) { + char userCheck; + bool loopDone = true; + + do { + if (choice == PIN) + std::cout << "Your new pin will be: " << tempInformation << '\n'; + else if (choice == NAME) + std::cout << "Your new name will be: " << tempInformation << '\n'; + std::cout << "Is this okay? (y or n) >: "; + std::cin.get(userCheck); + userCheck = static_cast(std::tolower(userCheck)); + if (userCheck == 'y') { + information = tempInformation; + std::cout << "New name applied." << '\n'; + loopDone = true; + } + + else if (userCheck == 'n') { + std::cout << "Discarding..." << '\n'; + loopDone = true; + } + + else { + std::cout << "Invalid choice." << "\n\n"; + loopDone = false; + } + + } while (!loopDone); + } + // 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': + // changeAccountPin is a function found later in the code. + // It is both the input, and algorithm for the new pin. + + // The algorithm returns "invalid" when something isn't as it should be. + // This is handled by the if statement. tempInformation = changeAccountPin(); if (tempInformation != "invalid") { - information = tempInformation; + // Checks with the user if the name is okay, if so, it applies it. + confirmUserInformation(information, tempInformation, PIN); 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. + // changeAccountName is a function found later in the code. + // It is both the input, and algorithm for the new name. - // The code below is a placeholder + // The algorithm returns "invalid" when something isn't as it should be. + // This is handled by the if statement. tempInformation = changeAccountName(); + if (tempInformation != "invalid") { - information = tempInformation; + confirmUserInformation(information, tempInformation, NAME); return true; } break; @@ -136,12 +180,12 @@ namespace AtmAdmin { std::cout << "Enter new name: "; - std::cin >> newName; Error::ignoreLine(); + std::getline(std::cin, newName); // Ensures the name is between 2 and 10 characters long. - if (newName.length() < 2 or newName.length() > 10) { - errorReport("Name has to be between 2 and 10 characters."); + if (newName.length() < 2 or newName.length() > 20) { + errorReport("Name has to be between 2 and 20 characters."); return "invalid"; } @@ -151,6 +195,14 @@ namespace AtmAdmin { return "invalid"; } + // Checks if any spaces are in the name, if so, reject it. + if ( std::ranges::any_of(newName.cbegin(), newName.cend(), [] (char input) { + return input == ' '; + })) { + errorReport("Name cannot contain spaces."); + return "invalid"; + } + // Turns all the letters into lowercase. std::ranges::transform (newName.cbegin(), newName.cend(), newName.begin(), [] (unsigned char input) { return tolower(input); }); return newName; diff --git a/src/main.cpp b/src/main.cpp index 5ca2ef4..f1855e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,15 +9,14 @@ int main() { bool continueLoop{true}; while (continueLoop) { - // startup() presents the user with the choices below, which are then stored and used by other functions. + // startup() presents the user with the choices below. // The other functions should handle invalid choices. // ((1. Change pin || 2. Change money || 3. Change account name || Q. Exit the program)) char userChoice{AtmAdmin::startup()}; - - // userInformation will later store information for the XML entry to save. std::string userInformation{}; - // Always returns true unless 'Q' is entered + // The bulk of the program, allowing user to interact with the program. + // Always returns true unless 'Q' is entered. continueLoop = AtmAdmin::handleUserInformation(static_cast(std::tolower(userChoice)), userInformation); std::cout << userInformation << '\n'; // DEBUG