-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.cpp
More file actions
1999 lines (1827 loc) · 47.1 KB
/
Classes.cpp
File metadata and controls
1999 lines (1827 loc) · 47.1 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 "Classes.h"
#include <bitset>
// Static Counters
int Teachers::totalteachers = 0;
int Students::totalstudents = 0;
int Courses::totalcourses = 0;
int Exams::totalexams = 0;
int Members::totalmembers = 0;
// Forward Declaration
School ENSIA;
// Date --------------------------------------------
Date::Date() : day(0), month(0), year(0) {}
Date::Date(int day, int month, int year) : day(day), month(month), year(year) {}
Date::~Date() {};
// Setters and Getters
void Date::setDay(int day)
{
this->day = day;
}
void Date::setMonth(int month)
{
this->month = month;
}
void Date::setYear(int year)
{
this->year = year;
}
void Date::setDate(int day, int month, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
int Date::getDay() const
{
return day;
}
int Date::getMonth() const
{
return month;
}
int Date::getYear() const
{
return year;
}
// Courses Class -------------------------------------------------------------------
// Setters
void Courses::setName(const string &newName)
{
name = newName;
}
void Courses::setGroups(const bool newgroups[12])
{
for (int i = 0; i < 12; i++)
{
groups[i] = newgroups[i];
}
}
void Courses::setSchedule(const string schedule[7][2])
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
this->schedule[i][j] = schedule[i][j];
}
}
}
void Courses::setId(const int id)
{
this->id = id;
}
string Courses::getName() const
{
return name;
}
string (*Courses::getSchedule())[2]
{
return schedule;
}
bool *Courses::getGroups()
{
return groups;
}
int Courses::getId()
{
return id;
}
Courses::Courses()
{
setName("Unknown");
Courses::totalcourses++;
for (int i = 1; i < 12; i++)
{
groups[i] = false;
}
// the schedule :
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
schedule[i][j] = "";
}
}
}
Courses::Courses(const string &newName, string newSchedule[7][2], const bool newgroups[12])
{
Courses::totalcourses++;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
schedule[i][j] = newSchedule[i][j];
}
}
setName(newName);
setSchedule(newSchedule);
setGroups(newgroups);
setId(ENSIA.getCourses().size());
ENSIA.addCourse(this);
}
Courses::~Courses()
{
// No dynamic memory to deallocate
// cout << "Course " << name << " ID :" << id << " has been Removed." << endl;
}
// Exams --------------------------------------------
Exams::Exams() : Module{}, start{}, end{}, groups{0} {
// cout << "Exam has been created." << endl;
};
Exams::Exams(Courses *Module, float Marks[7], const Time *start, const Time *end, bool groups[12]) : Module{Module}, start{*start}, end{*end}
{
Exams::totalexams++;
setId(ENSIA.getExams().size());
ENSIA.addExam(this);
for (int i = 0; i < 12; i++)
{
this->groups[i] = groups[i];
}
// cout << "Exam has been created." << endl;
};
Exams::~Exams()
{
// cout << "Exam has been removed." << endl;
}
void Exams::setModule(Courses *Module)
{
this->Module = Module;
};
Courses *Exams::getModule()
{
return Module;
};
void Exams::setDate(const Time &start, const Time &end)
{
this->start = start;
this->end = end;
};
Time Exams::getStartDate()
{
return start;
};
Time Exams::getEndDate()
{
return end;
};
void Exams::setGroups(const bool groups[12])
{
for (int i = 0; i < 12; i++)
{
this->groups[i] = groups[i];
}
}
bool *Exams::getGroups()
{
return groups;
}
void Exams::setId(int id)
{
this->id = id;
}
int Exams::getid()
{
return id;
}
Time &Exams::getTimeStart()
{
return start;
}
Time &Exams::getTimeEnd()
{
return end;
}
string Exams::getResponsible()
{
return responsible;
}
void Exams::setResponsible(const string &newResponsible)
{
responsible = newResponsible;
}
bool Exams::fixschedule()
{
struct ExamSlot
{
int day;
int month;
int year;
int startMinutes;
int endMinutes;
std::bitset<12> groups;
};
std::vector<ExamSlot> examSlots;
// Get all exams from ENSIA
const auto &allExams = ENSIA.getExams();
// Helper function to convert Time to minutes since midnight
auto timeToMinutes = [](const Time &time)
{
return time.getHour() * 60 + time.getMinute();
};
// Helper function to check if two bitsets have any common set bits
auto haveCommonGroups = [](const std::bitset<12> &a, const std::bitset<12> &b)
{
return (a & b).any();
};
// Collect time slots for all exams
for (const auto &exam : allExams)
{
if (exam != this)
{
ExamSlot slot;
slot.day = exam->getStartDate().getDay();
slot.month = exam->getStartDate().getMonth();
slot.year = exam->getStartDate().getYear();
slot.startMinutes = timeToMinutes(exam->getStartDate());
slot.endMinutes = timeToMinutes(exam->getEndDate());
// Convert bool array to bitset
for (int i = 0; i < 12; ++i)
{
slot.groups[i] = exam->getGroups()[i];
}
examSlots.push_back(slot);
}
}
// Create the current exam's slot
ExamSlot currentSlot;
currentSlot.day = this->start.getDay();
currentSlot.month = this->start.getMonth();
currentSlot.year = this->start.getYear();
currentSlot.startMinutes = timeToMinutes(this->start);
currentSlot.endMinutes = timeToMinutes(this->end);
// Convert bool array to bitset for current exam
for (int i = 0; i < 12; ++i)
{
currentSlot.groups[i] = this->getGroups()[i];
}
// Check for overlaps
for (const auto &slot : examSlots)
{
// Check if exams are on the same day
if (slot.day == currentSlot.day && slot.month == currentSlot.month && slot.year == currentSlot.year)
{
// Check if time slots overlap
if (currentSlot.startMinutes < slot.endMinutes && slot.startMinutes < currentSlot.endMinutes)
{
// Check if there are any common groups
if (haveCommonGroups(slot.groups, currentSlot.groups))
{
return true; // Conflict found
}
}
}
}
return false; // No conflicts found
}
// Time Class -------------------------------------------------------------------
Time::Time() : Date{}, hour(0), minute(0) {}
Time::Time(const int hour, const int minute, const int day, const int month, const int year) : Date(day, month, year), hour(hour), minute(minute) {}
Time::~Time() {}
void Time::setHour(int hour)
{
if (hour >= 0 && hour <= 23)
{
this->hour = hour;
}
else
{
// handle error
}
}
int Time::getHour() const
{
return this->hour;
}
void Time::setMinute(int minute)
{
if (minute >= 0 && minute <= 59)
{
this->minute = minute;
}
else
{
// handle error
}
}
int Time::getMinute() const
{
return this->minute;
}
void Time::setTime(int hour, int minute, int day, int month, int year)
{
setHour(hour);
setMinute(minute);
setDate(day, month, year);
}
// Members Class -------------------------------------------------------------------
void Members::setName(const string &name)
{
if (name != "")
{
this->name = name;
}
else
{
this->name = "Unknown";
}
}
string Members::getName() const
{
return name;
}
void Members::setId(const long int &id)
{
if (id > 0)
{
this->id = id;
}
else
{
this->id = 0;
}
}
long int Members::getId() const
{
return id;
}
void Members::setPassword(const string &password)
{
if (password != "")
{
this->password = password;
}
else
{
this->password = "x";
}
}
string Members::getPassword() const
{
return password;
}
void Members::setEmail(const string &email)
{
if (email != "")
{
this->email = email;
}
else
{
this->email = "";
}
}
string Members::getEmail() const
{
return email;
}
void Members::setPhone(const string &phone)
{
if (phone != "")
{
this->phone = phone;
}
else
{
this->phone = "0000000000";
}
}
string Members::getPhone() const
{
return phone;
}
void Members::setAddress(const string &address)
{
if (address != "")
{
this->address = address;
}
else
{
this->address = "Unknown";
}
}
string Members::getAddress() const
{
return address;
}
void Members::setCourses(const vector<Courses *> &courses)
{
this->courses = courses;
}
vector<Courses *> Members::getCourses() const
{
return courses;
}
void Members::setBirthDate(int day, int month, int year)
{
this->BD.setDay(day);
this->BD.setMonth(month);
this->BD.setYear(year);
}
Date &Members::getBirthDate()
{
return BD;
};
void Members::setevent(const string &event, QDateTime &time, int index)
{
cout << this->name << endl;
if (index < performance.size())
{
performance[index] = event;
dates[index] = time;
}
else
{
performance.push_back(event);
dates.push_back(time);
}
}
string Members::getevent(int index)
{
if (index < performance.size())
return performance[index];
else
return "";
}
QDateTime &Members::geteventtime(int index)
{
if (index < dates.size())
return dates[index];
}
vector<string> *Members::getevents()
{
return &performance;
}
vector<QDateTime> *Members::geteventstime()
{
return &dates;
}
Members::Members() : courses{}
{
name = "Unknown";
id = 0;
password = "x";
email = "email@ensia.edu.dz";
phone = "0000000000";
address = "Unknown";
totalmembers++;
performance.resize(0);
dates.resize(0);
}
Members::Members(const string &name, const string &password, const string &email, const string &phone, const string &address, const vector<Courses *> &courses, int day, int month, int year)
{
// default id :
setId(totalmembers);
// Checking Name
if (name != "")
{
setName(name);
}
else
{
cerr << "Name is Empty!" << endl;
}
// Checking Password
if (password != "")
{
setPassword(password);
}
else
{
cerr << "Password is Empty!" << endl;
}
// Checking Email
if (email != "")
{
setEmail(email);
}
else
{
cerr << "Email is Empty!" << endl;
}
// Checking Phone
if (phone != "")
{
setPhone(phone);
}
else
{
cerr << "Phone is Empty!" << endl;
}
// Checking Address
if (address != "")
{
setAddress(address);
}
else
{
cerr << "Address is Empty!" << endl;
}
// Checking Course
this->courses.resize(courses.size());
for (int i = 0; i < courses.size(); i++)
{
if (courses[i] != nullptr)
{
this->courses[i] = new Courses(*courses[i]); // Copy constructor
}
else
{
this->courses[i] = new Courses(); // Default constructor
}
}
// The schedule is combination of the courses schedule sorted by time
totalmembers++;
// events
// Birth Date
setBirthDate(day, month, year);
}
string (*Members::getSchedule())[2]
{
return schedule;
};
bool Members::fixschedule() {
// check if there is no overlapping
};
void Members::setSchedule(int a, int b, string newSchedule)
{
schedule[a][b] = newSchedule;
};
// Teachers Class -------------------------------------------------------------------
Teachers::Teachers() : Members{}
{
totalteachers++;
for (int i = 1; i < 12; i++)
{
groups[i] = false;
}
for (int i = 0; i < 12; i++)
{
type[i] = 0;
}
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
schedule[i][j] = "";
}
}
// cout << "Teacher " << name << " with ID " << id << " has been added." << endl;
};
Teachers::Teachers(string name, string password, string email, string phone, string address, vector<Courses *> course, bool groups[12], int day, int month, int year, int type[12])
: Members{name, password, email, phone, address, course, day, month, year}
{
totalteachers++;
setId(ENSIA.getTeachers().size());
ENSIA.addTeacher(this);
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
schedule[i][j] = "";
}
}
for (int i = 0; i < 12; i++)
{
setType(i, type[i]);
}
for (int i = 0; i < 12; i++)
{
this->groups[i] = groups[i];
}
// cout << "Teacher " << name << " with ID " << id << " has been added." << endl;
};
Teachers::~Teachers() {
// cout << "Teacher " << name << " with ID " << id << " has been Removed." << endl;
};
void Teachers::addCourse(Courses *course)
{
// Check if the course is already added
for (auto it = courses.begin(); it != courses.end(); ++it)
{
if (*it == course)
{
return;
}
}
if (course != nullptr)
{
courses.push_back(course);
}
};
void Teachers::removeCourse(int index)
{
for (int i = 0; i < courses.size(); i++)
{
if (courses[i]->getId() == index)
{
courses.erase(courses.begin() + i);
}
}
}
bool Teachers::fixschedule()
{
// Initialize a vector of bools to represent each minute of the day
vector<bool> timeSlots(24 * 60, false);
for (const auto &course : courses)
{
// Convert the start and end times of the courses to QTime
QTime start = QTime::fromString(QString::fromStdString(course->getSchedule()[0][0]), "hh:mm");
QTime end = QTime::fromString(QString::fromStdString(course->getSchedule()[0][1]), "hh:mm");
// Convert QTime to minutes since start of day
int startMinutes = start.hour() * 60 + start.minute();
int endMinutes = end.hour() * 60 + end.minute();
// Check each minute in the course's time slot
for (int i = startMinutes; i < endMinutes; ++i)
{
// If the time slot is already occupied, there is an overlap
if (timeSlots[i])
{
return false;
}
// Mark the time slot as occupied
timeSlots[i] = true;
}
}
return true;
}
// Getters and Setters
void Teachers::setGroups(const bool newgroups[12])
{
for (int i = 0; i < 12; i++)
{
groups[i] = newgroups[i];
}
};
bool *Teachers::getGroups()
{
return groups;
};
int *Teachers::getType()
{
return type;
}
void Teachers::setType(int index, int type)
{
if (type == 1 || type == 2 || type == 3 || type == 0)
{
this->type[index] = type;
}
else
{
this->type[index] = 1;
}
return;
}
// Students Class -------------------------------------------------------------------
Students::Students(string name, string password, string email, string phone, string address, vector<Courses *> course, int Group, int schoolYear, int section, int day, int month, int year)
: Members(name, password, email, phone, address, course, day, month, year), group(Group), schoolYear(schoolYear), section(section)
{
// const string &name, const string &password, const string &email, const string &phone, const string &address, const vector<Courses *> &courses, const Date &birthDate
totalstudents++;
setId(ENSIA.getStudents().size());
// cout << "Student " << name << " with ID " << id << " has been added." << endl;
ENSIA.addStudent(this);
}
void Students::setYear(const int schoolYear)
{
this->schoolYear = schoolYear;
}
int Students::getYear()
{
return schoolYear;
}
void Students::setSection(const int newSection)
{
section = newSection;
}
int Students::getSection()
{
return section;
}
Students::Students() : Results{}, group{0}, Members{}, schoolYear{1}, section{1}
{
totalstudents++;
setId(ENSIA.getStudents().size());
// cout << "Student " << name << " with ID " << id << " has been added." << endl;
ENSIA.addStudent(this);
}
Students::~Students()
{
for (int i = 0; i < Results.size(); i++)
{
delete Results[i];
}
}
vector<Exams *> Students::getResults()
{
return Results;
};
vector<double> Students::getResultsValue()
{
return ResultsValue;
};
void Students::setResult(int index = 0, Exams *exam = nullptr)
{
Results[index] = exam;
};
void Students::setResultvalue(int index = 0, double value = 10)
{
ResultsValue[index] = value;
};
void Students::setResultvalue(Exams *exam, double value)
{
for (int i = 0; i < Results.size(); i++)
{
if (Results[i] == exam)
{
ResultsValue[i] = value;
}
}
};
void Students::addCourse(Courses *course)
{
// scan if the course is already added
for (int i = 0; i < courses.size(); i++)
{
if (courses[i] == course)
{
return;
}
}
courses.push_back(course);
};
void Students::removeCourse(Courses *course)
{
for (int i = 0; i < courses.size(); i++)
{
if (courses[i] == course)
{
courses.erase(courses.begin() + i);
}
}
};
string *Members::getAttendance()
{
return Attendance;
};
int Students::getGroup()
{
return group;
};
void Students::setGroup(const int newGroup)
{
group = newGroup;
};
void Students::pushExam(Exams *exam)
{
Results.push_back(exam);
}
void Students::removeExam(Exams *exam)
{
for (int i = 0; i < Results.size(); i++)
{
if (Results[i] == nullptr)
{
continue;
}
if (Results[i]->getid() == exam->getid())
{
Results.erase(Results.begin() + i);
ResultsValue.erase(ResultsValue.begin() + i);
}
}
}
bool Students::fixschedule()
{
unordered_map<int, bool> timeSlots;
// Iterate over all courses
for (const auto &course : courses)
{
// Convert the start and end times of the courses to QTime
QTime start = QTime::fromString(QString::fromStdString(course->getSchedule()[0][0]), "hh:mm");
QTime end = QTime::fromString(QString::fromStdString(course->getSchedule()[0][1]), "hh:mm");
// Convert QTime to minutes since start of day
int startMinutes = start.hour() * 60 + start.minute();
int endMinutes = end.hour() * 60 + end.minute();
// Check each minute in the course's time slot
for (int i = startMinutes; i < endMinutes; ++i)
{
// If the time slot is already occupied, there is an overlap
if (timeSlots[i])
{
return false;
}
// Mark the time slot as occupied
timeSlots[i] = true;
}
}
return true;
}
void Students::pushExamResult(double value)
{
ResultsValue.push_back(value);
}
// -------------------------------------------- School - Class --------------------------------------------
// Constructors and Destructor
School::School() : teachers{}, courses{}, students{}, exams{} {
// cout << "School has been created." << endl;
};
School::School(const vector<Students *> &students, const vector<Teachers *> &teachers, const vector<Courses *> &courses, const vector<Exams *> &exams)
{
this->students = students;
this->teachers = teachers;
this->courses = courses;
this->exams = exams;
// cout << "School has been created." << endl;
}
School::~School()
{
for (auto student : students)
{
delete student;
}
students.clear();
for (auto teacher : teachers)
{
delete teacher;
}
teachers.clear();
for (auto course : courses)
{
delete course;
}
courses.clear();
for (auto exam : exams)
{
delete exam;
}
// cout << "School has been removed." << endl;
}
void School::setCourses(const vector<Courses *> &courses)
{
this->courses = courses;
}
void School::setStudents(const vector<Students *> &students)
{
this->students = students;
}
void School::setTeachers(const vector<Teachers *> &teachers)
{
this->teachers = teachers;
}
void School::setExams(const vector<Exams *> &exams)
{
this->exams = exams;
}
// Add Objects to the Lists
void School::pushstudent(Students *s, int group)
{
switch (group)
{
case 1:
g1.push_back(s);
break;
case 2:
g2.push_back(s);
break;
case 3:
g3.push_back(s);
break;
case 4:
g4.push_back(s);
break;
case 5:
g5.push_back(s);
break;
case 6:
g6.push_back(s);
break;
case 7:
g7.push_back(s);
break;
case 8:
g8.push_back(s);