From 9a4597511c39a1707f713b9bfcc4d39971a66840 Mon Sep 17 00:00:00 2001 From: theskywinds Date: Mon, 16 Jun 2025 12:47:25 +0200 Subject: [PATCH] More implementation of XML & bunch of fixes & changes around the name algorithm --- libs/AtmAdmin.h | 1 + libs/XMLStorage.h | 12 ++++------ src/AtmAdmin.cpp | 55 +++++++++++++++++++++++++++++++++++----------- src/XMLStorage.cpp | 49 +++++++++++++++++++++-------------------- 4 files changed, 72 insertions(+), 45 deletions(-) diff --git a/libs/AtmAdmin.h b/libs/AtmAdmin.h index 120d80f..6bd2ada 100644 --- a/libs/AtmAdmin.h +++ b/libs/AtmAdmin.h @@ -5,6 +5,7 @@ #include "XMLStorage.h" + namespace AtmAdmin { // startup() is inline because of how small it is. char startup(); diff --git a/libs/XMLStorage.h b/libs/XMLStorage.h index ccd91ce..935bff2 100644 --- a/libs/XMLStorage.h +++ b/libs/XMLStorage.h @@ -17,22 +17,18 @@ namespace AtmAdmin { this->name ={m_data.child_value("name") }; 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 updateNameData (); + void updatePinData (); pugi::xml_node initAccountNode (const std::string& toSearch); std::string name{}; double money{}; - int pin{}; - - // ~XMLStorage() { - // updateXMLData(m_search); - // } - + std::string pin{}; private: - // void updateXMLData (const std::string& toSearch); // Is currently unused. pugi::xml_document m_doc; const std::string m_search; pugi::xml_node m_data; diff --git a/src/AtmAdmin.cpp b/src/AtmAdmin.cpp index 387e052..c8f5693 100644 --- a/src/AtmAdmin.cpp +++ b/src/AtmAdmin.cpp @@ -44,14 +44,17 @@ namespace AtmAdmin { std::cout << "Is this okay? (y or n) >: "; std::cin.get(userCheck); userCheck = static_cast(std::tolower(userCheck)); + if (userCheck == 'y' and choice == PIN) { - mainStorage.pin = stoi(tempInformation); + mainStorage.pin = tempInformation; + mainStorage.updatePinData(); std::cout << "New pin applied." << '\n'; loopDone = true; } else if (userCheck == 'y' and choice == NAME) { mainStorage.name = tempInformation; + mainStorage.updateNameData(); std::cout << "New name applied." << '\n'; loopDone = true; } @@ -67,6 +70,7 @@ namespace AtmAdmin { } } while (!loopDone); + } // Controls the flow of the program based on the user's input. @@ -186,29 +190,54 @@ namespace AtmAdmin { } std::string changeAccountName() { + std::string firstName{}; + std::string lastName{}; std::string newName{}; - - std::cout << "Enter new name: "; 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. - if (newName.length() < 2 or newName.length() > 15) { - errorReport("Name has to be between 2 and 15 characters."); - return "invalid"; + // CONSTRAINTS FOR FIRST NAME + if (firstName.empty()) { + errorReport("First name cannot be empty."); } - // Checks if any spaces are in the name, if so, reject it. - if ( std::ranges::any_of(newName.cbegin(), newName.cend(), [] (char input) { + // Checks for letters that aren't alphabetical. + 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 == ' '; })) { - errorReport("Name cannot contain spaces."); + errorReport("First & last name cannot contain spaces."); return "invalid"; } + // ENDING CONSTRAINTS FOR FIRST NAME - // Turns all the letters into lowercase. - std::ranges::transform (newName.cbegin(), newName.cend(), newName.begin(), [] (unsigned char input) { return tolower(input); }); + std::cout << "Enter your last name(s): "; + 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; } } diff --git a/src/XMLStorage.cpp b/src/XMLStorage.cpp index 65145d3..4763855 100644 --- a/src/XMLStorage.cpp +++ b/src/XMLStorage.cpp @@ -26,29 +26,30 @@ namespace AtmAdmin { return data; } - // void XMLStorage::updateXMLData (const std::string& toSearch) { - // // I wish existed, but this will have to do. - // auto doubleToFinal = [] (double input) - // { - // std::ostringstream stream; - // stream << std::fixed << std::setprecision(2) << input; - // return stream.str(); - // }; - // - // pugi::xml_node data {getXMLUserData(toSearch)}; - // data.child("money").text().set(doubleToFinal(this->money)); - // - // // DEBUG OUTPUT - // // std::cout << data.name() << ' ' << data.child("money").text().get() << '\n'; - // - // data = getXMLAdminData(); - // data.child("money").text().set(doubleToFinal(this->bank)); - // - // // DEBUG OUTPUT - // // std::cout << data.name() << ' ' << data.child("money").text().get(); - // - // std::cout.flags (std::ios::boolalpha); - // std::cout << "Bank saved: " << m_doc.save_file("./data/users.xml") << '.'; - // } + void XMLStorage::updateNameData () { + m_data.child("name").text().set(this->name); + + + std::cout.flags (std::ios::boolalpha); + bool saveSuccess {m_doc.save_file("./data/users.xml")}; + + if (!saveSuccess) { + std::cerr << "Failed to apply. Aborting..."; + std::exit(1); + } + } + + void XMLStorage::updatePinData () { + m_data.child("pin").text().set(this->pin); + + + std::cout.flags (std::ios::boolalpha); + bool saveSuccess {m_doc.save_file("./data/users.xml")}; + + if (!saveSuccess) { + std::cerr << "Failed to apply. Aborting..."; + std::exit(1); + } + } }