Added an algorithm to changeAccountPin()

This commit is contained in:
theskywinds 2025-06-02 12:37:01 +02:00
parent 4817c36eae
commit 42d4704b0f
3 changed files with 104 additions and 57 deletions

View File

@ -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

View File

@ -1,6 +1,12 @@
#include <iostream>
#include "Errorseal.h"
#include "AtmAdmin.h"
#include <algorithm>
#include <oneapi/tbb/detail/_task.h>
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;
}
}

View File

@ -1,65 +1,9 @@
#include <iostream>
#include <cctype>
#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<char>(std::tolower(userChoice)), userInformation);
continueLoop = AtmAdmin::handleUserInformation(static_cast<char>(std::tolower(userChoice)), userInformation);
std::cout << userInformation << '\n'; // DEBUG
}