-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.cpp
75 lines (62 loc) · 1.92 KB
/
model.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
#include <iostream>
#include <sstream>
#include <fstream>
#include "utils.cpp"
#define PATH "resources/data.txt"
using namespace std;
void saveToFile(vector<CarDealer> &carDealers)
{
fstream file;
file.open(PATH, ios::out);
if (file.is_open())
{
for (auto cd : carDealers)
{
for (auto itr = cd.cars.begin(); itr != cd.cars.end(); ++itr)
{
file << cd.dealerID << '\n';
file << itr->first << '\n';
file << itr->second.brand << '\n';
file << itr->second.color << '\n';
file << itr->second.price << '\n';
}
}
// cout << "Dealer\t: " << dealerID << "\nID\t: " << carID << "\nBrand\t: " << brand << "\nColor\t: " << color << "\nPrice\t: " << price << endl;
}
file.close();
}
void readFromFile(vector<CarDealer> &carDealers)
{
fstream file;
file.open(PATH, ios::in);
if (file)
{
string line;
// Read data from the file object and put it into a string.
while (getline(file, line))
{
string dealerID = line;
getline(file, line);
string carID = line;
getline(file, line);
string brand = line;
getline(file, line);
string color = line;
getline(file, line);
double price = stod(line);
Car *car = new Car(dealerID, carID, brand, color, price);
for (int i = 0; i < carDealers.size(); i++)
{
if (carDealers[i].dealerID == dealerID)
carDealers[i] += *car;
}
// cout << "Dealer\t: " << dealerID << "\nID\t: " << carID << "\nBrand\t: " << brand << "\nColor\t: " << color << "\nPrice\t: " << price << endl;
}
// cout << "File exists" << endl;
}
else
{
cout << "No Data/File!!!" << endl;
}
file.close();
}