-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
201 lines (161 loc) · 5.24 KB
/
Database.cpp
File metadata and controls
201 lines (161 loc) · 5.24 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
#include "Database.h"
using namespace std;
void Database::SaveDatabase(string name) {
fstream file(name, ios::out | ios::binary | ios::beg);
if (table.size() > 0) {
file.write((char*)& table[0], table.size() * sizeof(table));
}
else {
cout << "Exception: must have data to save" << endl << endl;
}
file.close();
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::Help() {
cout << endl << "POSSIBLE COMMANDS: " << endl;
cout << "add STRING <--> where string is text data to be appended to database" << endl;
cout << "display <--> print current database" << endl;
cout << "save <--> saves current database as .bin file" << endl;
cout << "find STRING <--> searches current database for string" << endl;
cout << "display << INTEGER <--> display values at index" << endl;
cout << "display < OR > INTEGER <--> displays smaller OR larger than given integer " << endl << endl;
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::DisplayDataAt(int loc) {
cout << table.at(loc) << endl;
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::GetCommand() {
string stri;
getline(cin, stri);
istringstream stream(stri);
vector<string> vec;
do {
string word;
stream >> word;
vec.push_back(word);
} while (stream);
if (vec[0] == "find") {
Find(vec[1]);
} else
// CHECK THE DATABASE
if (vec[0] == "display") {
if (vec[1] == ">") {
Check(stoi(vec[2]), true);
}
else if (vec[1] == "<") {
Check(stoi(vec[2]), false);
}
} else
// SAVE THE DATABASE
if (vec[0] == "save") {
if (vec[1] != "") {
SaveDatabase(vec[1]);
}
else {
cout << "Exception: must provide a save name" << endl << endl;
}
} else
// DISPLAY HELP
if (vec[0] == "help") {
Help();
} else
// DISPLAY DATA AT SPECIFIC INDEX
if (vec[0] == "display") {
if (vec[1] == "<<") {
try {
DisplayDataAt(stoi(vec[2]));
}
catch (exception& e) {
cout << "Exception: the index could not be understood";
}
}
// DISPLAY DATA AT ALL INDEXES IF NO INDEX SPECIFIED
else {
if (vec[1] != ">" && vec[1] != "<") {
DisplayData();
}
}
} else
// ADD DATA TO DATABASE
if (vec[0] == "add") {
if (vec[1] != "") {
for (int i = 1; i < vec.size(); i++) {
AddData(vec[i]);
}
}
else {
cout << "Exception: no data found to add to database" << endl << endl;
}
}
// NO COMMAND COULD BE UNDERSTOOD
else {
cout << "COMMAND NOT FOUND" << endl << endl;
}
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::DisplayData() {
if (table.size() > 0) {
cout << endl << "Data Layout: " << endl;
for (int i = 0; i < table.size()-1; i++) {
cout << table[i] << " at table location " << i << endl;
if (i == table.size()-2) {
cout << endl;
}
}
}
else {
cout << "Exception: no data in table to display" << endl << endl;
}
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::AddData(string data) {
table.push_back(data);
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::Check(int check, bool greaterThan) {
for (int i = 0; i < table.size(); i++) {
if (!greaterThan) {
if (check > table.at(i).size() && i+1 < table.size()) {
cout << "Found match " << table.at(i) << " at location " << i << endl;
}
}
else {
if (check < table.at(i).size() && i+1 < table.size()) {
cout << "Found match " << table.at(i) << " at location " << i << endl;
}
}
}
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------
void Database::Find(string str2) {
string str1;
bool found = false;
for (int i = 0; i < table.size(); i++) {
str1 = table.at(i);
if (str1.find(str2) != string::npos) {
cout << "Found match " << table.at(i) << " at location " << i << endl;
found = true;
}
}
if (!found) {
cout << "Could not locate " << "'" << str2 << "' in database." << endl;
}
}
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// ----------------------------------------------------------------