// // Created by Nik on 20/05/2022. // #include "Student.h" #include Student::Student(unsigned int id, std::string name, std::string surname, Date date, Address address) : id(id), name(name), surname(surname), dateOfBirth(date), address(address) {} std::string Student::toString() const { return std::to_string(id) + "," + name + "," + surname + "," + dateOfBirth.toString() + "," + address.toString() + "\n"; } std::vector> Student::LoadFromFile(const std::string &fileName) { std::vector> ret; std::ifstream file (fileName); std::string line; unsigned int id; std::string name, surname; Date dateOfBirth; Address address; while (std::getline(file,line)){ std::stringstream word(line); std::string tmp; std::getline(word,tmp,','); id = std::stoi(tmp); std::getline(word,tmp,','); name = tmp; std::getline(word,tmp,','); surname = tmp; std::getline(word,tmp,','); try { dateOfBirth = Date::getDateFromString(tmp); }catch (UnparseableDateException e){ Log(LogType::INFO) << e.toString() << "\n"; dateOfBirth = {0,0,0}; } std::getline(word,tmp,','); address.setStreet(tmp); std::getline(word,tmp,','); address.setPost(tmp); std::getline(word,tmp,','); address.setCountry(tmp); std::shared_ptr tmppoint = std::make_shared(id,name,surname,dateOfBirth,address); ret.push_back(tmppoint); } file.close(); return ret; } void Student::SaveToFile(const std::vector> &students, const std::string &fileName) { std::ofstream file (fileName); file.clear(); for (int i = 0; i < students.size(); ++i) { file << students[i]->toString(); } file.close(); }