-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveLoadFile.cpp
115 lines (96 loc) · 3.93 KB
/
SaveLoadFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "SaveLoadFile.hpp"
#include <iostream>
SaveLoadFile::SaveLoadFile(std::shared_ptr<ValidationData> validation)
: validation_(validation)
{
}
void SaveLoadFile::operator()(std::vector<std::shared_ptr<Person>>& person) {
person.size();
std::cout << "SAVE LOAD HERE\n";
}
int SaveLoadFile::printSaveLoadMenu() {
int optionNumber = 0;
std::cout << ++optionNumber << ". Save(add to currently file new records (delete duplicate)\n";
std::cout << ++optionNumber << ". Save to new file \n";
std::cout << ++optionNumber << ". Clear existing data and load new data from file \n";
std::cout << ++optionNumber << ". Load from file and add records to existing file (delete duplicate)\n";
std::cout << ++optionNumber << ". Back\n";
return optionNumber;
}
SaveLoadFile::SaveLoadOptions SaveLoadFile::userChoice() {
auto menuSize = printSaveLoadMenu();
int userAnswer { };
std::cout << "Please insert your how you wanna do\n";
do {
std::cin >> userAnswer;
} while (validation_->isCorrectMenuChoice(userAnswer, menuSize) == false);
return static_cast<SaveLoadOptions>(userAnswer);
}
SaveLoadFile::SaveLoadOptions SaveLoadFile::saveLoadMenu(SaveLoadOptions userChoice, std::vector<std::shared_ptr<Person>>& person) {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (userChoice) {
case SaveLoadOptions::SaveToExist : {
saveToCurrentlyFile(person);
} break;
case SaveLoadOptions::SaveNew : {
saveToNewFile(person);
} break;
case SaveLoadOptions::LoadNew : {
loadToNewVector(person);
} break;
case SaveLoadOptions::LoadToExist : {
loadToExistVector(person);
} break;
case SaveLoadOptions::Back : {
std::cout << "BACK TO MAIN MENU\n";
} break;
default : {
std::cout << "ERROR ... WRONG ANSWER(SWITCH)\n";
} break;
}
return static_cast<SaveLoadOptions>(userChoice);
}
void SaveLoadFile::saveToCurrentlyFile(std::vector<std::shared_ptr<Person>>& person) {
saveMultiRecordsToFile(person);
}
void SaveLoadFile::saveToNewFile(std::vector<std::shared_ptr<Person>>& person) {
person.size(); // delete
}
void SaveLoadFile::loadToNewVector(std::vector<std::shared_ptr<Person>>& person) {
person.size(); // delete
}
void SaveLoadFile::loadToExistVector(std::vector<std::shared_ptr<Person>>& person) {
person.size(); // delete
}
void SaveLoadFile::saveSingleRecordToFile(std::vector<std::shared_ptr<Person>>& person, int indexNumber, std::fstream& file) {
file << "[Person nr. : " << indexNumber + 1 << "]\n";
file << person[indexNumber]->getName() << "\n";
file << person[indexNumber]->getSurname() << "\n";
file << person[indexNumber]->getAddress() << "\n";
file << person[indexNumber]->getPeselNumber() << "\n";
file << person[indexNumber]->getIndexNumber() << "\n";
file << person[indexNumber]->getSalary() << "\n\n";
}
void SaveLoadFile::saveMultiRecordsToFile(std::vector<std::shared_ptr<Person>>& person, std::string fileName) {
std::fstream file;
if (fileName == DEFAULT_FILE_NAME) {
file.open(fileName + ".txt", file.in | file.app);
} else {
file.open(fileName, file.in | file.trunc);
}
if (!file.is_open()) {
std::cout << "ERROR IN saveSingleRecordToFile() function\n";
}
for (int i = 0; i < static_cast<int>(person.size()); i++) {
saveSingleRecordToFile(person, i, file);
}
std::cout << "Data saved in " << fileName << '\n';
}
void SaveLoadFile::loadSingleRecordFromFile(std::vector<std::shared_ptr<Person>>& person, int lineNumber, std::fstream& file) {
}
void SaveLoadFile::loadMultiRecordsToFile(std::vector<std::shared_ptr<Person>>& person, std::string fileName) {
}
void SaveLoadFile::deleteDuplicate(std::vector<std::shared_ptr<Person>>& person) {
std::vector<Person>tempPersons();
person.size();
}