Repository navigation
Expand file tree
/
Copy pathinfo-syst.cpp
More file actions
429 lines (358 loc) · 12.3 KB
/
Copy pathinfo-syst.cpp
File metadata and controls
429 lines (358 loc) · 12.3 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (C) 2012, 2015 Dmytro Dzyubak
*
* This file is part of InformationSystem.
*
* InformationSystem is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* InformationSystem is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with InformationSystem. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;
using std::ios;
using std::left;
using std::right;
#include <list>
using namespace std;
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iomanip>
using std::setw;
#include "info-syst.h"
// "put-to" or "stream insertion" operator overloading
ostream &operator<<( ostream &output, const Student &number ) {
output
<< " | " << left << setw( 21 ) << number.LastName << " |"
<< right << setw( 11 ) << number.BirthYear << " |" << setw( 14 )
<< number.AverageGrade << " |";
return output;
}
bool operator<(const Student &o1, const Student &o2) {
return o1.AverageGrade < o2.AverageGrade;
}
bool operator>(const Student &o1, const Student &o2) {
return o1.AverageGrade > o2.AverageGrade;
}
bool operator==(const Student &o1, const Student &o2) {
return o1.AverageGrade == o2.AverageGrade;
}
bool operator!=(const Student &o1, const Student &o2) {
return o1.AverageGrade != o2.AverageGrade;
}
// constructor
Student::Student( string i, int j, int k ) {
LastName = i; // save LastName in an object
BirthYear = j;
AverageGrade = k;
};
Student::~Student() { }
int Student::getGrade() {
return AverageGrade;
};
string Student::getName() {
return LastName;
};
// constructor
Group::Group( string gName ) {
groupName = gName;
}
Group::~Group() { }
void Group::backToMainMenu() {
char backToMainMenuChoice;
do {
cout << "\n Type <m> to get back to the Main Menu."
<< endl;
cin >> backToMainMenuChoice;
}
while ( backToMainMenuChoice != 'm');
}
int Group::printToMonitor() {
// check, if the list is not empty
if ( group.empty() ) {
cout << "\n The list is currently empty. Please, enter some records "
<< "to the list first." << endl;
backToMainMenu();
return 0;
}
cout << "\n The list content is:\n"
<< " +-----------------------+------------+---------------+\n"
<< " | Last Name | Birth Year | Average Grade |\n"
<< " +-----------------------+------------+---------------+\n";
list<Student>::iterator p = group.begin();
while( p != group.end() ) {
cout << *p << endl;
p++;
}
cout << " +-----------------------+------------+---------------+\n\n";
cout << " We currently have " << group.size() << " students in the group."
<< endl;
backToMainMenu();
return 0;
}
void Group::linearSearch() {
/*
// alternative: search for average grade
int key = 123;
list<Student>::iterator p = group.begin();
while( p != group.end() )
{
cout << p->getGrade() << endl; // cout << p->getName() << endl;
if ( p->getGrade() == key )
cout << *p << endl;
p++;
}
*/
string key; // search key
int SearchResults = 0; // search results counter
cout << "Please type the Last Name below:" << endl;
cin >> key;
list<Student>::iterator p = group.begin();
while( p != group.end() ) {
if ( p->getName() == key ) {
cout << *p << endl;
SearchResults++;
}
p++;
}
if ( SearchResults == 0 ) {
cout << "No students with last name that matched your search query "
<< "were found."
<< "\nYou have typed \"" << key << "\" for Your search string."
<< "Please check Your spelling and try again."
<< endl;
} else if ( SearchResults == 1 ) {
cout << " " << SearchResults << " student with \"" << key
<< "\" last name was found."
<< endl;
} else {
cout << " " << SearchResults << " students with \"" << key
<< "\" last name were found."
<< endl;
}
backToMainMenu();
cout << " Done searching. Entering the Main Menu." << endl;
}
void Group::readConsole() {
string TheLastName;
int YearOfBirth;
int GradeAverage;
char ReadConsoleChoice;
do {
cout << " Please, enter the required details." << endl;
cout << " Input Last Name: ";
cin >> TheLastName;
cout << " Input Birth Year: ";
cin >> YearOfBirth;
cout << " Input Average Grade: ";
cin >> GradeAverage;
group.push_back( Student(TheLastName, YearOfBirth, GradeAverage) );
cout << " New record has been added successfully." << endl;
cout << right << setw( 21 ) << TheLastName << setw( 7 ) << YearOfBirth
<< setw( 5 ) << GradeAverage << endl;
cout << "\n Whould You like to add another record?"
<< "\n Type \"y\" for Yes or \"n\" for No" << endl;
cin >> ReadConsoleChoice;
}
while ( ReadConsoleChoice != 'n');
printToMonitor();
cout << " Done Reading from Console. Entering the Main Menu." << endl;
}
void Group::sortByAverageGrade() {
cout << "\n --- Unsorted group of students. ---" << endl;
cout << "\n The list content is:\n"
<< " +-----------------------+------------+---------------+\n"
<< " | Last Name | Birth Year | Average Grade |\n"
<< " +-----------------------+------------+---------------+\n";
list<Student>::iterator p = group.begin();
while( p != group.end() ) {
cout << *p << endl;
p++;
}
cout << " +-----------------------+------------+---------------+\n\n";
group.sort(); // 0...100
group.reverse(); // 100...0
cout << "\n --- The sorted group of students. ---" << endl;
printToMonitor();
cout << " Done sorting by average grade. Entering the Main Menu." << endl;
}
int Group::eraseRecord() {
int i = 1; // record counter
int eraseRecord;
// specific record is deleted by using the iterator
list<Student>::iterator ittr;
cout << " Deleting specific record by its number." << endl;
list<Student>::iterator p = group.begin();
while( p != group.end() ) {
cout << setw( 5 ) << i++ << *p << endl;
p++;
}
cout << "\n\n";
// check, if the list is not empty
if ( group.empty() ) {
cout << " The list is currently empty." << endl;
return 0;
}
cout << " Type the number of the record you want to delete." << endl;
cin >> eraseRecord;
if ( eraseRecord > i - 1 ) {
cout << " The number you typed is greater, "
<< "then there are records available."
<< "\n Please, try again.\n\n";
return 0;
}
if ( eraseRecord == 1 ) {
ittr = group.begin();
} else {
ittr = group.begin();
for ( int k = 1 ; k < eraseRecord; k++ ) {
ittr++;
}
}
char eraseRecordChoice;
cout << "\n Do you really want to delete this record from the list?\n"
<< *ittr << "\n\n Type \"y\" to delete the record from the list,"
<< "\n otherwise type any letter to enter the Edit Menu." << endl;
cin >> eraseRecordChoice;
if ( eraseRecordChoice == 'y' ) {
group.erase(ittr);
// output updated list to screen
i = 1;
list<Student>::iterator q = group.begin();
while( q != group.end() ) {
cout << i++ << " " << *q << endl;
q++;
}
cout << "\n\n";
}
return 0;
}
int Group::clearList() {
// check, if the list is not empty
if ( group.empty() ) {
cout << " The list is currently empty. Nothing to delete." << endl;
return 0;
}
char ClearListChoice;
cout << "\n\n CAUTION!!! Do you really want to delete "
<< "ALL elements from the list?"
<< "\n\n Type \"y\" to delete all elements from the list,"
<< "\n otherwise type any letter to enter the Edit Menu." << endl;
cin >> ClearListChoice;
if ( ClearListChoice == 'y' ) {
group.clear();
// check, if the list is really empty now
if ( group.empty() ) {
cout << " All elements from the list were deleted. "
<< "The list is empty now." << endl;
}
}
return 0;
}
int Group::editList() {
int EditChoice;
do {
cout << "\n\n ------------ Edit Menu ------------ " << endl;
cout << " Select \"1\" to Read from Console" << endl;
cout << " Select \"2\" to Delete specific record by number" << endl;
cout << " Select \"3\" to Delete ALL the Records from the List" << endl;
cout << " Select \"4\" to Enter the Main Menu" << endl;
cout << " Please, select some number to proceed:" << endl;
cin >> EditChoice;
switch ( EditChoice ) {
case 1:
cout << " Choice \"1\" is selected. Reading from Console." << endl;
readConsole();
break;
case 2:
cout << " Choice \"2\" is selected. Erasing Records." << endl;
eraseRecord();
break;
case 3:
cout << " Choice \"3\" is selected. "
<< "Deleting ALL Records from the List." << endl;
clearList();
break;
// ignore whitespaces input
case '\n':
case '\t':
case ' ':
break;
// catch all other symbols
default:
cout << "Incorrect digit is entered. Please enter another number."
<< endl;
break;
}
} while ( EditChoice != 4 );
cout << " Choice 4 is selected.\n Entering the Main Menu..." << endl;
return 0;
}
int Group::writeToFile() {
// open file
ofstream listFile( "students.txt", ios::out );
// if ofstream could not open the file, exit this function
if ( !listFile ) {
cerr << "File could not be opened." << endl;
return 0;
}
// write every single record to file
list<Student>::iterator p = group.begin();
while( p != group.end() ) {
listFile << *p << endl;
p++;
}
cout << "\n\n";
cout << " Data has been written to file successfully." << endl;
return 0;
}
int Group::readFromFile() {
// open file
ifstream listFile( "students.txt", ios::in );
// if ofstream could not open the file, exit this function
if ( !listFile ) {
cerr << "File could not be opened." << endl;
return 0;
}
string TheLastName;
int YearOfBirth;
int GradeAverage;
string tc1 = " | "; // vertical pipe to draw a table 1
string tc2 = " |"; // vertical pipe to draw a table 2
while ( listFile >> tc1 >> setw( 21 ) >> TheLastName >> tc2 >> setw( 11 )
>> YearOfBirth >> tc2 >> setw( 14 ) >> GradeAverage >> tc2 )
group.push_back( Student(TheLastName, YearOfBirth, GradeAverage) );
return 0;
}
void Group::infoAboutDeveloper() {
cout << "\n\n --- Information about the Developer of this Program. ---"
<< "\n\n This program is developed by Dmytro Dzyubak."
<< "\n If You have any questions, comments or bug reports, "
<< "don't hesitate\n to contact me."
<< "\n\n\n Special thanks to"
<< "\n\n - Domnin F. A. for educating through my first steps "
<< "in programming and\n motivation"
<< "\n - Harvey M. Deitel, Paul J. Dietel and Herbert Schildt for their"
<< "\n excellent books"
<< "\n - http://www.cplusplus.com/ for their outstanding resources network"
<< "\n - and of course my Mum and Granny for their loving care."
<< "\n\n Without the help of these people, this program would "
<< "just not exist."
<< "\n\n" << endl;
backToMainMenu();
}