More implementation of XML & bunch of fixes & changes around the name algorithm
This commit is contained in:
parent
9071166d9e
commit
9a4597511c
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "XMLStorage.h"
|
||||
|
||||
|
||||
namespace AtmAdmin {
|
||||
// startup() is inline because of how small it is.
|
||||
char startup();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -44,14 +44,17 @@ namespace AtmAdmin {
|
|||
std::cout << "Is this okay? (y or n) >: ";
|
||||
std::cin.get(userCheck);
|
||||
userCheck = static_cast<char>(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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,29 +26,30 @@ namespace AtmAdmin {
|
|||
return data;
|
||||
}
|
||||
|
||||
// void XMLStorage::updateXMLData (const std::string& toSearch) {
|
||||
// // I wish <format> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user