-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVRepository.cpp
More file actions
133 lines (115 loc) · 4.73 KB
/
CSVRepository.cpp
File metadata and controls
133 lines (115 loc) · 4.73 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
#include "CSVRepository.h"
#include <fstream>
Repository::ElectricCSVRepo::ElectricCSVRepo() {
this->cars = {};
this->fav_cars = {};
}
vector<Domain::Elektroauto> Repository::ElectricCSVRepo::get_cars() {
return this->cars;
}
vector<Domain::Elektroauto> Repository::ElectricCSVRepo::get_fav_cars() {
return this->fav_cars;
}
void Repository::ElectricCSVRepo::writeFavorites(const string &kunde) {
ofstream client_file;
client_file.open(kunde);
client_file.clear();
for (Domain::Elektroauto car: fav_cars)
client_file << car.getBrand() << "," << car.getModel() << "," << car.getYearOfRegistration() << ","
<< car.getPrice() << "," << car.getLoadingTime() << "," << car.getRange() << endl;
client_file.close();
}
void Repository::ElectricCSVRepo::writeToFile() {
ofstream manager_file;
manager_file.open("manager.txt");
manager_file.clear();
for (Domain::Elektroauto car: cars)
manager_file << car.getBrand() << "," << car.getModel() << "," << car.getYearOfRegistration() << ","
<< car.getPrice() << "," << car.getLoadingTime() << "," << car.getRange() << endl;
manager_file.close();
}
void Repository::ElectricCSVRepo::add_car_manager(string brand, string model, int year_of_registration, float price,
float loading_time, float range) {
this->cars.emplace_back(Domain::Elektroauto(brand, model, year_of_registration, price, loading_time, range));
writeToFile();
}
void Repository::ElectricCSVRepo::delete_car_manager(int id) {
auto itr = std::remove_if(this->cars.begin(), this->cars.end(),
[id](Domain::Elektroauto car) {
return car.getId() == id;
});
this->cars.erase(itr);
writeToFile();
}
void Repository::ElectricCSVRepo::modify_car_manager(int id, float kilometer,
float price, float loading_time, float range) {
for (Domain::Elektroauto &car: this->cars) {
if (car.getId() == id) {
car.setKilometer(kilometer);
car.setPrice(price);
car.setLoadingTime(loading_time);
car.setRange(range);
}
}
writeToFile();
}
void Repository::ElectricCSVRepo::add_car_client(int id, string kunde) {
string brand, model;
int year_of_registration;
float price, loading_time, range;
for (Domain::Elektroauto car: this->get_cars())
if (car.getId() == id) {
brand = car.getBrand();
model = car.getModel();
year_of_registration = car.getYearOfRegistration();
price = car.getPrice();
loading_time = car.getLoadingTime();
range = car.getRange();
}
this->fav_cars.emplace_back(Domain::Elektroauto(brand, model, year_of_registration, price, loading_time, range));
writeFavorites(kunde);
}
void Repository::ElectricCSVRepo::delete_car_client(int id, string kunde) {
auto itr = std::remove_if(this->fav_cars.begin(), this->fav_cars.end(),
[id](Domain::Elektroauto car) {
return car.getId() == id;
});
this->fav_cars.erase(itr);
writeFavorites(kunde);
}
void Repository::ElectricCSVRepo::read_manager() {
ifstream manager_file;
string line;
manager_file.open("manager.txt");
while (getline(manager_file, line)) {
vector<string> elements_of_car = {};
size_t pos = 0;
string token;
while ((pos = line.find(',')) != std::string::npos) {
token = line.substr(0, pos);
elements_of_car.emplace_back(token);
line.erase(0, pos + 1);
}
this->cars.emplace_back(Domain::Elektroauto(elements_of_car[0], elements_of_car[1], stoi(elements_of_car[2]),
stof(elements_of_car[3]), stof(elements_of_car[4]), stof(line)));
}
manager_file.close();
}
void Repository::ElectricCSVRepo::read_client(string kunde) {
ifstream client_file;
string line;
client_file.open(kunde);
while (getline(client_file, line)) {
vector<string> elements_of_car = {};
size_t pos = 0;
string token;
while ((pos = line.find(',')) != std::string::npos) {
token = line.substr(0, pos);
elements_of_car.emplace_back(token);
line.erase(0, pos + 1);
}
this->fav_cars.emplace_back(Domain::Elektroauto(elements_of_car[0], elements_of_car[1], stoi(elements_of_car[2]),
stof(elements_of_car[3]), stof(elements_of_car[4]), stof(line)));
}
client_file.close();
}