-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocation.cpp
1814 lines (1501 loc) · 60.3 KB
/
allocation.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
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 "allocation.h"
#include <sstream>
#include <memory>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <type_traits>
#include <string>
#include <iterator>
#include <fstream>
#include <stdexcept>
#include <any>
#include <random>
#include <fstream>
using std::any;
using std::any_cast;
using std::cerr;
using std::cout;
using std::distance;
using std::endl;
using std::exception;
using std::find;
using std::for_each;
using std::fstream;
using std::ifstream;
using std::invalid_argument;
using std::is_base_of;
using std::istringstream;
using std::make_unique;
using std::minus;
using std::move;
using std::ofstream;
using std::ostream;
using std::pair;
using std::remove_if;
using std::stringstream;
using std::to_string;
/// ========------- Helper functions (namespace )-------========///
namespace helper
{
// helper function for checking number of files input
bool check_num_of_files_input(int argc, char *argv[], stringstream &ss)
{
// if (argc != 5 )
if (argc != 4 && argc != 5)
{
int files_count = 0;
// ignore the first argument (program name) and loop through the rest of the arguments
for (int i = 1; i < argc; i++)
{
ss << "argv[" << i << "] = " << argv[i] << " ";
files_count++;
}
cerr << "Input file: ";
(files_count == 0) ? cerr << "No files passed!" << endl : cerr << "Files passed: " << files_count << "expected total of at least 3 files!" << endl;
// throw an exception if the number of arguments is not 4 (obviously 5 including the program name)
throw invalid_argument("File is missing. Make sure you're at least passing 3 files!");
return false;
}
// yes, there are 4 files
return true;
}
// helper function for read file
bool read_file(const string &file_name, ifstream &in_file)
{
// read the file line by line
string line;
while (getline(in_file, line))
{
// create an object from the line
bool is_obj_created_and_move_to_list = ObjectFactory::create_object(file_name, line);
// check if the object is not null
if (is_obj_created_and_move_to_list != true)
{
cerr << "Error: Object is not created nor moved to ProjectAlloc list" << endl;
return false;
}
}
// yes, the file has been read
return true;
}
// read the file and create a objects and store in ProjectAlloc class
bool read_file_and_create_obj(const vector<pair<string, string>> &files)
{
try
{
// loop through the files
for (auto &file : files)
{
// ignore the allocations file
if (file.first == "allocations")
{
// return true; exit the loop
return true;
}
// get file name from the pair
string file_name = file.first;
// open the file
ifstream in_file(file.second);
// check if the file is open
bool is_open = check_file_open(in_file, file_name);
// check if the file is not empty
bool is_not_empty = check_file_empty(in_file, file_name);
// check if the file is not empty and is open
if ((is_open && is_not_empty) == true)
{
// read the file
read_file(file_name, in_file);
// return true;
}
}
}
catch (const exception &e)
{
cerr << "Error: " << e.what() << endl;
return false;
}
return false;
}
// helper function to check if the object is not null
template <typename T>
bool is_not_null(const T &obj)
{
{
// check if entity is not null
return obj != nullptr;
}
}
// function to remove obj from the list
// template <typename T>
// void remove_obj(vector<T> &obj_list, const T &obj)
// {
// // check if obj is not null
// if (is_not_null(obj))
// {
// // check if obj is in the list
// auto it = get_obj(obj_list, obj);
// if (is_not_null(it))
// {
// // remove obj from the list
// obj_list.erase(std::remove(obj_list.begin(), obj_list.end(), obj), obj_list.end());
// }
// }
// }
// function to check if obj is in the list and return the obj
template <typename T>
T get_obj(const vector<T> &obj_list, const T &obj)
{
// check if obj is not null
if (is_not_null(obj))
{
// check if obj is in the list
auto it = std::find(obj_list.begin(), obj_list.end(), obj);
if (it != obj_list.end())
{
return *it;
}
}
return nullptr;
}
// function to check if obj is in the list and return the obj
bool check_file_open(ifstream &in_file, const string &file_name)
{
// check if the file is open
if (!in_file.is_open())
{
// print the error message
cerr << "Error: " << file_name << " file is not open!" << endl;
// throw an exception if the file is not open
throw invalid_argument("File is not open!");
return false;
}
else
{
return true;
}
}
// function to check if the file is empty
bool check_file_empty(ifstream &in_file, const string &file_name)
{
// if no characters to read from the file till the end of the file, file is empty
if (in_file.peek() == ifstream::traits_type::eof())
{
// print the error message
cerr << "Error: " << file_name << " file is empty!" << endl;
// throw an exception if the file is empty
throw invalid_argument("File is empty!");
return false;
}
return true;
}
// convert the map into vector
template <typename T, typename datatype>
vector<std::pair<datatype, shared_ptr<T>>> convert_map_to_vector(const map<datatype, shared_ptr<T>> &map_list)
{
std::vector<std::pair<datatype, shared_ptr<T>>> class_name_vector(map_list.begin(), map_list.end());
return class_name_vector;
}
}
// ==--------========= Object class implementation =========--------==//
// object default constructor implementation
Object::Object(any id)
{
// set person id
obj_id_ = id;
}
// destructor implementation
Object::~Object() {}
// get person id implementation
any Object::get_obj_id() const { return obj_id_; }
// set person id implementation
void Object::set_obj_id(any id) { obj_id_ = id; }
// handle line implementation
// unique_ptr<Object> Object::handle_line(string class_type, string line)
// {
// // check type of class
// if (class_type == "students")
// {
// // let student class handle the line
// return make_unique<Student>(move(Student::handle_line(line)));
// }
// if (class_type == "supervisors")
// {
// // let supervisor class handle the line
// return Supervisor::handle_line(line);
// }
// return nullptr;
// }
// print implementation
string Object::print() const
{
return "";
}
// return the dynamic cast of the child in shared pointer
// template <typename T>
// shared_ptr<T> Object::get_dynamic_cast(const shared_ptr<Object> &obj)
// {
// // check if the object is not null
// if (obj != nullptr)
// {
// // check if the object is of type T
// if (is_base_of<T, Object>::value)
// {
// // return the dynamic cast of the object
// return dynamic_pointer_cast<T>(obj);
// }
// }
// return nullptr;
// }
// ====------ Student class implementation ------====//
// student default constructor implementation
Student::Student(string student_id, vector<int> student_project_preferences) : Object(student_id)
{
// set project preferences
this->project_preference_ids_ = student_project_preferences;
// set total number of project preferences
total_project_preference_count_ = project_preference_ids_.size();
// set project allocated to nullptr
project_allocated_ = nullptr;
}
// copy constructor implementation
Student::Student(const Student &student) : Object(student)
{
// get project preferences
vector<int> student_project_preferences = student.get_project_preference_ids();
// copy project preferences
// check if student has project preferences
if (student_project_preferences.size() > 0)
for_each(student_project_preferences.begin(), student_project_preferences.end(), [this](const int &project_id)
{ add_project_preference_id(project_id); }
);
// check if student allocated in student. copy if allocated
if (student.is_project_allocated())
{
// make duplicate of project allocated
allocate_project_to_student(make_unique<Project>(*student.get_allocated_project()));
}
else
{
// set project allocated to nullptr by resetting unique pointer
project_allocated_.reset();
}
}
// destructor implementation
Student::~Student() {}
// add project preference implementation
void Student::add_project_preference_id(int project_preferences)
{
// insert project preference
project_preference_ids_.push_back(project_preferences);
// increase total project preferences number by 1
total_project_preference_count_++;
}
// get project preferences implementation
vector<int> Student::get_project_preference_ids() const { return project_preference_ids_; }
// get total number of project preferences implementations
// int Student::get_total_project_preference_count() const { return total_project_preference_count_; }
// exceeds_project_preferences
// bool Student::has_reached_project_preference_limit() const
// {
// // only 4 project preferences allowed else return false
// return total_project_preference_count_ >= 4;
// }
// get project allocated implementation
// get duplicate of project allocated
shared_ptr<Project> Student::get_allocated_project() const
{
// make duplicate of project allocated
return make_unique<Project>(*project_allocated_);
}
// set project allocated implementation
void Student::allocate_project_to_student(shared_ptr<Project> project)
{
// set project allocated
project_allocated_ = move(project);
}
// add project to student
void Student::add_project_to_student(const int &project_id)
{
// add project to student
shared_ptr<Project> project = ProjectAlloc::get_object_by_id<Project, int>(project_id);
// add project to student
project_allocated_ = move(project);
}
// get the project allocated
shared_ptr<Project> Student::get_project_allocated() const
{
// return the project allocated
return project_allocated_;
}
// get the student allocation score
int Student::get_student_allocation_score() const
{
return student_allocation_score_;
}
// set the student allocation score
void Student::set_student_allocation_score(int score)
{
student_allocation_score_ = score;
}
// check if student is allocated implementation
bool Student::is_project_allocated() const
{
// get the project allocated
// unique_ptr<Project> project_allocated = get_allocated_project();
// check if project allocated is not null
return is_project_allocated_;
}
// update the project allocation state
void Student::update_project_allocation_state(bool status)
{
// update the project allocation status
is_project_allocated_ = status;
}
// get the supervisor who supervises the project allocated to the student
// shared_ptr<Supervisor> Student::get_student_supervisor() const
// {
// // get the supervisor of the project allocated
// return this->supervisor_;
// }
// set the supervisor who supervises the project allocated to the student
// void Student::set_supervisor_to_student(const any &supervisor_id)
// {
// // search for the supervisorr by id
// shared_ptr<Supervisor> supervisor = ProjectAlloc::get_object_by_id<Supervisor, string>(std::any_cast<string>(supervisor_id));
// // set the supervisor of the project allocated
// this->supervisor_ = move(supervisor);
// }
void Student::set_supervisor_to_student(const any &supervisor_id)
{
// search for the supervisorr by id
shared_ptr<Supervisor> supervisor = ProjectAlloc::get_object_by_id<Supervisor, string>(std::any_cast<string>(supervisor_id));
// set the supervisor of the project allocated
this->supervisor_ = move(supervisor);
}
// print implementation
string Student::print() const
{
// get student preferences
vector<int> student_project_preferences = get_project_preference_ids();
// build string stream
stringstream ss;
// print student details
ss << "Student id: " << any_cast<string>(get_obj_id()) << endl;
// print project preferences
ss << "Project preferences: " << endl;
for_each(student_project_preferences.begin(), student_project_preferences.end(), [&ss](const int &project_id)
{ ss << project_id << endl; });
// check if student is allocated
if (is_project_allocated())
{
// print project allocated
ss << "Project allocated: " << any_cast<string>(this->get_allocated_project()->get_obj_id()) << endl;
}
else
{
// print project allocated
ss << "Project allocated: None" << endl;
}
return ss.str();
}
// << operator implementation
ostream &operator<<(ostream &os, const Student &student)
{
// print student details
os << student.print();
return os;
}
// ====------ Project class implementation ------====//
// project default constructor implementation
Project::Project(int project_id, shared_ptr<Supervisor> supervisor_id, int project_multiplicity, string project_title) : Object(project_id)
{
// set the supervisor for this project
supervisor_ = move(supervisor_id);
// also add this project id to the supervisor's proposed projects
// set project title
project_title_ = project_title;
// set project multiplicity
project_multiplicity_ = project_multiplicity;
// set project allocated to false
available_space_in_project_ = project_multiplicity;
}
// copy constructor implementation
Project::Project(const Project &project) : Object(project)
{
// get project details
int project_id = any_cast<int>(Object::get_obj_id());
string project_title = project.get_project_title();
int project_multiplicity = project.get_project_multiplicity();
const vector<shared_ptr<Student>> &students_allocated_to_project = project.get_students_allocated();
const shared_ptr<Supervisor> &project_supervisor = project.get_project_supervisor();
// copy the available space in project
available_space_in_project_ = project.get_available_space();
// set project details
set_obj_id(project_id);
set_project_title(project_title);
set_project_multiplicity(project_multiplicity);
set_supervisor_to_project(move(project_supervisor));
// copy students allocated
for_each(students_allocated_to_project.begin(), students_allocated_to_project.end(), [this](const shared_ptr<Student> &student)
{ alloc_student_to_proj(make_unique<Student>(*student)); });
}
// destructor implementation
Project::~Project() {}
// get project title implementation
string Project::get_project_title() const { return project_title_; }
// set project title implementation
void Project::set_project_title(string project_title) { project_title_ = project_title; }
// get multiplicity implementation
int Project::get_project_multiplicity() const { return project_multiplicity_; }
// set multiplicity implementation
void Project::set_project_multiplicity(int project_multiplicity) { project_multiplicity_ = project_multiplicity; }
// add student allocated implementation
void Project::alloc_student_to_proj(const shared_ptr<Student> &student)
{
// search the student in the list of students ProjectAlloc
// shared_ptr<Student> student = ProjectAlloc::get_object_by_id<Student, string>(student_id);
// add student allocated to project
this->num_of_students_allocated_.emplace_back(move(student));
// up date the available space in the project
}
void Project::update_available_space()
{
// minus the available space by 1
this->available_space_in_project_--;
}
// set supervisor implementation
void Project::set_supervisor_to_project(const shared_ptr<Supervisor> &allocate_supervisor)
{
// set supervisor
supervisor_ = make_unique<Supervisor>(*allocate_supervisor);
}
// get the availability of project implementation
// bool Project::is_project_available_to_allocate() const
// {
// // check if project is available
// return is_project_available_;
// }
// set the availability of project implementation
void Project::set_is_project_available_to_allocate(bool is_project_available)
{
// set project availability
is_project_available_ = is_project_available;
}
// get available space implementation
int Project::get_available_space() const
{
// making sure available space is not negative value
// return available space
return available_space_in_project_;
}
// get total number of students allocated implementation
int Project::get_total_of_students_allocated_to_project() const
{
// get student allocated
const vector<shared_ptr<Student>> &students_allocated_in_project = get_students_allocated();
int total = 0;
// check if students_allocated is not empty
(students_allocated_in_project.size() > 0) ? total = students_allocated_in_project.size() : total;
// return total number of students allocated
return total;
}
// get students allocated implementation
const vector<shared_ptr<Student>> &Project::get_students_allocated() const
{
// return list of students allocated
return num_of_students_allocated_;
}
// get supervisor implementation
const shared_ptr<Supervisor> &Project::get_project_supervisor() const
{
// make new unique pointer of supervisor and return it
return supervisor_;
}
// is project full implementation
bool Project::is_project_alloc_full() const
{
// get this project multiplicity
int multiplicity = get_project_multiplicity(); // 3
// get the total number of students allocated
int total_students_allocated = get_total_of_students_allocated_to_project();
bool is_full = false;
// check if project is full
(total_students_allocated >= multiplicity) ? is_full = true : is_full;
return is_full;
}
// is project allocated implementation
bool Project::has_project_allocated_to_supervisor() const
{
// get supervisor
const shared_ptr<Supervisor> &project_supervisor = get_project_supervisor();
// check if supervisor is not null
return helper::is_not_null(project_supervisor);
}
// print project details implementation
string Project::print() const
{
// print project details
stringstream ss;
// print project details
ss << "Project id: " << any_cast<int>(get_obj_id()) << endl;
ss << "Multiplicity: " << project_multiplicity_ << endl;
ss << "Available space: " << get_available_space() << endl;
// check if project is full
if (is_project_alloc_full())
{
ss << "Project is full" << endl;
}
else
{
ss << "Project is not full" << endl;
}
// check if project is allocated
if (has_project_allocated_to_supervisor())
{
ss << "Project is allocated to supervsor" << endl;
}
else
{
ss << "Project is not allocated" << endl;
}
// print supervisor details
ss << "Supervisor: " << any_cast<string>(supervisor_.get()->get_obj_id()) << endl;
// print students allocated
ss << "Students allocated: " << endl;
for_each(num_of_students_allocated_.begin(), num_of_students_allocated_.end(), [&ss](const shared_ptr<Student> &student)
{ ss << "\t" << any_cast<string>(student->get_obj_id()) << endl; });
return ss.str();
}
// overload stream operator implementation
ostream &operator<<(ostream &os, const Project &project)
{
os << project.print();
return os;
}
// ====------ Supervisor class implementation ------====//
// supervisor default constructor implementation
Supervisor::Supervisor(string id, int supervision_load) : Object(id)
{
// set supervisor details
supervision_load_ = supervision_load;
}
// copy constructor implementation
Supervisor::Supervisor(const Supervisor &supervisor) : Object(supervisor)
{
// get the supervisor id and cast it to string as it is stored as any in base class
string id = any_cast<string>(supervisor.get_obj_id());
// get supervision load
int supervision_load = supervisor.get_supervision_load();
// get proposed projects
const vector<shared_ptr<Project>> &projects_proposed = supervisor.get_projects_proposed();
// get allocated projects
const vector<shared_ptr<Project>> &projects_supervised = supervisor.get_projects_supervised();
// set the supervisor_id to new supervisor
this->set_obj_id(id);
// set the supervision load to new supervisor
set_supervision_load(supervision_load);
// copy projects proposed
// check if projects_proposed is not empty
if (projects_proposed.size() > 0)
for_each(projects_proposed.begin(), projects_proposed.end(), [this](const shared_ptr<Project> &project)
{ add_project_proposed(std::make_shared<Project>(*project)); });
// copy projects supervised
// for_each(projects_supervised.begin(), projects_supervised.end(), [this](const unique_ptr<Project> &project)
// { set_supervisor_to_project(std::make_shared<Project>(*project)); });
// get students supervised
const vector<shared_ptr<Student>> &students_supervised = supervisor.get_students_supervised();
// copy students supervised
// check if students_supervised is not empty
if (students_supervised.size() > 0)
for_each(students_supervised.begin(), students_supervised.end(), [this](const shared_ptr<Student> &student)
{ add_student_supervised(std::make_shared<Student>(*student)); });
}
// destructor implementation
Supervisor::~Supervisor() {}
// add student supervised implementation
void Supervisor::add_student_supervised(shared_ptr<Student> student_supervised)
{
// add student supervised
students_supervised_.push_back(move(student_supervised));
}
// add project to supervise implementation
void Supervisor::add_project_to_supervised(const int &project_id)
{
// add project to supervise
shared_ptr<Project> project = ProjectAlloc::get_object_by_id<Project, int>(project_id);
projects_proposed_.push_back(move(project));
// update project supervised count
active_num_of_students_supervised_++;
}
// get supervision load implementation
int Supervisor::get_supervision_load() const { return this->supervision_load_; }
// set supervision load implementation
void Supervisor::set_supervision_load(int supervision_load) { this->supervision_load_ = supervision_load; }
// get the project supervised count implementation
int Supervisor::get_current_supervised_student_count() const
{
return this->active_num_of_students_supervised_;
}
// set the project supervised count implementation
// void Supervisor::set_project_supervised_count(int project_supervised_count)
// {
// this->active_num_of_students_supervised_ = project_supervised_count;
// }
// get supervisor available implementation
// bool Supervisor::is_supervisor_available() const { return this->is_supervisor_available_to_alloc_; }
// update supervisor availability implementation
void Supervisor::set_is_supervisor_availability(bool is_available)
{
this->is_supervisor_available_to_alloc_ = is_available;
}
// get the supervisor with modifiable access implementation get_projects_supervised_modifiable
// vector<shared_ptr<Project>> &Supervisor::get_projects_supervised_modifiable()
// {
// // return list of projects supervised
// return projects_supervised_;
// }
// get proposed projects implementation
const vector<shared_ptr<Project>> &Supervisor::get_projects_proposed() const
{
// return list of projects proposed
return projects_proposed_;
}
// get project supervised implementation
const vector<shared_ptr<Project>> &Supervisor::get_projects_supervised() const
{
// return list of projects supervised
return projects_supervised_;
}
// get total number of projects supervised implementation
int Supervisor::get_total_numb_of_projects_supervised() const
{
// get projects supervised
const vector<shared_ptr<Project>> &projects_supervised = get_projects_supervised();
// return total number of projects supervised
return projects_supervised.size();
}
void Supervisor::add_project_proposed(shared_ptr<Project> project)
{
}
// {
// // check if project is not null
// if (helper::is_not_null(project))
// {
// // add project to projects proposed
// projects_proposed_.push_back(move(project));
// }
// }
// get available space for projest supervised implementation
int Supervisor::get_available_space() const
{
// get supervision load
int supervision_load = get_supervision_load();
// get total number of projects supervised
int total_numb_of_projects_supervised = get_total_numb_of_projects_supervised();
// return available space for projects supervised
return supervision_load - total_numb_of_projects_supervised;
}
// get is overloaded implementation
bool Supervisor::is_supervisor_overloaded() const
{
// get available space
int available_space = get_available_space();
// check if available space is less than 0
if (available_space < 0)
{
// return true
return true;
}
// return false
return false;
}
// print implementation
string Supervisor::print() const
{
// get supervisor details
string id = any_cast<string>(get_obj_id());
int supervision_load = get_supervision_load();
// get projects proposed
const vector<shared_ptr<Project>> &projects_proposed = get_projects_proposed();
// get projects supervised
const vector<shared_ptr<Project>> &projects_supervised = get_projects_supervised();
// get students supervised
const vector<shared_ptr<Student>> &students_supervised = get_students_supervised();
// create string stream
stringstream ss;
// add supervisor details to string stream
ss << "Supervisor ID: " << id << endl;
ss << "Supervision Load: " << supervision_load << endl;
// add projects proposed to string stream
// ss << "Projects Proposed: " << endl;
// for_each(projects_proposed.begin(), projects_proposed.end(), [&ss](const unique_ptr<Project> &project)
// { ss << *project << endl; });
// // add projects supervised to string stream
// ss << "Projects Supervised: " << endl;
// for_each(projects_supervised.begin(), projects_supervised.end(), [&ss](const shared_ptr<Project> &project)
// { ss << *project << endl; });
// // add students supervised to string stream
// ss << "Students Supervised: " << endl;
// for_each(students_supervised.begin(), students_supervised.end(), [&ss](const shared_ptr<Student> &student)
// { ss << *student << endl; });
// return string stream
return ss.str();
}
// {
// // check if student is not null
// if (helper::is_not_null(student))
// {
// // add student to students supervised
// students_supervised_.push_back(move(student));
// }
// }
// get students supervised implementation
const vector<shared_ptr<Student>> &Supervisor::get_students_supervised() const
{
// return list of students supervised
return students_supervised_;
}
// Supervisor &Supervisor::operator=(Supervisor supervisor)
// {
// // Swap the contents of this object with the argument
// std::swap(obj_id_, supervisor.obj_id_);
// std::swap(supervision_load_, supervisor.supervision_load_);
// std::swap(projects_proposed_, supervisor.projects_proposed_);
// // Return a reference to this object
// return *this;
// }
// operator overloader implementation
ostream &operator<<(ostream &os, const Supervisor &supervisor)
{
os << supervisor.print();
return os;
}
// comparing the supervisor by supervisor load in descending order
bool operator<(const Supervisor &supervisor1, const Supervisor &supervisor2)
{
// get supervision load
return supervisor1.get_supervision_load() < supervisor2.get_supervision_load();
}
// comparing the supervisor by supervisor id in ascending order
bool operator>(const Supervisor &supervisor1, const Supervisor &supervisor2)
{
// get supervisor id
string supervisor_id1 = any_cast<string>(supervisor1.get_obj_id());
string supervisor_id2 = any_cast<string>(supervisor2.get_obj_id());
return supervisor_id1 > supervisor_id2;
}
// =====-------------- ProjectAlloc class ------------=====
// // constructor implementation
ProjectAlloc &ProjectAlloc::get_instance()
{
// create instance of ProjectAlloc
return project_allocation_system_;
}
// this declaration is required to avoid linker error
ProjectAlloc ProjectAlloc::project_allocation_system_;
map<string, shared_ptr<Supervisor>> ProjectAlloc::supervisors_;
map<string, shared_ptr<Student>> ProjectAlloc::students_;
map<int, shared_ptr<Project>> ProjectAlloc::projects_;
// destructor implementation
ProjectAlloc::~ProjectAlloc()
{
}
template <typename T>
void ProjectAlloc::add_object_to_map(const std::variant<std::string, int> &id, std::shared_ptr<T> obj)
{
// Check if the object is a Supervisor
if constexpr (std::is_same_v<T, Supervisor>)
{
// checking the type of id
// Make sure the ID is a string
if (auto id_string = std::get_if<std::string>(&id))
{
// Add the supervisor to the map of supervisors
supervisors_.emplace(*id_string, std::move(obj));
}
else
{
// Handle error, id is not a string
throw std::invalid_argument("Invalid argument: expected string ID for Supervisor");
}
}
// Check if the object is a Student
else if constexpr (std::is_same_v<T, Student>)
{
// Make sure the ID is a string
if (auto id_string = std::get_if<std::string>(&id))
{