-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1710 lines (1680 loc) · 72.4 KB
/
Copy pathmain.cpp
File metadata and controls
1710 lines (1680 loc) · 72.4 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <string>
#include <map>
#include <filesystem>
#include <stdio.h>
#include <sstream>
using namespace std;
namespace fs = std::filesystem;
enum class UserRole {
Admin,
User
};
class Encryption {
public:
static string shiftEncrypt(const string& input ) {
string result = input;
int shiftAmount = 3;
for (size_t i = 0; i < input.size(); ++i) {
char currentChar = input[i];
if (isalpha(currentChar)) {
char base = isupper(currentChar) ? 'A' : 'a';
result[i] = static_cast<char>((currentChar - base + shiftAmount + 26) % 26 + base);
}
else if (isdigit(currentChar)) {
result[i] = static_cast<char>((currentChar - '0' + shiftAmount + 10) % 10 + '0');
}
}
return result;
}
static string shiftDecrypt(const string& input) {
string result = input;
int shiftAmount = -3;
for (size_t i = 0; i < input.size(); ++i) {
char currentChar = input[i];
if (isalpha(currentChar)) {
char base = isupper(currentChar) ? 'A' : 'a';
result[i] = static_cast<char>((currentChar - base + shiftAmount + 26) % 26 + base);
}
else if (isdigit(currentChar)) {
result[i] = static_cast<char>((currentChar - '0' + shiftAmount + 10) % 10 + '0');
}
}
return result;
}
};
class User {
private:
string username, password, fullName, address, phone, userType;
int unicalId;
public:
User(){}
User(int unicalId, string username, string password, string fullName, string address, string phone, string userType)
: unicalId(unicalId), username(move(username)), password(move(password)),
fullName(move(fullName)), address(move(address)),
phone(move(phone)), userType(move(userType)) {
this->unicalId = unicalId;
}
int getUnicalId() {return unicalId;}
string getUsername() { return username; }
string getPassword() { return password; }
string getFullName() { return fullName; }
string getAddress() { return address; }
string getPhone() { return phone; }
string getUsertype() { return userType; }
void setUnicalId(int unicalId) {this->unicalId = unicalId;}
void setUsername(string username) { this->username = move(username); }
void setPassword(string password) { this->password = move(password); }
void setFullName(string fullName) { this->fullName = move(fullName); }
void setAddress(string address) { this->address = move(address); }
void setPhone(string phone) { this->phone = move(phone); }
void setUsertype(string userType) { this->userType = move(userType); }
friend ostream& operator<<(ostream& out, User& user);
};
ostream& operator<<(ostream& out, User& user) {
out <<user.getUnicalId()<<endl<< Encryption::shiftEncrypt(user.getUsername()) << endl << Encryption::shiftEncrypt(user.getPassword()) << endl << Encryption::shiftEncrypt(user.getFullName()) << endl << Encryption::shiftEncrypt(user.getAddress()) << endl << Encryption::shiftEncrypt(user.getPhone()) << endl << user.getUsertype() << endl;
return out;
}
class UsersVector {
private:
bool AdminExist = false;
vector<User> users;
public:
bool isUserExist(string username) {
for (auto& user : users) {
if (user.getUsername() == username) {
return true;
}
}
return false;
}
bool isUserExistPasswordCorrect(const string& username, const string& password) {
for (auto& user : users)
if (user.getUsername() == username && user.getPassword() == password) {
return true;
}
return false;
}
bool findAdmin() {
for (auto& user : users) {
if (user.getUsertype() == "admin") {
return true;
}
}
return false;
}
void addUsers(User&& user) {
users.push_back(user);
}
void registrationAsAdminFromParametrs(int unicalId, const string& username, const string& password) {
if (!findAdmin()) {
if (!isUserExist(username)) {
string userType = "admin";
users.emplace_back(unicalId, username, password, "-", "-", "-", userType);
}
}
}
void registrationAsStudentFromParametrs(int unicalId, const string& username, const string& password, const string& fullName, const string& address, const string& phone) {
if (!isUserExist(username)) {
string userType = "student";
users.emplace_back(unicalId, username, password, fullName, address, phone, userType);
}
}
bool isUserIsAdmin(const string& username) {
for (auto& user : users) {
if (user.getUsertype() == "admin" && user.getUsername() == username) {
return true;
}
return false;
}
return false;
}
vector<User>& getUsers() {
return users;
}
int getUnicalId(const string& username, const string& password) {
for (auto& user : users)
if (user.getUsername() == username && user.getPassword() == password) {
return user.getUnicalId();
}
return false;
}
};
class LogIn {
private:
bool logInAsAdmin = false;
bool logInAsStudent = false;
public:
void loginInSystem(UsersVector& usersWork, const string& username, const string& password) {
if (!usersWork.isUserExist(username)) {
cerr << "Incorrect username. Please try again." << endl;
}
else {
if (usersWork.isUserExist(username) && !usersWork.isUserExistPasswordCorrect(username, password)) {
cerr << "Incorrect password. Please try again." << endl;
}
else {
if (usersWork.isUserIsAdmin(username)) {
this->logInAsAdmin = true;
this->logInAsStudent = false;
}
else if (!usersWork.isUserIsAdmin(username)) {
this->logInAsAdmin = false;
this->logInAsStudent = true;
}
cout << "Login successful." << endl;
}
}
}
bool getLogInAsAdmin() { return logInAsAdmin; }
bool getLogInAsStudent() { return logInAsStudent; }
};
class Question {
private:
string text;
map<int, string> options;
int correctAnswer;
public:
Question() {}
Question(const string& qText) : text(qText){}
void addOption(int option, const string& optionText) {
this->options[option] = optionText;
}
void setCorrectAnswer(int correct) {
this->correctAnswer = correct;
}
void setText(const string& qText) {
this->text = qText;
}
string& getText(){ return text; }
const map<int, string>& getOptions() { return options; }
int getCorrectAnswer() const { return correctAnswer; }
void setOptions(const map<int, string>& newOptions) {
options = newOptions;
}
void setOption(int option, const string& optionText) {
options[option] = optionText;
}
void setCorrectAnswerChar(int correct) {
correctAnswer = correct;
}
};
class Test {
private:
string testName;
vector<Question*> questions;
public:
Test() {}
Test(const string& tname) : testName(tname) {}
void addQuestion(Question* question) {
questions.push_back(question);
}
const vector<Question*>& getQuestions() const { return questions; }
const string& getTestName() const { return testName; }
void setQuestions(const vector<Question*>& newQuestions) {
questions = newQuestions;
}
};
class TestCategory {
private:
string categoryName;
vector<Test*> tests;
public:
TestCategory() {}
TestCategory(const string& name) : categoryName(name) {}
void addTest(Test* test) {
tests.push_back(test);
}
vector<Test*>& getTests() { return tests; }
const string& getCategoryName() const { return categoryName; }
};
class TestSession {
private:
vector<TestCategory*> categories;
int totalQuestions;
public:
TestSession() {}
void addTestCategory(const string& categoryName) {
for (TestCategory *category: categories) {
if(categoryName == category->getCategoryName()){
return;
}
}
categories.push_back(new TestCategory(categoryName));
}
void addTestToCategory(const string& categoryName, Test* test) {
for (TestCategory *category: categories) {
if (category->getCategoryName() == categoryName) {
category->addTest(test);
return;
}
}
}
void addQuestionToTest(Question* question, Test* test) {
test->addQuestion(question);
}
const vector<TestCategory*>& getCategories() {
return categories;
}
const vector<TestCategory*> getCategoriesByIndex(int index) {
if (index >= 0 && index < categories.size()) {
vector<TestCategory*> result;
result.push_back(categories[index]);
return result;
}
else {
cerr << "Error: Index out of bounds." << endl;
return vector<TestCategory*>();
}
}
void setCategories(const vector<TestCategory*>& newCategories) {
categories = newCategories;
}
void setTotalAmountOfQuestions(const string& categoryName, const string& testName){
int counter = 0;
for (TestCategory* category : categories) {
if (category->getCategoryName() == categoryName) {
for (Test *test: category->getTests()) {
if (test->getTestName() == testName) {
const vector<Test *> testsInCategory = category[0].getTests();
for (Test *findTest: testsInCategory) {
const vector<Question *> questionsInTest = test->getQuestions();
for (Question *question: questionsInTest){
counter++;
}
break;
}
}
}
}
}
this->totalQuestions = counter;
};
int getTotalAmountOfQuestions() {
return totalQuestions;
}
};
class workWithFiles {
public:
void saveUsersToFile(UsersVector& usersWork, const string& filename) {
ofstream outFile(filename);
if (!outFile) {
cerr << "Error: Could not open the file for writing." << endl;
return;
}
for (auto& user : usersWork.getUsers()) {
outFile << user;
}
outFile.close();
}
void loadUsersFromFile(UsersVector& usersWork, const string& filename) {
ifstream inFile(filename);
if (!inFile) {
cerr << "Error: Could not open the file for reading." << endl;
return;
}
while (true) {
string unicalId, username, password, fullName, address, phone, userType;
getline(inFile, unicalId);
inFile.clear();
getline(inFile, username);
inFile.clear();
getline(inFile, password);
inFile.clear();
getline(inFile, fullName);
inFile.clear();
getline(inFile, address);
inFile.clear();
getline(inFile, phone);
inFile.clear();
getline(inFile, userType);
inFile.clear();
int value;
istringstream iss(unicalId);
iss >> value;
if(userType == ""){
inFile.close();
return;
}
usersWork.addUsers(User (move(value), move(Encryption::shiftDecrypt(username)), move(Encryption::shiftDecrypt(password)),move(Encryption::shiftDecrypt(fullName)),move(Encryption::shiftDecrypt(address)), move(Encryption::shiftDecrypt(phone)), move(userType )));
}
}
void saveTests(TestSession& testSessions, const string& filename) {
string categoryName, testName, questions;
int counterCategories = 0;
ofstream outFile(filename);
if (!outFile) {
cerr << "Error: Could not open the file for writing." << endl;
return;
}
for (TestCategory* category : testSessions.getCategories()) {
const vector<TestCategory*> categories = testSessions.getCategoriesByIndex(counterCategories);
if (!categories.empty()) {
categoryName = categories[0]->getCategoryName();
const vector<Test*> testsInCategory = categories[0]->getTests();
for (Test* findTest : testsInCategory) {
testName = findTest->getTestName();
const vector<Question*> questionsInTest = findTest->getQuestions();
for (Question* question : questionsInTest) {
const map<int, string>& options = question->getOptions();
outFile << categoryName<<endl<<testName<<endl<<question->getText() << endl;
for (const auto& option : options) {
outFile<<option.second<<endl;
}
outFile<<question->getCorrectAnswer()<<endl;
}
}
}
counterCategories++;
}
outFile.close();
}
void loadTests(TestSession& testSessions, const string& filename){
ifstream inFile(filename);
if (!inFile) {
cerr << "Error: Could not open the file for reading." << endl;
return;
}
while (!inFile.eof()) {
string categoryName, testName, questionText, optionText1, optionText2, optionText3, optionText4;
int correctAnswer;
getline(inFile, categoryName);
getline(inFile, testName);
getline(inFile, questionText);
getline(inFile, optionText1);
getline(inFile, optionText2);
getline(inFile, optionText3);
getline(inFile, optionText4);
inFile >> correctAnswer;
inFile.ignore(numeric_limits<streamsize>::max(), '\n');
if(categoryName != "" && testName != "" && questionText != "" && optionText1 != "" && optionText2 != "" && optionText3 != "" && optionText4 != "" && correctAnswer != 0){
bool addInExistCategory = true;
bool addInExistTest = true;
int counter = 0;
for (TestCategory *category: testSessions.getCategories()) {
if(categoryName == category->getCategoryName()){
addInExistCategory = false;
const vector<TestCategory *> categories = testSessions.getCategoriesByIndex(counter);
const vector<Test *> testsInCategory = categories[0]->getTests();
int counterTest = 0;
for (Test *findTest : testsInCategory) {
if(testName == findTest->getTestName()){
addInExistTest = false;
Question* question = new Question(questionText);
question->setText(move(questionText));
question->addOption(1, move(optionText1));
question->addOption(2, move(optionText2));
question->addOption(3, move(optionText3));
question->addOption(4, move(optionText4));
question->setCorrectAnswer(move(correctAnswer));
testSessions.addQuestionToTest(move(question), move(findTest));
}
counterTest++;
}
if(addInExistTest){
Test* test = new Test(testName);
testSessions.addTestToCategory(categoryName, test);
Question* question = new Question(questionText);
question->setText(move(questionText));
question->addOption(1, move(optionText1));
question->addOption(2, move(optionText2));
question->addOption(3, move(optionText3));
question->addOption(4, move(optionText4));
question->setCorrectAnswer(move(correctAnswer));
testSessions.addQuestionToTest(move(question), move(test));
}
}
counter++;
}
if(addInExistCategory && addInExistTest){
testSessions.addTestCategory(categoryName);
Test* test = new Test(testName);
testSessions.addTestToCategory(categoryName, test);
Question* question = new Question(questionText);
question->addOption(1, optionText1);
question->addOption(2, optionText2);
question->addOption(3, optionText3);
question->addOption(4, optionText4);
question->setCorrectAnswer(correctAnswer);
testSessions.addQuestionToTest(question, test);
}
correctAnswer = 0;
}
}
inFile.close();
}
void addResultsToTestResults(const string& filename, const string& category, const string& test,const string& question, int score){
ofstream outFile(filename, ios::app);
outFile <<category<<endl<<test<<endl<<question<<endl<<score<<endl;
outFile.close();
}
int loadTestForResult(const string& filename, const string& category, const string& test){
ifstream inFile(filename);
int correctAnswer = 0;
if (inFile) {
string none, none1, none2, none3;
while (!inFile.eof()) {
int checkCorrectAnswer = 0;
getline(inFile, none);
inFile.clear();
getline(inFile, none1);
inFile.clear();
getline(inFile, none2);
inFile.clear();
getline(inFile, none3);
if(none3 =="1" ){
correctAnswer++;
}
inFile.clear();
if(none3 == ""){
break;
}
}
}
inFile.close();
return correctAnswer;
}
void removeTest(const string& filename){
ifstream inFile(filename);
remove(filename.c_str());
}
string loadLastQuestionFromPausedTest(const string& filename){
ifstream inFile(filename);
string nothing;
string lastQuestion;
string lastQuestioncheck;
while (true) {
getline(inFile, nothing);
inFile.clear();
getline(inFile, nothing);
inFile.clear();
getline(inFile, lastQuestioncheck);
inFile.clear();
if(lastQuestioncheck!=""){
lastQuestion = lastQuestioncheck;
}
else{
break;
}
getline(inFile, nothing);
inFile.clear();
}
inFile.close();
return lastQuestion;
}
vector<string> getTestFilesInFolder(const string& folderPath) {
vector<string> testFiles;
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (entry.is_regular_file() && entry.path().extension() == ".txt") {
testFiles.push_back(entry.path().filename().string());
}
}
return testFiles;
}
int getUnicalId(const string& filename){
ifstream inFile(filename);
int unicalId = 0;
if (inFile) {
string none;
string UnicalIdcheck;
while (!inFile.eof()) {
int checkCorrectAnswer = 0;
getline(inFile, UnicalIdcheck);
inFile.clear();
int value;
istringstream iss(UnicalIdcheck);
iss >> value;
if(unicalId<=value && UnicalIdcheck != ""){
unicalId=value+1;
}
else if(UnicalIdcheck == ""){
inFile.close();
return unicalId;
}
getline(inFile, none);
inFile.clear();
getline(inFile, none);
inFile.clear();
getline(inFile, none);
inFile.clear();
getline(inFile, none);
inFile.clear();
getline(inFile, none);
inFile.clear();
getline(inFile, none);
inFile.clear();
}
}
return 0;
}
};
class Checker{
public:
static int selectAnswers(int from, int till);
};
int Checker::selectAnswers(int from, int till){
int selectAnswer;
stringstream ss;
stringstream sss;
ss<<from;
sss<<till;
string error = "Enter index from " + ss.str()+ " to " + sss.str()+ ". Try again: ";
while (true) {
cin >> selectAnswer;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
// throw out_of_range(error);
cout << "Invalid input. Please enter a valid number: ";
}
else if (selectAnswer == from || (selectAnswer > from-1 && selectAnswer <till+1 )) {
cin.ignore();
return selectAnswer;
}
else {
// throw out_of_range(error);
cout << "Invalid input. Please enter a valid number: " ;
}
}
}
class Administration {
private:
workWithFiles files;
public:
void showResultOfUser(TestSession& testsissions, UsersVector& usersWork, string pathToFolder, int unicalId){
int counter = 0;
string unicalIdString = to_string(unicalId);
for (TestCategory* category : testsissions.getCategories()) {
for (Test* test : category->getTests()) {
const vector<Question *> questionsInTest = test->getQuestions();
testsissions.setTotalAmountOfQuestions(category->getCategoryName(), test->getTestName());
vector<string> testFiles = files.getTestFilesInFolder(pathToFolder);
string fileName = "Results/"+category->getCategoryName() + "_" + test->getTestName() + "_" +"unicalId_" + unicalIdString + ".txt";
string fileNameForCheck = category->getCategoryName() + "_" + test->getTestName() + "_" +"unicalId_" + unicalIdString + ".txt";
for (const auto& fileNames : testFiles) {
if(fileNameForCheck == fileNames) {
int counter1 = 1;
for (Question *question: questionsInTest) {
if(files.loadLastQuestionFromPausedTest(fileName) == question->getText()){
break;
}
counter1++;
}
int correct = files.loadTestForResult(fileName, category->getCategoryName(), test->getTestName());
cout<<"Category: "<<category->getCategoryName()<<endl<<"Test: "<<test->getTestName()<<endl;
if(counter1 == testsissions.getTotalAmountOfQuestions()){
cout<<"User answered "<<correct<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl;
int percentage = correct*100 / testsissions.getTotalAmountOfQuestions();
cout<<"User score is "<< percentage<<"%"<<endl<<endl;
}
else{
cout<<"User test is paused. User are stoped at "<<counter1<<" question";
cout<<endl<<"Till now user answered "<<correct<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl;
int percentage = correct*100 / testsissions.getTotalAmountOfQuestions();
cout<<"User score is "<< percentage<<"%"<<endl<<endl;
}
counter++;
}
}
}
}
if(counter == 0){
cout<<endl<<"This user didn't pass any exams "<<endl<<endl;
}
}
void showResultsByTests(TestSession& testsissions, UsersVector& usersWork, string& pathToFolder, const string& categoryName, const string& testName){
int counter = 0;
vector<User> users = usersWork.getUsers();
for(auto& user : users){
if(user.getUsertype() != "admin"){
string unicalIdString = to_string(user.getUnicalId());
for (TestCategory* category : testsissions.getCategories()) {
for (Test* test : category->getTests()) {
if(category->getCategoryName() == categoryName && test->getTestName() == testName) {
const vector<Question *> questionsInTest = test->getQuestions();
testsissions.setTotalAmountOfQuestions(category->getCategoryName(), test->getTestName());
vector<string> testFiles = files.getTestFilesInFolder(pathToFolder);
string fileName =
"Results/" + category->getCategoryName() + "_" + test->getTestName() + "_" +
"unicalId_" + unicalIdString + ".txt";
string fileNameForCheck =
category->getCategoryName() + "_" + test->getTestName() + "_" + "unicalId_" +
unicalIdString + ".txt";
for (const auto &fileNames: testFiles) {
if (fileNameForCheck == fileNames) {
int counter1 = 1;
for (Question *question: questionsInTest) {
if (files.loadLastQuestionFromPausedTest(fileName) == question->getText()) {
break;
}
counter1++;
}
int correct = files.loadTestForResult(fileName, category->getCategoryName(),
test->getTestName());
cout<<"User "<<user.getFullName()<<endl;
cout << "Category: " << category->getCategoryName() << endl << "Test: "
<< test->getTestName() << endl;
if (counter1 == testsissions.getTotalAmountOfQuestions()) {
cout << "User answered " << correct << " from "<< testsissions.getTotalAmountOfQuestions() << endl;
int percentage = correct * 100 / testsissions.getTotalAmountOfQuestions();
cout << "User score is " << percentage << "%" << endl << endl;
} else {
cout << "User test is paused. User are stoped at " << counter1 << " question";
cout << endl << "Till now user answered " << correct << " from "
<< testsissions.getTotalAmountOfQuestions() << endl;
int percentage = correct * 100 / testsissions.getTotalAmountOfQuestions();
cout << "User score is " << percentage << "%" << endl << endl;
}
counter++;
}
}
}
}
}
}
}
if(counter == 0){
cout<<endl<<"Тo one has taken this test yet "<<endl<<endl;
}
}
void showResultByCategory(TestSession& testsissions, UsersVector& usersWork, string& pathToFolder, const string& categoryName){
int counter = 0;
vector<User> users = usersWork.getUsers();
for(auto& user : users){
if(user.getUsertype() != "admin"){
string unicalIdString = to_string(user.getUnicalId());
for (TestCategory* category : testsissions.getCategories()) {
if(category->getCategoryName() == categoryName) {
for (Test* test : category->getTests()) {
const vector<Question *> questionsInTest = test->getQuestions();
testsissions.setTotalAmountOfQuestions(category->getCategoryName(), test->getTestName());
vector<string> testFiles = files.getTestFilesInFolder(pathToFolder);
string fileName =
"Results/" + category->getCategoryName() + "_" + test->getTestName() + "_" +
"unicalId_" + unicalIdString + ".txt";
string fileNameForCheck =
category->getCategoryName() + "_" + test->getTestName() + "_" + "unicalId_" +
unicalIdString + ".txt";
for (const auto &fileNames: testFiles) {
if (fileNameForCheck == fileNames) {
int counter1 = 1;
for (Question *question: questionsInTest) {
if (files.loadLastQuestionFromPausedTest(fileName) == question->getText()) {
break;
}
counter1++;
}
int correct = files.loadTestForResult(fileName, category->getCategoryName(),test->getTestName());
cout<<"User "<<user.getFullName()<<endl;
cout << "Category: " << category->getCategoryName() << endl << "Test: " << test->getTestName() << endl;
if (counter1 == testsissions.getTotalAmountOfQuestions()) {
cout << "User answered " << correct << " from "
<< testsissions.getTotalAmountOfQuestions() << endl;
int percentage = correct * 100 / testsissions.getTotalAmountOfQuestions();
cout << "User score is " << percentage << "%" << endl << endl;
} else {
cout << "User test is paused. User are stoped at " << counter1 << " question";
cout << endl << "Till now user answered " << correct << " from "
<< testsissions.getTotalAmountOfQuestions() << endl;
int percentage = correct * 100 / testsissions.getTotalAmountOfQuestions();
cout << "User score is " << percentage << "%" << endl << endl;
}
counter++;
}
}
}
}
}
}
}
if(counter == 0){
cout<<endl<<"Тo one has taken this test yet "<<endl<<endl;
}
}
void showInfoAboutUser(UsersVector& usersWork, int indexToDisplay){
vector<User> users = usersWork.getUsers();
size_t select = indexToDisplay;
if (select <= users.size()) {
cout << "===== User info =====\n";
cout << "Unical id: " <<users[indexToDisplay].getUnicalId()<<endl;
cout << "Username: " <<users[indexToDisplay].getUsername()<<endl;
cout << "Password: "<<users[indexToDisplay].getPassword()<<endl;
cout << "Fullname: "<<users[indexToDisplay].getFullName()<<endl;
cout << "Adres: "<<users[indexToDisplay].getAddress()<<endl;
cout << "Phone: "<<users[indexToDisplay].getPhone()<<endl;
}
}
void setNewFullnameToUser(UsersVector& usersWork, string newFullname, int indexVector){
size_t indexToDisplay = indexVector;
vector<User>& users = usersWork.getUsers();
if (indexToDisplay <= users.size()) {
users[indexToDisplay].setFullName(newFullname);
}
}
void setNewUsernameToUser(UsersVector& usersWork, string newUsername, int indexVector){
size_t indexToDisplay = indexVector;
vector<User>& users = usersWork.getUsers();
if (indexToDisplay <= users.size()) {
users[indexToDisplay].setUsername(newUsername);
}
}
void setNewPhoneNumberToUser(UsersVector& usersWork, string phonenumber, int indexVector){
size_t indexToDisplay = indexVector;
vector<User>& users = usersWork.getUsers();
if (indexToDisplay <= users.size()) {
users[indexToDisplay].setPhone(phonenumber);
}
}
void setNewAddressToUser(UsersVector& usersWork, string address, int indexVector){
size_t indexToDisplay = indexVector;
vector<User>& users = usersWork.getUsers();
if (indexToDisplay <= users.size()) {
users[indexToDisplay].setAddress(address);
}
}
void setNewPasswordToUser(UsersVector& usersWork, string password, int indexVector){
size_t indexToDisplay = indexVector;
vector<User>& users = usersWork.getUsers();
if (indexToDisplay <= users.size()) {
users[indexToDisplay].setPassword(password);
}
}
void showUsersList(UsersVector& usersWork){
vector<User> users = usersWork.getUsers();
int i = 0;
for(auto& user : users){
if(user.getUsertype() != "admin"){
cout<<i+1<<"." <<user.getFullName()<<endl;
i++;
}
}
}
void deleteUser(UsersVector& usersWork, size_t indexVector){
vector<User>& users = usersWork.getUsers();
if (indexVector < users.size()) {
users.erase(users.begin() + indexVector);
}
}
int countOfUsers(UsersVector& usersWork){
vector<User> users = usersWork.getUsers();
int i = 0;
for(auto& user : users){
if(user.getUsertype() != "admin"){
i++;
}
}
return i;
}
};
class Student{
workWithFiles files;
public:
void startTest(TestSession& testsissions, int unicalId, const string& categoryName, const string& testName, string pathToFolder, bool restartOrContinue) {
int selectAnswer;
int score;
testsissions.setTotalAmountOfQuestions(categoryName,testName);
string unicalIdString = to_string(unicalId);
for (TestCategory* category : testsissions.getCategories()) {
int coutCorrect = 0;
if (category->getCategoryName() == categoryName) {
for (Test* test : category->getTests()) {
if (test->getTestName() == testName) {
const vector<Question *> questionsInTest = test->getQuestions();
vector<string> testFiles = files.getTestFilesInFolder(pathToFolder);
string fileName = "Results/"+categoryName + "_" + testName + "_" +"unicalId_" + unicalIdString + ".txt";
string fileNameForCheck = categoryName + "_" + testName + "_" +"unicalId_" + unicalIdString + ".txt";
int choose = 0;
for (const auto& fileNames : testFiles) {
if(fileNameForCheck == fileNames){
int counter1 = 0;
for (Question *question: questionsInTest) {
if(files.loadLastQuestionFromPausedTest(fileName) == question->getText()){
break;
}
counter1++;
}
if(!restartOrContinue){
files.removeTest(fileName);
}
if(restartOrContinue){
int counter2 = 0;
for (Question *question: questionsInTest) {
if(counter1 < counter2){
cout << "Question: " << question->getText() << endl;
const map<int, string> &options = question->getOptions();
for (const auto &option: options) {
cout << "Option " << option.first << ": " << option.second << endl;
}
cout << "Enter your answer or press 0 to exit test: ";
selectAnswer = Checker::selectAnswers(0,4);
if(question->getCorrectAnswer() == selectAnswer) {
cout<<"Correct! "<<endl;
files.addResultsToTestResults(fileName,categoryName,testName, question->getText(),1);
coutCorrect++;
}
else if(selectAnswer == 0){
return;
}
else{
cout<<"Incorrect! "<<endl;
files.addResultsToTestResults(fileName,categoryName,testName, question->getText(),0);
}
}
counter2++;
}
int correct = files.loadTestForResult(fileName, categoryName, testName);
coutCorrect+=correct;
cout<<endl<<"You answered "<<coutCorrect<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl;
int percentage = coutCorrect*100 / testsissions.getTotalAmountOfQuestions();
cout<<"Your score is "<< percentage<<"%"<<endl;
return;
}
}
}
int correct = 0;
for (Question *question: questionsInTest) {
cout << "Question: " << question->getText() << endl;
const map<int, string> &options = question->getOptions();
for (const auto &option: options) {
cout << "Option " << option.first << ": " << option.second << endl;
}
cout << "Enter your answer or press 0 to exit test: ";
selectAnswer = Checker::selectAnswers(0,4);
if (question->getCorrectAnswer() == selectAnswer) {
cout << "Correct! " << endl;
correct++;
files.addResultsToTestResults(fileName, categoryName, testName, question->getText(), 1);
}
else if (selectAnswer == 0) {
return;
}
else {
cout << "Incorrect! " << endl;
files.addResultsToTestResults(fileName, categoryName, testName, question->getText(), 0);
}
}
cout<<endl<<"You answer "<<correct<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl<<endl;
int percentage = correct*100 / testsissions.getTotalAmountOfQuestions();
cout<<percentage<<"%"<<endl;
}
}
return;
}
}
}
void showResults(TestSession& testsissions, string pathToFolder, int unicalId){
int counter = 0;
string unicalIdString = to_string(unicalId);
for (TestCategory* category : testsissions.getCategories()) {
for (Test* test : category->getTests()) {
const vector<Question *> questionsInTest = test->getQuestions();
testsissions.setTotalAmountOfQuestions(category->getCategoryName(), test->getTestName());
vector<string> testFiles = files.getTestFilesInFolder(pathToFolder);
string fileName = "Results/"+category->getCategoryName() + "_" + test->getTestName() + "_" +"unicalId_" + unicalIdString + ".txt";
string fileNameForCheck = category->getCategoryName() + "_" + test->getTestName() + "_" +"unicalId_" + unicalIdString + ".txt";
for (const auto& fileNames : testFiles) {
if(fileNameForCheck == fileNames) {
int counter1 = 1;
for (Question *question: questionsInTest) {
if(files.loadLastQuestionFromPausedTest(fileName) == question->getText()){
break;
}
counter1++;
}
int correct = files.loadTestForResult(fileName, category->getCategoryName(), test->getTestName());
cout<<"Category: "<<category->getCategoryName()<<endl<<"Test: "<<test->getTestName()<<endl;
if(counter1 == testsissions.getTotalAmountOfQuestions()){
cout<<"You answered "<<correct<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl;
int percentage = correct*100 / testsissions.getTotalAmountOfQuestions();
cout<<"Your score is "<< percentage<<"%"<<endl<<endl;
}
else{
cout<<"Your test is paused. You are stoped at "<<counter1<<" question";
cout<<endl<<"Till now you answered "<<correct<<" from "<<testsissions.getTotalAmountOfQuestions()<<endl;
int percentage = correct*100 / testsissions.getTotalAmountOfQuestions();
cout<<"Your score is "<< percentage<<"%"<<endl<<endl;
}
counter++;
}
}
}
}
if(counter == 0){
cout<<endl<<"You dont pass any exams "<<endl<<endl;
}
}
};
class ConsoleInterface {
string pathToFolder = "/Users/a2141pro4/CLionProjects/ExamWork/cmake-build-debug/Results";
string usersTxt = "users.txt";
string testsTxt = "alltests.txt";
int unicalId;
string username, password, fullName, address, phone;
string categoryName, testName;
Administration administrator;
Student student;
workWithFiles workWithFile;
UsersVector usersWork;
LogIn logForReg;
TestSession session;
//Main menu
void displayMainMenu() {
cout << "===== Main Menu =====\n";
cout << "1. Sign in\n";
cout << "2. Log in\n";
cout << "3. Exit\n";
cout << "=====================\n";
cout << "Enter your choice: ";
}
void displaySignInMenu() {
bool flag = false;
while (!flag) {
cout << "===== Sign In Menu =====\n";