consolidate all repos to one for archive
This commit is contained in:
66
semester_2/programiranje_2/naloga1102/Student.cpp
Normal file
66
semester_2/programiranje_2/naloga1102/Student.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "Student.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<std::shared_ptr<Student>> Student::LoadFromFile(const std::string &fileName) {
|
||||
|
||||
std::vector<std::shared_ptr<Student>> 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<Student> tmppoint = std::make_shared<Student>(id,name,surname,dateOfBirth,address);
|
||||
ret.push_back(tmppoint);
|
||||
}
|
||||
file.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Student::SaveToFile(const std::vector<std::shared_ptr<Student>> &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();
|
||||
}
|
||||
Reference in New Issue
Block a user