More implementation of XML & bunch of fixes & changes around the name algorithm

This commit is contained in:
theskywinds 2025-06-16 12:47:25 +02:00
parent 9071166d9e
commit 9a4597511c
4 changed files with 72 additions and 45 deletions

View File

@ -5,6 +5,7 @@
#include "XMLStorage.h" #include "XMLStorage.h"
namespace AtmAdmin { namespace AtmAdmin {
// startup() is inline because of how small it is. // startup() is inline because of how small it is.
char startup(); char startup();

View File

@ -17,22 +17,18 @@ namespace AtmAdmin {
this->name ={m_data.child_value("name") }; this->name ={m_data.child_value("name") };
this->money ={std::stod(m_data.child_value("money"))}; this->money ={std::stod(m_data.child_value("money"))};
this->pin ={std::stoi(m_data.child_value("pin")) }; this->pin ={m_data.child_value("pin")};
} }
void initXMLDoc (); void initXMLDoc ();
void updateNameData ();
void updatePinData ();
pugi::xml_node initAccountNode (const std::string& toSearch); pugi::xml_node initAccountNode (const std::string& toSearch);
std::string name{}; std::string name{};
double money{}; double money{};
int pin{}; std::string pin{};
// ~XMLStorage() {
// updateXMLData(m_search);
// }
private: private:
// void updateXMLData (const std::string& toSearch); // Is currently unused.
pugi::xml_document m_doc; pugi::xml_document m_doc;
const std::string m_search; const std::string m_search;
pugi::xml_node m_data; pugi::xml_node m_data;

View File

@ -44,14 +44,17 @@ namespace AtmAdmin {
std::cout << "Is this okay? (y or n) >: "; std::cout << "Is this okay? (y or n) >: ";
std::cin.get(userCheck); std::cin.get(userCheck);
userCheck = static_cast<char>(std::tolower(userCheck)); userCheck = static_cast<char>(std::tolower(userCheck));
if (userCheck == 'y' and choice == PIN) { if (userCheck == 'y' and choice == PIN) {
mainStorage.pin = stoi(tempInformation); mainStorage.pin = tempInformation;
mainStorage.updatePinData();
std::cout << "New pin applied." << '\n'; std::cout << "New pin applied." << '\n';
loopDone = true; loopDone = true;
} }
else if (userCheck == 'y' and choice == NAME) { else if (userCheck == 'y' and choice == NAME) {
mainStorage.name = tempInformation; mainStorage.name = tempInformation;
mainStorage.updateNameData();
std::cout << "New name applied." << '\n'; std::cout << "New name applied." << '\n';
loopDone = true; loopDone = true;
} }
@ -67,6 +70,7 @@ namespace AtmAdmin {
} }
} while (!loopDone); } 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.
@ -186,29 +190,54 @@ namespace AtmAdmin {
} }
std::string changeAccountName() { std::string changeAccountName() {
std::string firstName{};
std::string lastName{};
std::string newName{}; std::string newName{};
std::cout << "Enter new name: ";
Error::ignoreLine(); Error::ignoreLine();
std::getline(std::cin, newName); std::cout << "Enter your first name: ";
std::getline(std::cin, firstName, '\n');
// Ensures the name is between 2 and 15 characters long. // CONSTRAINTS FOR FIRST NAME
if (newName.length() < 2 or newName.length() > 15) { if (firstName.empty()) {
errorReport("Name has to be between 2 and 15 characters."); errorReport("First name cannot be empty.");
return "invalid";
} }
// Checks if any spaces are in the name, if so, reject it. // Checks for letters that aren't alphabetical.
if ( std::ranges::any_of(newName.cbegin(), newName.cend(), [] (char input) { if (std::ranges::any_of(firstName.cbegin(), firstName.cend(), [] (unsigned char input) {
return !std::isalpha(input) and !std::isspace(input);
})) {
errorReport("Name cannot contain non-alphabetic letters.");
return ("invalid");
}
// Checks for any spaces in the first name.
if (std::ranges::any_of(firstName.cbegin(), firstName.cend(), [] (char input) {
return input == ' '; return input == ' ';
})) { })) {
errorReport("Name cannot contain spaces."); errorReport("First & last name cannot contain spaces.");
return "invalid"; return "invalid";
} }
// ENDING CONSTRAINTS FOR FIRST NAME
// Turns all the letters into lowercase. std::cout << "Enter your last name(s): ";
std::ranges::transform (newName.cbegin(), newName.cend(), newName.begin(), [] (unsigned char input) { return tolower(input); }); std::getline(std::cin, lastName, '\n');
// CONSTRAINTS FOR LAST NAME
if (isspace(lastName.at(0))) {
errorReport("Last name cannot start with a space.");
return ("invalid");
}
// ENDING CONSTRAINTS FOR LAST NAME
newName = firstName + " " + lastName;
// Ensures the name is between 2 and 15 characters long.
if (newName.length() > 40) {
errorReport("Name has to be less than 40 characters.");
return "invalid";
}
return newName; return newName;
} }
} }

View File

@ -26,29 +26,30 @@ namespace AtmAdmin {
return data; return data;
} }
// void XMLStorage::updateXMLData (const std::string& toSearch) { void XMLStorage::updateNameData () {
// // I wish <format> existed, but this will have to do. m_data.child("name").text().set(this->name);
// auto doubleToFinal = [] (double input)
// {
// std::ostringstream stream; std::cout.flags (std::ios::boolalpha);
// stream << std::fixed << std::setprecision(2) << input; bool saveSuccess {m_doc.save_file("./data/users.xml")};
// return stream.str();
// }; if (!saveSuccess) {
// std::cerr << "Failed to apply. Aborting...";
// pugi::xml_node data {getXMLUserData(toSearch)}; std::exit(1);
// data.child("money").text().set(doubleToFinal(this->money)); }
// }
// // DEBUG OUTPUT
// // std::cout << data.name() << ' ' << data.child("money").text().get() << '\n'; void XMLStorage::updatePinData () {
// m_data.child("pin").text().set(this->pin);
// data = getXMLAdminData();
// data.child("money").text().set(doubleToFinal(this->bank));
// std::cout.flags (std::ios::boolalpha);
// // DEBUG OUTPUT bool saveSuccess {m_doc.save_file("./data/users.xml")};
// // std::cout << data.name() << ' ' << data.child("money").text().get();
// if (!saveSuccess) {
// std::cout.flags (std::ios::boolalpha); std::cerr << "Failed to apply. Aborting...";
// std::cout << "Bank saved: " << m_doc.save_file("./data/users.xml") << '.'; std::exit(1);
// } }
}
} }