ASDV-Cpp/Assignments/MP4_CalebFontenot/userData.cpp

87 lines
2.9 KiB
C++

//
// Created by caleb on 4/10/24.
//
#include <fstream>
#include <cstdio>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/vector.hpp>
#include "userData.h"
void enterRecords(std::vector<userData> data);
userData findRecord(std::vector<userData> data);
void changeEntry(userData entry);
std::string displayData(std::vector<userData> data) {
std::string returnString = "";
std::string DASHES = "------------";
std::printf("vector size: %i\n", static_cast<int>(data.size()));
for (int i = 0; i < data.size(); ++i) {
userData currentUserData = data[i];
returnString.append(DASHES).append(" line ").append(std::to_string(i + 1)).append(DASHES).append("\n");
returnString.append("Name: ").append(currentUserData.name).append("\n");
returnString.append("Address: ").append(currentUserData.address).append("\n");
returnString.append("City: ").append(currentUserData.city).append("\n");
returnString.append("State: ").append(currentUserData.state).append("\n");
returnString.append("ZIP: ").append(currentUserData.zip).append("\n");
returnString.append("Phone #: ").append(currentUserData.phone).append("\n");
//returnString.append("Account Balance : $").append(currentUserData.accountBal).append("\n");
returnString.append("Date of Last Payment: ").append(ctime(&currentUserData.dateOfLastPayment)).append("\n");
}
return returnString;
}
inline bool fileExists (const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
void updateData(std::vector<userData> data) {
// Serialize the vector
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << data;
// Save the contents
std::ofstream outputFile("data.dat");
if (outputFile.is_open()) {
outputFile << ss.str();
outputFile.close();
}
}
std::vector<userData> getData() {
std::vector<userData> data;
if (!fileExists("data.dat")) {
// create struct with dummy data
// TODO: call enterRecords() to get this data
userData* pUserData = new userData();
pUserData->name = "dummy name";
pUserData->address = "dummy address";
pUserData->city = "dummy city";
pUserData->phone = "867-5309";
pUserData->state = "Los Angeles";
pUserData->city = "Hollywood";
pUserData->dateOfLastPayment = 1712851395;
data.emplace_back(*pUserData);
updateData(data);
} else {
// Deserialize and read the data from the file
std::ifstream inputFile("data.dat", std::ios::binary);
boost::archive::text_iarchive ia(inputFile);
ia >> data;
}
return data;
}