-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.cpp
More file actions
155 lines (140 loc) · 3.77 KB
/
student.cpp
File metadata and controls
155 lines (140 loc) · 3.77 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
#include "Student.h"
#include "database.h"
#include "CSVFile.h"
#include"Course.h"
Student::Student(int userID) : User(*Database::GetUserByID(userID))
{
/*auto* user = Database::GetUserByID(userID);
User::Name = user->Name;
User::Username = user->Username;
User::Password = user->Password;
User::ID = user->ID;
User::Role = user->Role;*/
}
Student::Student(string name, string username, string password, int academicyear, vector<string> finished,
vector<string> inprogress) : User(name,username,password,0)
{
Academicyear = academicyear;
FinishedCourses = finished;
CoursesInProgress = inprogress;
Database::Students.push_back(this);
Database::Save();
}
void Student::MoveCourseToFinished(string courseID)
{
if (!HaveCourseInProgress(courseID))
return;
FinishedCourses.push_back(courseID);
int index=0;
for (auto course : CoursesInProgress)
{
if (course == courseID)
break;
index++;
}
CoursesInProgress.erase(CoursesInProgress.begin() + index);
}
bool Student::CanTakeCourse(Course* c)
{
if (HaveFinishedCourse(c->Code) || HaveCourseInProgress(c->Code))
return false;
if (c->GetStudents().size() > c->MaxNumOfStudents)
return false;
for (int i = 0; i < c->PreRequiredCourses.size(); i++)
{
string courseid = c->PreRequiredCourses[i];
if (!HaveFinishedCourse(courseid))
return false;
if (HaveCourseInProgress(courseid))
return false;
}
return true;
}
bool Student::HaveCourseInProgress(string courseid)
{
for (auto course : CoursesInProgress)
{
if(course == courseid)
return true;
}
return false;
}
bool Student::HaveFinishedCourse(string courseid)
{
for (int i = 0; i < FinishedCourses.size(); i++)
{
if(FinishedCourses[i] == courseid)
return true;
}
return false;
}
vector<Course*> Student::GetFinishedCourses()
{
vector<Course*> result;
for (auto CID : FinishedCourses)
{
auto c = Database::GetCourse(CID);
if (c != nullptr)
result.push_back(c);
}
return result;
}
vector<Course*> Student::GetCoursesInProgress()
{
vector<Course*> result;
for (auto CID : CoursesInProgress)
{
auto c = Database::GetCourse(CID);
if (c != nullptr)
result.push_back(c);
}
return result;
}
vector<Student*> Student::LoadStudents()
{
vector<Student*> result;
CSVFile StudFile("Students.csv");
auto lines = StudFile.Load();
for (auto line : lines)
{
vector<string> parsedLine = CSVFile::ParseLine(line);
Student* std = new Student(stoi(parsedLine[0]));
std->Academicyear = stoi(parsedLine[1]);
auto numFinished = stoi(parsedLine[2]);
auto numProgrs = stoi(parsedLine[3]);
for(int i=4 ; i<= numFinished +3 ;i++)
std->FinishedCourses.push_back(parsedLine[i]) ;
for(int i = 4 + numFinished; i < 4 + numFinished + numProgrs;i++)
std->CoursesInProgress.push_back(parsedLine[i]);
result.push_back(std);
}
return result;
}
vector<string> Student::GetStudentLines()
{
vector<string> result;
for (int i = 0; i < Database::Students.size(); i++)
{
Student* student = Database::Students[i];
User* user =Database::GetUserByID(student->ID);
user->Name = student->Name;
user->Username = student->Username;
user->Password = student->Password;
user->Role = student->Role;
string s = to_string(student->ID) + "," + to_string(student->Academicyear) + "," +
to_string(student->FinishedCourses.size()) + "," + to_string(student->CoursesInProgress.size());
for (int i = 0; i < student->FinishedCourses.size(); i++)
s += ","+student->FinishedCourses[i];
for (int i = 0; i < student->CoursesInProgress.size(); i++)
s +=","+ student->CoursesInProgress[i];
result.push_back(s);
}
//ex:id,academicyear,number of finished courses,number of courses in progress,coursefinished1,coursefinished2,coursefinished3,....,courseinprogress1,courseinprogress2,....
return result;
}
Student::Student()
{
}
Student::~Student()
{
}