-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesHelper.cpp
More file actions
177 lines (163 loc) · 4.7 KB
/
FilesHelper.cpp
File metadata and controls
177 lines (163 loc) · 4.7 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <ctime>
#include "Password.h"
#include "FilesHelper.h"
#include "Parser.h"
using namespace std;
void FilesHelper::saveLast(string last_id_file, int id) {
ofstream LId_out;
LId_out.open(last_id_file);
LId_out << id << endl;
LId_out.close();
}
int FilesHelper::getLast(string last_id_file) {
ifstream Lid_in;
int x = 0;
Lid_in.open(last_id_file);
Lid_in >> x;
Lid_in.close();
return x;
}
int FilesHelper::generate_id(std::string last_id_file) {
time_t now = time(nullptr);
tm * t = localtime(&now);
int y = (((t ->tm_year)+1900)%100)*1000, id;
if (getLast(last_id_file) == 0 || (getLast(last_id_file)/1000) != (y/1000)){
id = ++y;
} else{
id = getLast(last_id_file) + 1;
}
return id;
}
void FilesHelper::save_national_id(string national_id){
ofstream LNationalID_out;
LNationalID_out.open("Nationals_ID.txt", ios::app);
LNationalID_out << national_id << endl;
LNationalID_out.close();
}
void FilesHelper::saveClient(Client &c, bool generateID) {
ofstream client_out;
client_out.open("Clients.txt", ios::app);
if(generateID){
int id = generate_id("Last_id_Clients.txt");
c.set_id(to_string(id));
save_national_id(c.get_national_id());
}
if(c.get_password() == ""){
c.set_password(c.get_id()+"00000000");
}
string password = Password::encrypt_password(c.get_password(),c.get_id());
client_out << c.get_id() << "," << c.get_name() << "," << password << "," << c.get_phone_number() << "," << c.get_national_id() << "," << c.get_balance() << endl;
saveLast("Last_id_Clients.txt", stoi(c.get_id()));
client_out.close();
}
void FilesHelper::saveEmployee(string file_name, string last_id_file, Employee &e, bool generateID) {
ofstream employee_out;
employee_out.open(file_name, ios::app);
if(generateID) {
int id = generate_id(last_id_file);
e.set_id(to_string(id));
save_national_id(e.get_national_id());
}
if(e.get_password() == ""){
e.set_password(e.get_id()+"00000000");
}
save_national_id(e.get_national_id());
string password = Password::encrypt_password(e.get_password(),e.get_id());
employee_out << e.get_id() << "," << e.get_name() << "," << password << "," << e.get_phone_number() << "," << e.get_national_id() << "," << e.get_salary() << endl;
saveLast(last_id_file, stoi(e.get_id()));
employee_out.close();
}
vector<Client> FilesHelper::getClients() {
vector<Client> C;
ifstream file("Clients.txt");
string line;
while (getline(file, line)) {
Client c = Parser::parse_to_client(line);
C.push_back(c);
}
file.close();
return C;
}
vector<Employee> FilesHelper::getEmployees() {
vector<Employee> E;
ifstream file("Employees.txt");
string line;
while (getline(file, line)) {
Employee e = Parser::parse_to_employee(line);
E.push_back(e);
}
file.close();
return E;
}
vector<Admin> FilesHelper::getAdmins() {
vector<Admin> A;
ifstream file("Admins.txt");
string line;
while (getline(file, line)) {
Admin a = Parser::parse_to_admin(line);
A.push_back(a);
}
file.close();
return A;
}
void FilesHelper::clearFile(string file_name, string last_id_file) {
ofstream file;
file.open(file_name, ios::trunc);
file.close();
file.open(last_id_file, ios::trunc);
file.close();
}
Client* FilesHelper::SearchClient(int id) {
ifstream file("Clients.txt");
string line;
while (getline(file, line)) {
Client* c = new Client();
*c = Parser::parse_to_client(line);
if(c->get_id() == to_string(id))
{
file.close();
return c;
}
}
file.close();
return nullptr;
}
Employee* FilesHelper::SearchEmployee(int id) {
ifstream file("Employees.txt");
string line;
while (getline(file, line)) {
Employee* e = new Employee(Parser::parse_to_employee(line));
if(e->get_id() == to_string(id))
{
file.close();
return e;
}
}
file.close();
return nullptr;
}
Admin* FilesHelper::search_admin(int id){
ifstream file("Admins.txt");
string line;
while (getline(file, line)) {
Admin* a = new Admin(Parser::parse_to_admin(line));
if(a->get_id() == to_string(id))
{
file.close();
return a;
}
}
file.close();
return nullptr;
}
bool FilesHelper::IsFound_national_id(string national_id) {
ifstream file("Nationals_ID.txt");
string line;
while (getline(file,line)){
if (national_id == line){
return true;
}
}
return false;
}