-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilesHelper.cpp
85 lines (69 loc) · 2.04 KB
/
FilesHelper.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
#include "FilesHelper.h"
using namespace std;
static void FilesHelper::saveLast(string fileName, int id) {
fstream saveId(fileName, ios::app);
saveId << id;
saveId.close();
}
static int FilesHelper::getLast(string fileName) {
ifstream lastId(fileName);
int id;
lastId >> id;
lastId.close();
return id;
}
// Persons files will be comma separated -- Info provided for parsing
static void FilesHelper::saveClient(Client c) {
fstream clientFile("Clients.txt", ios::app);
int id = getLast("saveClientLastId.txt") + 1;
clientFile << id << ',' << c.getName() << ',' << c.getPassword() << ',' << c.getBalance() << "\n";
clientFile.close();
saveLast("saveClientLastId.txt", id);
}
static void FilesHelper::saveEmployee(string fileName, string lastIdFile, Employee e) {
fstream employeeFile(fileName, ios::app);
int id = getLast(lastIdFile) + 1;
employeeFile << id << ',' << e.getName() << ',' << e.getPassword() << ',' << e.getSalary() << "\n";
employeeFile.close();
saveLast(lastIdFile, id);
}
static void FilesHelper::getClients(){
ifstream file;
file.open("Clients.txt");
string line;
while (getline(file, line)) {
Client person = Parser::parseToClient(line);
allClients.push_back(person);
}
}
static void FilesHelper::getEmployees() {
ifstream file;
file.open("Employee.txt");
string line;
while (getline(file, line)) {
Employee person = Parser::parseToEmployee(line);
allEmployees.push_back(person);
}
}
static void FilesHelper::getAdmins() {
ifstream file;
file.open("Admin.txt");
string line;
while (getline(file, line)) {
Admin person = Parser::parseToAdmin(line);
allAdmins.push_back(person);
}
}
static void FilesHelper::clearFile(string fileName, string lastIdFile) {
fstream file(fileName, ios::in | ios::out);
string line;
while (getline(file, line)) {
if (line[0] = stoi(lastIdFile)) {
line.clear();
}
}
file.close();
}
static vector<Client> allClients;
static vector<Employee> allEmployees;
static vector<Admin> allAdmins;