-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
472 lines (430 loc) · 16.6 KB
/
Main.cpp
File metadata and controls
472 lines (430 loc) · 16.6 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <iostream>
#include <fstream>
#include <string>
#include "Gradebook.h"
#include "Student.h"
#include "GradebookPrinter.h"
using namespace std;
ofstream transLog("Grades.trn", ios::out | ios::app);
void saveGrades(Gradebook &gradebook) {
ofstream saveFile;
saveFile.open("Grades.dat");
gradebook.serialize(saveFile);
transLog << "Saved to Grades.dat" << endl;
}
void handleSetup(int &state, Gradebook &gradebook) {
int numPrograms, numTests, numFinals;
int programsWeight, testsWeight, finalExamWeight;
cout << "Enter number of programming assignments (Between 0-6): " << endl;
cin >> numPrograms;
while (numPrograms<0||numPrograms>6){
cout << "Error, incorrect value entered." <<endl<<"Enter number of programming assignments (Between 0-6): " << endl;
cin >> numPrograms;
}
cout << "Enter number of tests (Between 0-4): " << endl;
cin >> numTests;
while (numTests<0||numTests>4){
cout << "Error, incorrect value entered." <<endl<<"Enter number of tests (Between 0-4): " << endl;
cin >> numTests;
}
cout << "Enter number of Final Exams: (0 or 1)" << endl;
cin >> numFinals;
while (numFinals<0||numFinals>1){
cout << "Error, incorrect value entered." <<endl<< "Enter number of Final Exams: (0 or 1)" << endl;
cin >> numFinals;
}
if (numFinals > 0) {
cout << "Enter the relative percentages for programs, tests, and final exam. example: \n30 50 20" << endl;
cin >> programsWeight >> testsWeight >> finalExamWeight;
while ((programsWeight + testsWeight + finalExamWeight) != 100) {
cout << "Error, must equal 100%" << endl;
cout << "Enter the relative percentages for programs, tests, and final exam. example: \n30 50 20" << endl;
cin >> programsWeight >> testsWeight >> finalExamWeight;
}
} else {
cout << "Enter the relative percentages for programs, and tests. example: \n40 60" << endl;
cin >> programsWeight >> testsWeight;
while ((programsWeight + testsWeight) != 100) {
cout << "Error, must equal 100%" << endl;
cout << "Enter the relative percentages for programs, and tests. example: \n40 60" << endl;
cin >> programsWeight >> testsWeight;
}
}
gradebook.initialize(
numPrograms,
numTests,
numFinals,
programsWeight,
testsWeight,
finalExamWeight
);
saveGrades(gradebook);
state = 1;
transLog.close();
transLog.open("Grades.trn");
transLog << "Initialized Gradebook:"
<< " Programs: " << numPrograms
<< " Tests: " << numTests
<< " Finals: " << numFinals << " | "
<< " Pwt: " << programsWeight
<< " Twt: " << testsWeight
<< " Fwt: " << finalExamWeight << endl;
}
void handleQuit(int &state, Gradebook &gradebook) {
//program hangs if no semester (student) data and no prior Grades.dat
if (state == 0) {
transLog << "Quit" << endl;
return;
}
cout << "Saving grades and exiting..." << endl;
saveGrades(gradebook);
state = 2;
transLog << "Quit" << endl;
}
void handleModeToggle(bool &idMode) {
idMode = !idMode;
cout << "Toggled output mode to: " << ( idMode ? "by Id" : "by Name" ) << endl;
transLog << "Toggled output mode to: " << ( idMode ? "by Id" : "by Name" ) << endl;
}
void handleOutputGrades(Gradebook &gradebook, bool idMode) {
ofstream outfile;
outfile.open("Grades.out");
GradebookPrinter::printGradebook(gradebook, cout, idMode);
cout << "\nOutputing the complete gradebook to Grades.out..." << endl;
GradebookPrinter::printGradebook(gradebook, outfile, idMode);
cout << endl;
transLog << "Output grades to Grades.out" << endl;
}
void handleAddProgramGrade(Gradebook &gradebook, bool idMode) {
int programNumber, programGrade;
cout << "Enter programming assignment number: (1 - " << gradebook.getNumPrograms() << ")" << endl;
cin >> programNumber;
if (programNumber < 1 || programNumber > gradebook.getNumPrograms()) {
cout << "Invalid Program Number." << endl;
return;
}
if (gradebook.getProgramRecorded(programNumber)) {
cout << "Program " << programNumber << " already recorded..." << endl;
return;
}
Student * student;
if (!idMode) {
Node * current = gradebook.getIndex();
while (current != NULL) {
student = current->student;
cout << "Enter Program Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> programGrade;
student->setProgramGrade(programNumber - 1, programGrade);
saveGrades(gradebook);
current = current->next;
}
} else {
student = gradebook.getHead();
while (student != NULL) {
cout << "Enter Program Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> programGrade;
student->setProgramGrade(programNumber - 1, programGrade);
saveGrades(gradebook);
student = student->next;
}
}
gradebook.setProgramRecorded(programNumber);
saveGrades(gradebook);
handleOutputGrades(gradebook, idMode);
transLog << "Recorded grades for Program: " << programNumber << endl;
}
void handleAddTestGrade(Gradebook &gradebook, bool idMode) {
int testNumber, testGrade;
cout << "Enter test number: (1 - " << gradebook.getNumTests() << ")" << endl;
cin >> testNumber;
if (testNumber < 1 || testNumber > gradebook.getNumTests()) {
cout << "Invalid Test Number." << endl;
return;
}
if (gradebook.getTestRecorded(testNumber)) {
cout << "Test " << testNumber << " already recorded..." << endl;
return;
}
Student * student;
if (!idMode) {
Node * current = gradebook.getIndex();
while (current != NULL) {
student = current->student;
cout << "Enter Test " << testNumber << " Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> testGrade;
student->setTestGrade(testNumber - 1, testGrade);
saveGrades(gradebook);
current = current->next;
}
} else {
student = gradebook.getHead();
while (student != NULL) {
cout << "Enter Test " << testNumber << " Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> testGrade;
student->setTestGrade(testNumber - 1, testGrade);
saveGrades(gradebook);
student = student->next;
}
}
gradebook.setTestRecorded(testNumber);
saveGrades(gradebook);
handleOutputGrades(gradebook, idMode);
transLog << "Recorded grades for Test: " << testNumber << endl;
}
void handleAddFinalExamGrade(Gradebook &gradebook, bool idMode) {
int finalExamGrade;
Student * student;
if (gradebook.getFinalExamRecorded()) {
cout << "Final Exam Already Recorded..." << endl;
return;
}
if (!idMode) {
Node * current = gradebook.getIndex();
while (current != NULL) {
student = current->student;
cout << "Enter Final Exam Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> finalExamGrade;
student->setFinalExamGrade(finalExamGrade);
saveGrades(gradebook);
current = current->next;
}
} else {
student = gradebook.getHead();
while (student != NULL) {
cout << "Enter Final Exam Grade for "
<< student->getLastName() << ", " << student->getFirstName() << endl;
cin >> finalExamGrade;
student->setFinalExamGrade(finalExamGrade);
saveGrades(gradebook);
student = student->next;
}
}
gradebook.setFinalExamRecorded();
saveGrades(gradebook);
handleOutputGrades(gradebook, idMode);
transLog << "Recorded grades for Final" << endl;
}
void handleChangeGrade(Gradebook &gradebook) {
int studentId, gradeNumber, newGrade;
char gradeType;
Student * student;
cout << "Enter the student number: " << endl;
cin >> studentId;
student = gradebook.findStudentById(studentId);
if (student == NULL) {
cout << "Student number " << studentId << " not found\n";
return;
}
if (gradebook.getNumFinals() > 0) {
cout << "Enter the type of grade: ('P' for program, 'T' for test, 'F' for final exam)" << endl;
} else {
cout << "Enter the type of grade: ('P' for program, 'T' for test)" << endl;
}
cin >> gradeType;
gradeType = toupper(gradeType);
switch (gradeType) {
case 'P':
cout << "Enter the program number (1 - " << gradebook.getNumPrograms() << ")\n";
cin >> gradeNumber;
if (gradeNumber < 1 || gradeNumber > gradebook.getNumPrograms()) {
cout << "Invalid program number.\n";
return;
}
break;
case 'T':
cout << "Enter the test number (1 - " << gradebook.getNumTests() << ")\n";
cin >> gradeNumber;
if (gradeNumber < 1 || gradeNumber > gradebook.getNumTests()) {
cout << "Invalid test number.\n";
return;
}
break;
case 'F':
if (gradebook.getNumFinals() > 0) {
break;
}
default:
cout << "Unrecognized Grade Type\n";
return;
}
cout << "Enter the new grade: " << endl;
cin >> newGrade;
while (newGrade < 0 || newGrade>100) {
cout << "Error, please enter a value between 0-100: " << endl;
cin >> newGrade;
}
transLog << "Changed student " << student->getId();
if (gradeType == 'P') {
cout << "Changing the grade for program " << gradeNumber << " to " << newGrade << endl;
student->setProgramGrade(gradeNumber - 1, newGrade);
transLog << " program " << gradeNumber << " to " << newGrade << endl;
} else if (gradeType == 'T') {
cout << "Changing the grade for test " << gradeNumber << " to " << newGrade << endl;
student->setTestGrade(gradeNumber - 1, newGrade);
transLog << " test " << gradeNumber << " to " << newGrade << endl;
} else {
cout << "Changing the final exam grade to " << newGrade << endl;
transLog << " Final grade from " << student->getFinalExamGrade() << " to " << newGrade << endl;
student->setFinalExamGrade(newGrade);
}
}
void handleCalculateGrades(Gradebook &gradebook, bool idMode) {
cout << "Calculating the final grades for every student..." << endl;
Student * student = gradebook.getHead();
while (student != NULL) {
student->calculateFinalAverage(
gradebook.getProgramsWeight(),
gradebook.getTestsWeight(),
gradebook.getFinalExamWeight()
);
saveGrades(gradebook);
student = student->next;
}
transLog << "Final grades calculated" << endl;
handleOutputGrades(gradebook, idMode);
}
void handleAddStudent(Gradebook &gradebook) {
string firstName, lastName;
int studentId;
cout << "Enter the new student's first and last name: " << endl;
cin >> firstName;
while (firstName.length() > 20){
cout<< "Error, too many characters, please shorten to less than 20"<<endl;
cout << "Enter the new student's first name: " << endl;
cin >> firstName;
}
cin >> lastName;
while (lastName.length() > 20){
cout<< "Error, too many characters, please shorten to less than 20"<<endl;
cout << "Enter the new student's last name: " << endl;
cin >> lastName;
}
cout << "Enter the new student's ID number: " << endl;
cin >> studentId;
while (studentId>9999 || studentId<1){
cout<< " Error please enter a value between 1-9999"<< endl;
cout << "Enter the new student's ID number: " << endl;
cin >> studentId;
}
cout << "Adding " << firstName << " " << lastName << " (" << studentId << ") to the gradebook" << endl;
try {
Student * newStudent = gradebook.addStudent(studentId, firstName, lastName);
cout << "Successfully added student " << newStudent->getId() << ": "
<< newStudent->getFirstName() << " " << newStudent->getLastName() << endl;
saveGrades(gradebook);
} catch (const string& reason) {
cout << "Unable to add new student: " << endl << reason << endl;
}
transLog << "Added " << firstName << " " << lastName << " id " << studentId << endl;
}
void displayMenu(int state, Gradebook& gradebook, bool idMode) {
switch (state) {
case 0:
cout << "WELCOME TO GRADEBOOK!" << endl;
cout << "Type 'S' to setup a new semester." << endl;
cout << "Type 'Q' to quit." << endl;
break;
case 1:
cout << "GRADEBOOK MENU" << endl;
cout << "Type 'S' to re-initialize the semester (all grades will be lost)." << endl;
cout << "Type 'A' to add a new student." << endl;
if (gradebook.getNumStudents() > 0) {
cout << "Type 'P' to enter a program grade for all students." << endl;
cout << "Type 'T' to enter a test grade for all students." << endl; if (gradebook.getNumFinals() > 0) {
cout << "Type 'F' to enter a final exam grade for all students." << endl;
}
}
cout << "Type 'C' to change a grade for one student." << endl;
cout << "Type 'G' to calculate and store the final grade for all students." << endl;
cout << "Type 'O' to output the grade data." << endl;
cout << "Type 'M' to toggle the output mode (currently " << ((idMode) ? "by Id)" : "by Name)") << endl;
cout << "Type 'Q' to quit" << endl;
}
}
int main() {
int state; // 0 = unitialized, 1 = initialized, 2 = done
bool idMode = true; // Whether to print students by id or by name.
Gradebook gradebook;
char C = '?';
bool done = false;
ifstream inData;
try {
inData.open("Grades.dat");
if (!inData) {
state = 0;
} else {
gradebook.deserialize(inData);
state = 1;
}
while (C != 'Q' && state != 2) {
displayMenu(state, gradebook, idMode);
cin >> C;
C = toupper(C);
if (state == 0) {
switch (C) {
case 'Q':
handleQuit(state, gradebook);
break;
case 'S':
handleSetup(state, gradebook);
break;
default :
cout << "Unrecognized command." << endl;
break;
}
} else if (state == 1) {
switch (C) {
case 'A':
handleAddStudent(gradebook);
break;
case 'G':
handleCalculateGrades(gradebook, idMode);
break;
case 'O':
handleOutputGrades(gradebook, idMode);
break;
case 'S':
handleSetup(state, gradebook);
break;
case 'Q':
handleQuit(state, gradebook);
break;
case 'M':
handleModeToggle(idMode);
break;
case 'P':
if (gradebook.getNumStudents() > 0) {
handleAddProgramGrade(gradebook, idMode);
break;
}
case 'T':
if (gradebook.getNumStudents() > 0) {
handleAddTestGrade(gradebook, idMode);
break;
}
case 'F':
if (gradebook.getNumStudents() > 0) {
handleAddFinalExamGrade(gradebook, idMode);
break;
}
case 'C':
if (gradebook.getNumStudents() > 0) {
handleChangeGrade(gradebook);
break;
}
default :
cout << "Unrecognized Command." << endl;
}
}
}
} catch (string& e) {
cout << "Got exception:" << endl << e;
exit(1);
}
return 0;
}