-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseRecord.h
More file actions
290 lines (260 loc) · 4.91 KB
/
Copy pathCaseRecord.h
File metadata and controls
290 lines (260 loc) · 4.91 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <fstream>
#include <iostream>
#include <ncurses.h>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
namespace LawFirm
{
class CaseRecord
{
public:
string lawyer;
string title;
string code;
string description;
string buffer;
string lawyer_list[100];
int address_list[100];
int count;
void read_data();
void pack();
void write_to_file();
void create_index();
bool remove(string);
int search(string);
int search_index(string);
string extract_lawyer();
void sort_index();
void disp();
void unpack();
void modify(string);
};
void CaseRecord::read_data()
{
std::cin.get();
std::cin.clear();
std::cout << "Lawyer: ";
getline(cin, lawyer, '\n');
std::cout << "Title: ";
getline(cin, title, '\n');
std::cout << "Code: ";
getline(cin, code, '\n');
std::cout << "Description: ";
getline(cin, description, '\n');
}
void CaseRecord::pack()
{
buffer.erase();
buffer += lawyer + "|" + title + "|" + code + "|" + description + "$\n";
}
void CaseRecord::write_to_file()
{
int pos;
fstream file;
file.open("case_file.txt", ios::out | ios::app);
pos = file.tellp();
file << buffer;
file.close();
lawyer_list[++count] = lawyer;
address_list[count] = pos;
sort_index();
}
string CaseRecord::extract_lawyer()
{
string lawyer;
int i = 0;
lawyer.erase();
while (buffer[i] != '|')
lawyer += buffer[i++];
return lawyer;
}
void CaseRecord::unpack()
{
int i = 0;
lawyer.erase();
while (buffer[i] != '|')
lawyer += buffer[i++];
title.erase();
i++;
while (buffer[i] != '|')
title += buffer[i++];
code.erase();
i++;
while (buffer[i] != '|')
code += buffer[i++];
description.erase();
i++;
while (buffer[i] != '$')
description += buffer[i++];
}
void CaseRecord::create_index()
{
fstream file;
int pos;
string lawyer;
count = -1;
file.open("case_file.txt", ios::in);
while (!file.eof())
{
pos = file.tellg();
buffer.erase();
getline(file, buffer);
if (buffer[0] != '*')
{
if (buffer.empty())
break;
lawyer = extract_lawyer();
lawyer_list[++count] = lawyer;
address_list[count] = pos;
}
}
file.close();
sort_index();
buffer.erase();
}
void CaseRecord::sort_index()
{
int i, j, temp_address;
string temp_lawyer;
for (int i = 0; i <= count; i++)
{
for (int j = i + 1; j <= count; j++)
{
if (lawyer_list[i] > lawyer_list[j])
{
temp_lawyer = lawyer_list[i];
lawyer_list[i] = lawyer_list[j];
lawyer_list[j] = temp_lawyer;
temp_address = address_list[i];
address_list[i] = address_list[j];
address_list[j] = temp_address;
}
}
}
for (i = 0; i <= count; i++)
{
std::cout << lawyer_list[i] << "\t" << address_list[i] << "\n";
}
}
int CaseRecord::search_index(string key)
{
int low = 0, high = count, mid = 0, flag = 0, pos;
while (low <= high)
{
mid = (low + high) / 2;
if (lawyer_list[mid] == key)
{
flag = 1;
break;
}
if (lawyer_list[mid] > key)
high = mid - 1;
if (lawyer_list[mid] < key)
low = mid + 1;
}
if (flag)
return mid;
else
return -1;
}
int CaseRecord::search(string key)
{
int pos = 0, address;
fstream file;
buffer.erase();
pos = search_index(key);
if (pos == -1)
{
std::cout << endl
<< "Record not found." << endl;
return -1;
}
else
{
file.open("case_file.txt");
address = address_list[pos];
file.seekp(address, ios::beg);
getline(file, buffer);
unpack();
std::cout << "Record found" << endl << endl
<< "Lawyer: " << lawyer << endl
<< "Title: " << title << endl
<< "Case Code: " << code << endl
<< "Description: " << description << endl;
file.close();
return pos;
}
}
bool CaseRecord::remove(string key)
{
int pos = 0, i, address;
fstream file;
pos = search(key);
if (pos >= 0)
{
file.open("case_file.txt", ios::out | ios::in);
address = address_list[pos];
file.seekp(address, ios::beg);
file.put('*');
file.close();
for (i = pos; i < count; i++)
{
lawyer_list[i] = lawyer_list[i + 1];
address_list[i] = address_list[i + 1];
}
count--;
return true;
}
else
return false;
}
void CaseRecord::modify(string key)
{
int c;
if (remove(key))
{
cout << "Which field do you want to modify?\n1. Lawyer Name \t2. Case Title\t3. Case Code\t4. Case Description\n";
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 1:
cout << "Lawyer Name: ";
cin >> lawyer;
break;
case 2:
cout << "Case Title: ";
cin >> title;
break;
case 3:
cout << "Case Code: ";
cin >> code;
break;
case 4:
cout << "Case Description: ";
cin >> description;
break;
case 5:
break;
default:
cout << "Invalid choice.\n";
}
pack();
write_to_file();
}
else cout << "Record not found.";
}
void CaseRecord::disp()
{
int i;
std::cout << endl
<< "INDEX FILE " << endl
<< "LAWYER\tADDRESS";
for (i = 0; i <= count; i++)
std::cout << endl
<< lawyer_list[i] << "\t" << address_list[i];
std::cout << "\n";
}
} // namespace LawFirm