Fully implemented name algorithm & refined user control.
This commit is contained in:
parent
6ba17abcb1
commit
cbc3bb557d
|
@ -11,7 +11,7 @@ This program will be terminal-only.
|
||||||
## Current plans
|
## Current plans
|
||||||
|
|
||||||
- [x] Allow the user to choose different options (Changing pin, name, etc)
|
- [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.
|
- [ ] Intergrate it into XML. For now, only allow PIN & money to be changed.
|
||||||
- [ ] Add a way to create new users.
|
- [ ] Add a way to create new users.
|
||||||
- [ ] (MAYBE) Add a way to delete users.
|
- [ ] (MAYBE) Add a way to delete users.
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
#include "AtmAdmin.h"
|
#include "AtmAdmin.h"
|
||||||
#include "Errorseal.h"
|
#include "Errorseal.h"
|
||||||
|
|
||||||
namespace AtmAdmin {
|
namespace AtmAdmin {
|
||||||
|
// Used to make certain functions more concise.
|
||||||
|
enum Choice {
|
||||||
|
PIN,
|
||||||
|
NAME,
|
||||||
|
MONEY,
|
||||||
|
};
|
||||||
|
|
||||||
char startup() {
|
char startup() {
|
||||||
char choice{};
|
char choice{};
|
||||||
std::cout << "1. Change PIN\n" << "2. Change account name\n" << "3. Change money\n" << "Q. Exit the program\n\n";
|
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;
|
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<char>(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.
|
// Controls the flow of the program based on the user's input.
|
||||||
bool handleUserInformation(char choice, std::string& information) {
|
bool handleUserInformation(char choice, std::string& information) {
|
||||||
std::string tempInformation{};
|
std::string tempInformation{};
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
case '1':
|
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();
|
tempInformation = changeAccountPin();
|
||||||
if (tempInformation != "invalid") {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '2':
|
||||||
// Implement a function for entering a new username.
|
// changeAccountName is a function found later in the code.
|
||||||
// The function should check if the name isn't already taken. If it is, return to start.
|
// It is both the input, and algorithm for the new name.
|
||||||
// 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
|
// The algorithm returns "invalid" when something isn't as it should be.
|
||||||
|
// This is handled by the if statement.
|
||||||
tempInformation = changeAccountName();
|
tempInformation = changeAccountName();
|
||||||
|
|
||||||
if (tempInformation != "invalid") {
|
if (tempInformation != "invalid") {
|
||||||
information = tempInformation;
|
confirmUserInformation(information, tempInformation, NAME);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -136,12 +180,12 @@ namespace AtmAdmin {
|
||||||
|
|
||||||
|
|
||||||
std::cout << "Enter new name: ";
|
std::cout << "Enter new name: ";
|
||||||
std::cin >> newName;
|
|
||||||
Error::ignoreLine();
|
Error::ignoreLine();
|
||||||
|
std::getline(std::cin, newName);
|
||||||
|
|
||||||
// Ensures the name is between 2 and 10 characters long.
|
// Ensures the name is between 2 and 10 characters long.
|
||||||
if (newName.length() < 2 or newName.length() > 10) {
|
if (newName.length() < 2 or newName.length() > 20) {
|
||||||
errorReport("Name has to be between 2 and 10 characters.");
|
errorReport("Name has to be between 2 and 20 characters.");
|
||||||
return "invalid";
|
return "invalid";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,6 +195,14 @@ namespace AtmAdmin {
|
||||||
return "invalid";
|
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.
|
// Turns all the letters into lowercase.
|
||||||
std::ranges::transform (newName.cbegin(), newName.cend(), newName.begin(), [] (unsigned char input) { return tolower(input); });
|
std::ranges::transform (newName.cbegin(), newName.cend(), newName.begin(), [] (unsigned char input) { return tolower(input); });
|
||||||
return newName;
|
return newName;
|
||||||
|
|
|
@ -9,15 +9,14 @@ int main() {
|
||||||
bool continueLoop{true};
|
bool continueLoop{true};
|
||||||
|
|
||||||
while (continueLoop) {
|
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.
|
// The other functions should handle invalid choices.
|
||||||
// ((1. Change pin || 2. Change money || 3. Change account name || Q. Exit the program))
|
// ((1. Change pin || 2. Change money || 3. Change account name || Q. Exit the program))
|
||||||
char userChoice{AtmAdmin::startup()};
|
char userChoice{AtmAdmin::startup()};
|
||||||
|
|
||||||
// userInformation will later store information for the XML entry to save.
|
|
||||||
std::string userInformation{};
|
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<char>(std::tolower(userChoice)), userInformation);
|
continueLoop = AtmAdmin::handleUserInformation(static_cast<char>(std::tolower(userChoice)), userInformation);
|
||||||
|
|
||||||
std::cout << userInformation << '\n'; // DEBUG
|
std::cout << userInformation << '\n'; // DEBUG
|
||||||
|
|
Loading…
Reference in New Issue
Block a user