// // Created by Nik on 20/05/2022. // #include "Student.h" #include #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; Address address; while (std::getline(file,line)){ std::stringstream word(line); std::string tmp; try { std::getline(word,tmp,','); id = std::stoi(tmp); std::getline(word,tmp,','); name = tmp; std::getline(word,tmp,','); surname = tmp; std::getline(word,tmp,','); Date dateOfBirth = Date::getDateFromString(tmp); 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); }catch (UnparseableDateException e){ std::cout << e.what() << "\n"; }catch (InvalidDateException e){ std::cout << e.what() << "\n"; } } file.close(); return ret; } void Student::SaveToFile(const std::vector> &students, const std::string &fileName) { std::ofstream file (fileName); for (int i = 0; i < students.size(); ++i) { file << students[i]->toString(); } file.close(); }