-
Notifications
You must be signed in to change notification settings - Fork 2
/
GradePage.cpp
180 lines (159 loc) · 3.94 KB
/
GradePage.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
#include "GradePage.h"
/**
* Constructor
*/
GradePage::GradePage(string& html) : Page(html)
{
cout << "bulding GradePage..." << endl;
genList();
rowSize = COURSE_FIELDS;
colSize = courses.size();
//cleaning memory - no need for vectors anymore
lines.~vector();
courseArgs.~vector();
cout << "finished building the GradePage" << endl;
}
/**
* DeConstructor
*/
GradePage::~GradePage()
{
cout << "killing GradePage..." <<endl;
courses.~list();
}
/**
* Returns the linked list of courses
*/
list<Course*> GradePage::getList()
{
return this->courses;
}
/**
* this method is the only way to get a new obj instance of this class.
* for more details see the header file.
*
* @param html - a string with the HTML code
* @return GradePage* - returns a NEW object of a GradePage
*/
GradePage* GradePage::createGradeClass(string& html)
{
try //the creation of Page.cpp can throw an exception
{
GradePage* newInstence = new GradePage(html);
return newInstence;
}catch(int err)
{
cout << "ERROR: in GradePage @ createGradeClass func. number " << err <<endl;
return nullptr;
}
}
/**
* this method will create a linked list of all the classes and there
* details that are needed.
*/
void GradePage::genList()
{
bool first = true; // a boolean to make sure we are not getting the HTML title of the menu.
tokenToLines(); //tokening the text into lines.
for(string str : lines) // and then tokening every line into args for a single course. and adding it to list.
{
tokenToCourseArgs(str, first);
}
}
/**
* Using strtok function to cut the Page.cpp text into lines and storing them into a vector.
*/
void GradePage::tokenToLines()
{
char *tok;
char* textToTok = strdup(text.c_str());
tok = strtok(textToTok, "\n");
while(tok != NULL)
{
lines.push_back(string(tok));
tok = strtok(NULL, "\n");
}
}
/**
* this method gets a line of text and cutting it into args via strtok with '\t' as a separator.
* pushing all the args into a vector, and if there are 8 args, this line actually contains a course.
* so we send it forward to linkCourse method.
* at the end, clearing the vector for the next line to come in.
*
* @param line - is the line string to chop
* @param first - a boolean to make sure not to get the title of the HTML table...
*/
void GradePage::tokenToCourseArgs(string& line, bool& first)
{
char* tok;
char* cLine = strdup(line.c_str());
tok = strtok(cLine, "\t");
while(tok != NULL)
{
courseArgs.push_back(string(tok));
tok=strtok(NULL, "\t");
}
if(courseArgs.size() == COURSE_FIELDS+1) //in this->text there is an extra "slot" containing nothing.
{ if(first)
first = false;
else
linkCourse();
}
courseArgs.clear();
}
/**
* this method will build all the variables we neet to build a Course Obj.
* build it, and link it to the courses list.
*/
void GradePage::linkCourse()
{
int serial;
string name, type, additions;
double points, hours, grade;
char* temp;
additions = courseArgs.back();
courseArgs.pop_back();
temp = strdup(courseArgs.back().c_str());
grade = atof(temp);
courseArgs.pop_back();
temp = strdup(courseArgs.back().c_str());
hours = atof(temp);
courseArgs.pop_back();
temp = strdup(courseArgs.back().c_str());
points = atof(temp);
courseArgs.pop_back();
type = courseArgs.back();
courseArgs.pop_back();
name = courseArgs.back();
courseArgs.pop_back();
temp = strdup(courseArgs.back().c_str());
serial = atoi(temp);
courseArgs.pop_back();
//Now to add to the linked list
Course* c = new Course(serial, name, type, points, hours, grade, additions);
courses.push_back(c);
}
/**
* Uses the printCourse function in the Course class
* to print the list of courses CLI.
*/
void GradePage::printCourses()
{
for(Course* c : courses)
c->printCourse();
}
double GradePage::getAvg()
{
double avg = 0;
double points = 0;
for(Course* c : courses)
{
if(c->getGrade() != 0)
{
avg += c->getGrade() * c->getPoints();
points += c->getPoints();
}
}
avg /= points;
return avg;
}