-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.cpp
370 lines (303 loc) · 8.58 KB
/
service.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "chocan.h"
#include <time.h>
#include <cctype>
#define NAME_SZ 20
#define COMMENT_SZ 100
service_directory available_services;
service_list::service_list() {}
service_list::service_list(const service_list ©): list(copy.list) {}
void service_list::add_new_service_record()
{
service temp;
temp.create();
vector<service>::iterator t = list.begin();
while ( t < list.end() && ((*t).compare_dates(temp)) <= 0 )
t++;
list.insert(t, temp);
cout << "\n\tService record saved.\n" << endl;
}
void service_list::display_all_services()
{
for (unsigned int i = 0; i < list.size(); i++)
list[i].display();
}
float service_list::total_cost()
{
float sum = 0.0;
for (unsigned int i = 0; i < list.size(); i++)
sum += list[i].get_cost();
return sum;
}
void service_list::read_from_disk(ifstream &ifile)
{
service temp;
temp.read_from_disk(ifile);
list.push_back(temp);
}
int service_list::num_services() { return list.size(); }
service::service(): date_of_service("Date of service not saved"),
code(0), name("No service name saved"), comments("No comments"),
cost(0.0), current_date("Date of record not saved")
{
name.reserve(NAME_SZ);
comments.reserve(COMMENT_SZ);
}
void service::create()
{
cout << "\n\t\t[ADDING A NEW SERVICE RECORD]" << endl;
enter_date();
get_service_dir_data();
// enter comments (optional)
char resp;
cout << "\n\tWould you like to add any additional comments? (Y/N): ";
do {
cin >> resp;
if ('Y' != resp && 'y' != resp && 'N' != resp && 'n' != resp) {
cout << "\n\tDid not understand your response. Please enter 'Y' or 'N'.\n"
<< "Would you like to add any additional comments? ";
}
else break;
} while (true);
if ('Y' == resp || 'y' == resp) {
cout << "\n\tPlease enter them here: (500 character limit)\n";
cin.ignore(100, '\n');
getline(cin, comments, '\n');
cin.ignore(500, '\n');
}
get_curr_date();
cout << "\n\tService record created.\n" << endl;
}
void service::display() const
{
cout << "\n\t" << name << " (" << code << ") - $" << cost;
cout << "\n\tDate provided: " << date_of_service;
cout << "\n\tDate recorded: " << current_date;
cout << "\n\t" << comments << endl;
}
int service::compare_dates(service &to_cmp)
{
// compare years
string y = date_of_service.substr(6, 4);
string cmp_y = to_cmp.date_of_service.substr(6, 4);
if (parse_date(y) < parse_date(cmp_y)) return -1;
if (parse_date(y) > parse_date(cmp_y)) return 1;
// years are the same, compare months
string m = date_of_service.substr(0, 2);
string cmp_m = to_cmp.date_of_service.substr(0, 2);
if (parse_date(m) < parse_date(cmp_m)) return -1;
if (parse_date(m) > parse_date(cmp_m)) return 1;
// months also the same, compare days
string d = date_of_service.substr(3, 2);
string cmp_d = to_cmp.date_of_service.substr(3, 2);
if (parse_date(d) < parse_date(cmp_d)) return -1;
if (parse_date(d) > parse_date(cmp_d)) return 1;
return 0;
}
void service::read_from_disk(ifstream &ifile)
{
if (!ifile) {
cerr << "\n\nError getting services from provider list.\n\n";
ifile.close();
return;
}
getline(ifile, date_of_service, '|');
ifile >> code; ifile.ignore(1, '|');
getline(ifile, name, '|');
getline(ifile, comments, '|');
ifile >> cost; ifile.ignore(1, '|');
getline(ifile, current_date, '|');
}
float service::get_cost() { return cost; }
void service::enter_date()
{
string m, d, y;
char valid = 0;
cout << "\n\tDate of service (MM-DD-YYYY): ";
do {
cin.ignore(100, '\n');
getline(cin, m, '-');
getline(cin, d, '-');
getline(cin, y, '\n');
valid = check_date(m, d, y);
if (!valid) {
cout << "\n\tInvalid date. Please check your entry and try again.\n" << endl;
cout << "\tRe-enter Date of service (MM-DD-YYYY): ";
}
} while (!valid);
date_of_service = m + "-" + d + "-" + y;
}
void service::get_service_dir_data()
{
cout << "\n\tName of service provided: ";
do {
getline(cin, name, '\n');
code = available_services.find_code_by_name(prep_str(name));
if (code < 0) {
cout << "\n\tInvalid service name. Please check your entry and try again.\n" << endl;
cout << "\tRe-enter name of service provided: ";
}
} while (code < 0);
cost = available_services.find_cost_by_name(name);
capitalize_name();
}
void service::get_curr_date()
{
char conv_t[26];
time_t t = time(NULL);
ctime_r(&t, conv_t);
// get date
switch(conv_t[4]+conv_t[5]+conv_t[6]) {
case 'J' + 'a' + 'n': current_date = "01-"; break;
case 'F' + 'e' + 'b': current_date = "02-"; break;
case 'M' + 'a' + 'r': current_date = "03-"; break;
case 'A' + 'p' + 'r': current_date = "04-"; break;
case 'M' + 'a' + 'y': current_date = "05-"; break;
case 'J' + 'u' + 'n': current_date = "06-"; break;
case 'J' + 'u' + 'l': current_date = "07-"; break;
case 'A' + 'u' + 'g': current_date = "08-"; break;
case 'S' + 'e' + 'p': current_date = "09-"; break;
case 'O' + 'c' + 't': current_date = "10-"; break;
case 'N' + 'o' + 'v': current_date = "11-"; break;
case 'D' + 'e' + 'c': current_date = "12-"; break;
}
current_date = (current_date + conv_t[8] + conv_t[9] + "-");
current_date = (current_date + conv_t[20] + conv_t[21] + conv_t[22] + conv_t[23] + " ");
// get time
for (int i = 11; i < 19; i++)
current_date += conv_t[i];
}
int service::parse_date(string date)
{
int sum = 0;
for (unsigned int i = 0; i < date.size(); i++) {
int pos = 1;
for (int j = (date.size() - (i+1)); j > 0; j--) {
pos *= 10;
}
sum += (date[i] * pos);
}
return sum;
}
bool service::check_date(string m, string d, string y)
{
int month = parse_date(m), day = parse_date(d), year = parse_date(y);
int valid = 2;
if (month < ('0'*10)+'1' || month > ('1'*10)+'2' || day < ('0'*10)+'1')
valid = false;
switch(month) {
case ('0'*10)+'1':
case ('0'*10)+'3':
case ('0'*10)+'5':
case ('0'*10)+'7':
case ('0'*10)+'8':
case ('1'*10)+'0':
case ('1'*10)+'2':
if (day > ('3'*10)+'1')
valid = false;
break;
case ('0'*10)+'4':
case ('0'*10)+'6':
case ('0'*10)+'9':
case ('1'*10)+'1':
if (day > ('3'*10)+'0')
valid = false;
break;
case ('0'*10)+'2':
if (day > ('2'*10)+'8')
valid = false;
break;
default:
valid = false;
}
if (year < parse_date("1970") || year > parse_date("2023"))
valid = false;
return valid;
}
string service::prep_str(string to_prep)
{
unsigned int i = 0;
char temp[to_prep.size() + 1];
for ( ; i < to_prep.size(); i++)
temp[i] = tolower(to_prep[i]);
temp[i] = '\0';
return string(temp);
}
void service::capitalize_name()
{
name[0] = toupper(name[0]);
for (unsigned int i = 1; i < name.size(); i++) {
if (name[i-1] == ' ')
name[i] = toupper(name[i]);
else name[i] = tolower(name[i]);
}
}
service_directory::service_directory()
{
// attempt to access database to load services
ifstream ifile("databases/available_services.txt", ios_base::in);
if (!ifile) {
cerr << "\n\nCould not connect to services database.\n\n";
ifile.close();
return;
}
entry temp;
// load services for quicker access from app
do {
// check if connected
if (ifile) {
ifile >> temp.code; // test read
// if test read passed, read rest of service info
if (!ifile.eof() && ifile.is_open()) {
ifile.ignore();
getline(ifile, temp.name, '~');
ifile >> temp.cost;
ifile.ignore(100, '\n');
// add newly-read service
dir.push_back(temp);
}
}
} while (!ifile.eof());
ifile.close();
}
int service_directory::find_code_by_name(string name)
{
int low = 0, high = (dir.size() - 1);
for ( ; low <= high; ) {
int mid = (low + high) / 2;
if (name == dir[mid].name)
return dir[mid].code;
if (name > dir[mid].name)
low = mid + 1;
else high = mid - 1;
}
return -1;
}
float service_directory::find_cost_by_name(string name)
{
int low = 0, high = (dir.size() - 1);
for ( ; low <= high ; ) {
int mid = (low + high) / 2;
if (name == dir[mid].name)
return dir[mid].cost;
if (name > dir[mid].name)
low = mid + 1;
else high = mid - 1;
}
return -1.0;
}
void service_directory::display_all()
{
for (unsigned int i = 0; i < dir.size(); i++) {
cout << "\n\t" << dir[i].name
<< "\t(" << dir[i].code
<< ")\t$" << dir[i].cost << endl;
}
}
bool service_directory::is_code_valid(int to_check)
{
for (unsigned int i = 0; i < dir.size(); i++) {
if (dir[i].code == to_check)
return true;
}
return false;
}