forked from shashankparchure/Group2-G4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVHandler.cpp
More file actions
228 lines (204 loc) · 6.21 KB
/
Copy pathCSVHandler.cpp
File metadata and controls
228 lines (204 loc) · 6.21 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "CSVHandler.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
CSVHandler::CSVHandler(const string& filename, BTree& tree)
: filename(filename), bTree(tree) {}
void CSVHandler::loadCSV() {
data.clear();
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Could not open file " << filename << endl;
return;
}
string line;
bool firstRow = true;
while (getline(file, line)) {
if (line.empty()) continue;
if (firstRow) {
firstRow = false;
continue; // skip the header line
}
stringstream ss(line);
string value;
vector<string> row;
while (getline(ss, value, ',')) row.push_back(value);
if (row.size() == 4) {
data.push_back(row);
} else {
cerr << "Warning: Row does not have 4 columns and will be skipped: " << line << endl;
}
}
file.close();
cout << "CSV file loaded successfully!" << endl;
}
void CSVHandler::displayCSV() const {
for (size_t i = 0; i < data.size(); ++i) {
cout << i << ": ";
for (const auto& col : data[i]) cout << col << " ";
cout << endl;
}
}
void CSVHandler::addRow(const vector<string>& row) {
if (row.size() == 4) {
data.push_back(row);
} else {
cout << "Invalid row size!" << endl;
}
}
void CSVHandler::updateRow(int rowNumber, const vector<string>& newRow) {
if (rowNumber >= 0 && rowNumber < static_cast<int>(data.size())) {
if (newRow.size() == 4) {
data[rowNumber] = newRow;
} else {
cout << "Invalid new row size!" << endl;
}
} else {
cout << "Invalid row number!" << endl;
}
}
void CSVHandler::deleteRow(int rowNumber) {
if (rowNumber >= 0 && rowNumber < static_cast<int>(data.size())) {
data.erase(data.begin() + rowNumber);
} else {
cout << "Invalid row number!" << endl;
}
}
void CSVHandler::saveCSV() const {
ofstream file(filename);
if (!file.is_open()) {
cerr << "Error: Could not save file " << filename << endl;
return;
}
file << "ID,Name,Age,Salary" << endl;
for (const auto& row : data) {
for (size_t i = 0; i < row.size(); ++i) {
file << row[i];
if (i < row.size() - 1) file << ",";
}
file << endl;
}
file.close();
cout << "CSV file saved successfully!" << endl;
}
// No longer skips a row - the header was already stripped in loadCSV().
void CSVHandler::loadDataIntoBTree() {
for (const auto& row : data) {
if (row.size() != 4) continue;
try {
int id = stoi(row[0]);
string name = row[1];
int age = stoi(row[2]);
float salary = stof(row[3]);
bTree.insert(id, name, age, salary);
} catch (const invalid_argument& e) {
cerr << "Error converting data: " << e.what() << endl;
} catch (const out_of_range& e) {
cerr << "Error: Data out of range: " << e.what() << endl;
}
}
cout << "Data loaded into BTree successfully!" << endl;
}
void CSVHandler::exportBTreeToCSV() {
auto records = bTree.extractData();
ofstream csvFile(filename);
if (!csvFile.is_open()) {
cerr << "Failed to open CSV file for writing." << endl;
return;
}
csvFile << "ID,Name,Age,Salary\n";
for (const auto& record : records) {
int id;
string name;
int age;
float salary;
tie(id, name, age, salary) = record;
csvFile << id << "," << name << "," << age << "," << salary << "\n";
}
csvFile.close();
cout << "BTree data exported to CSV file successfully!" << endl;
}
bool CSVHandler::fileExists(const string& fname) const {
ifstream f(fname);
return f.is_open();
}
void CSVHandler::menu() {
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Load CSV\n";
cout << "2. Display CSV\n";
cout << "3. Add Row\n";
cout << "4. Update Row\n";
cout << "5. Delete Row\n";
cout << "6. Save CSV\n";
cout << "7. Load Data into BTree\n";
cout << "8. Export BTree to CSV\n";
cout << "9. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter CSV file name: ";
cin >> filename;
loadCSV();
break;
case 2:
displayCSV();
break;
case 3: {
vector<string> newRow(4);
cout << "Enter ID: ";
cin >> newRow[0];
cout << "Enter Name: ";
cin.ignore();
getline(cin, newRow[1]);
cout << "Enter Age: ";
cin >> newRow[2];
cout << "Enter Salary: ";
cin >> newRow[3];
addRow(newRow);
break;
}
case 4: {
int rowNum;
vector<string> newRow(4);
cout << "Enter row number to update: ";
cin >> rowNum;
cout << "Enter new ID: ";
cin >> newRow[0];
cout << "Enter new Name: ";
cin.ignore();
getline(cin, newRow[1]);
cout << "Enter new Age: ";
cin >> newRow[2];
cout << "Enter new Salary: ";
cin >> newRow[3];
updateRow(rowNum, newRow);
break;
}
case 5: {
int rowNum;
cout << "Enter row number to delete: ";
cin >> rowNum;
deleteRow(rowNum);
break;
}
case 6:
saveCSV();
break;
case 7:
loadDataIntoBTree();
break;
case 8:
exportBTreeToCSV();
break;
case 9:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice, try again!" << endl;
}
} while (choice != 9);
}