-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.h
More file actions
163 lines (136 loc) · 4.93 KB
/
DataManager.h
File metadata and controls
163 lines (136 loc) · 4.93 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
#pragma once
#include "Logistics.h"
#include "Assets.h"
#include "Exceptions.h"
#include "Registry.h"
#include <fstream>
#include <sstream>
using namespace std;
// this file handles saving and loading to text files using comma separated values
class DataManager
{
private:
string crateFile = "crates.txt";
string shipFile = "shipments.txt";
// helper function to cut up a string based on commas
static int chopLine(string line, char delim, string pieces[], int maxPieces)
{
int count = 0;
stringstream ss(line);
string tempToken;
while (getline(ss, tempToken, delim) && count < maxPieces)
{
pieces[count] = tempToken;
count++;
}
return count;
}
public:
// Saving data below
void saveCrates(const Registry<CargoCrate*>& crateList) const
{
ofstream out(crateFile);
if (!out.is_open())
{
throw FileIOException(crateFile, "Failed to open file for writing.");
}
for (int i = 0; i < crateList.size(); i++)
{
CargoCrate* c = crateList[i];
// format: id,weight,desc,fragile,hazmat,country
out << c->getCrateID() << "," << c->getWeight() << "," << "None" << "," << c->getFragileFlag() << "," << c->getHazmat() << "," << "None" << "\n";
}
out.close();
cout << "Saved " << crateList.size() << " crates to " << crateFile << endl;
}
void saveShipments(const Registry<ShipmentOrder*>& shipList) const
{
ofstream out(shipFile);
if (!out.is_open()) {
throw FileIOException(shipFile, "Failed to open file for writing.");
}
for (int i = 0; i < shipList.size(); i++)
{
ShipmentOrder* s = shipList[i];
// format: id,origin,dest,weight,priority,status,type,intl,assetID,crateCount
out << s->getOrderID() << "," << s->getOriginNode() << "," << s->getDestinationNode() << "," << s->getTotalWeight() << "," << s->getPriority() << "," << s->getStatus() << ","
<< s->getCargoType() << "," << s->getIsInternational() << "," << s->getAssignedAssetID() << "," << s->getCrateCount() << "\n";
}
out.close();
cout << "Saved " << shipList.size() << " shipments to " << shipFile << endl;
}
void saveAll(const Registry<CargoCrate*>& crates, const Registry<ShipmentOrder*>& shipments) const
{
saveCrates(crates);
saveShipments(shipments);
cout << "All data saved successfully.\n";
}
// Loading data below
void loadCrates(Registry<CargoCrate*>& crateList) const
{
ifstream in(crateFile);
if (!in.is_open())
{
throw FileIOException(crateFile, "File not found. Save data first.");
}
string currLine;
int loaded = 0;
while (getline(in, currLine))
{
if (currLine == "")
continue;
string parts[6];
int pCount = chopLine(currLine, ',', parts, 6);
if (pCount < 6)
continue; // skip bad lines
int id = stoi(parts[0]);
double weight = stod(parts[1]);
string desc = parts[2];
string fragile = parts[3];
string hazmat = parts[4];
string country = parts[5];
crateList.add(new CargoCrate(id, weight, desc, fragile, hazmat, country));
loaded++;
}
in.close();
cout << "Loaded " << loaded << " crates.\n";
}
void loadShipments(Registry<ShipmentOrder*>& shipList) const
{
ifstream in(shipFile);
if (!in.is_open())
{
throw FileIOException(shipFile, "File not found. Save data first.");
}
string currLine;
int loaded = 0;
while (getline(in, currLine))
{
if (currLine == "") continue;
string parts[10];
int pCount = chopLine(currLine, ',', parts, 10);
if (pCount < 10) continue;
// parts[0] is orderID but it auto increments in the constructor so we skip it
string origin = parts[1];
string dest = parts[2];
double weight = stod(parts[3]);
string priority = parts[4];
string status = parts[5];
string type = parts[6];
bool isIntl = (parts[7] == "1");
int assetID = stoi(parts[8]);
ShipmentOrder* newShip = new ShipmentOrder(origin, dest, weight, priority, status, type, isIntl, assetID);
newShip->setStatus(status); // force correct status
shipList.add(newShip);
loaded++;
}
in.close();
cout << "Loaded " << loaded << " shipments.\n";
}
void loadAll(Registry<CargoCrate*>& crates, Registry<ShipmentOrder*>& shipments) const
{
loadCrates(crates);
loadShipments(shipments);
cout << "All data loaded successfully.\n";
}
};