Skip to content

Commit

Permalink
Added Course Class, modified makefile, fixing all the coredumps cause…
Browse files Browse the repository at this point in the history
…d by GradePage. tested all Page-GradePage-Course classes, all works!
  • Loading branch information
sagidayan committed Mar 23, 2014
1 parent 861f246 commit be4e330
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 98 deletions.
30 changes: 30 additions & 0 deletions Course.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "Course.h"

Course::Course(int serial, string name, string type, double points, double hours, double grade, string additions)
{
this->serialNum = serial;
this->name = name;
this->type = type;
this->points = points;
this->hours = hours;
this->grade = grade;
this->additions = additions;
}

Course::~Course()
{

}

void Course::printCourse()
{
cout << "===== Course =====" << endl;
cout << "serial: " << this->serialNum <<endl;
cout << "name: " << this->name <<endl;
cout << "type: " << this->type <<endl;
cout << "points: " << this->points <<endl;
cout << "hours: " << this->hours <<endl;
cout << "grade: " << this->grade <<endl;
cout << "additions: " << this->additions <<endl;
cout <<endl;
}
37 changes: 37 additions & 0 deletions Course.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef COURSE_H
#define COURSE_H

#include <string>
#include <iostream>

using namespace std;
/**
* A simple Course Class to contain all the Course Args,
* and printing them if needed.
*/
class Course{

public:
Course(int serial, string name, string type, double points, double hours, double grade, string additions);
~Course();
void printCourse();
int getSerialNum(){return this->serialNum;}
string getName(){return this->name;}
string getType(){return this->type;}
double getPoints(){return this->points;}
double getHours(){return this->hours;}
double getGrade(){return this->grade;}
string getAddidtions(){return this->additions;}
private:
int serialNum;
string name;
string type;
double points;
double hours;
double grade;
string additions;
};



#endif
196 changes: 124 additions & 72 deletions GradePage.cpp
Original file line number Diff line number Diff line change
@@ -1,110 +1,162 @@
#include "GradePage.h"

/**
* Constructor
*/
GradePage::GradePage(string& html) : Page(html)
{
cout << "bulding GradePage..." << endl;
genList();
rowSize = 6;
colSize = courses.size();
cout << "finished building the GradePage" << endl;
}

/**
* DeConstructor
*/
GradePage::~GradePage()
{
cout << "killing GradePage..." <<endl;
courses.~list();
lines.~vector();
courseArgs.~vector();
}

list<COURSE*> GradePage::getList()
/**
* Returns the linked list of courses
*/
list<Course*> GradePage::getList()
{
return this->courses;
}

/**
* this method is the only way to get a new obj instence 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
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;
cout << "ERROR: in GradePage @ createGradeClass func. number " << err <<endl;
} return nullptr;
}

/**
* this method will create a linked list of all the courses and there
* details that are needed.
*/
void GradePage::genList()
{
string name, type, numbers; //numbers = temp text to convert to double
double points, hours, grade;
int i=0, temp; //indexes

while(text[i] != '>')
i++;
i++;
//Actual line with content
runLine:
if(text[i] == '>')
i++;
if(text[i] == '\t' && text[i+1] == '\t')
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.
{
i++;
goto runLine;
tokenToCourseArgs(str, first);
}
i++;
//getting name of course;
temp = i+1;
temp = goToNextArg(temp);
if(!isMoreToList(temp))
return;
name = text.substr(i, temp);
//getting type of course;
i = temp+1;
temp = goToNextArg(temp);
if(!isMoreToList(temp))
return;
type = text.substr(i, temp);
//getting points
i = temp+1;
temp = goToNextArg(temp);
if(!isMoreToList(temp))
return;
numbers = text.substr(i, temp);
points = atof(numbers.c_str());
//getting hours
i = temp+1;
temp = goToNextArg(temp);
if(!isMoreToList(temp))
return;
numbers = text.substr(i, temp);
hours = atof(numbers.c_str());
//getting grade
i = temp+1;
temp = goToNextArg(temp);
if(!isMoreToList(temp))
return;
numbers = text.substr(i, temp);
grade = atof(numbers.c_str());

//adding to list!
Course c = {name, type, points, hours, grade};

courses.push_back(&c);

while(text[i] != '\n')
i++;
i++;
if(!isMoreToList(i))
return;
goto runLine;
}

/**
* 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");
}
free(textToTok); //cleaning memory
free(tok); // cleaning memory
}

int GradePage::goToNextArg(int index)
/**
* 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 actualy 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)
{
while(text[index] != '\t')
index++;
return index;
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() == 8)
{ if(first)
first = false;
else
linkCourse();
}
free(tok);
free(cLine);
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);
}

bool GradePage::isMoreToList(int index)
/**
* Uses the printCourse function in the Course class
* to print the list of courses.
*/
void GradePage::printCourses()
{
if(index > text.length()-1)
return false;
return true;
for(Course* c : courses)
c->printCourse();
}
40 changes: 26 additions & 14 deletions GradePage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,47 @@

#include "Page.h"
#include <list>
#include <vector>
#include <string.h>
#include "Course.h"

using namespace std;

typedef struct COURSE{
string name;
string type;
double points;
double hours;
double grade;
}Course;


/**
* Usage: to be able to create a GradePage obj, you will need
* to do so by using the createGradeClass method.
* ex: GradePage* gp = GradePage::createGradeClass(*recieverPage);
* that is why that methos is *static*.
* you canot use new.
*
* if something went wrong while the creation of this class, debug
* it via the output in the Terminal.
*
*
* 2014.
* sagi dayan
*/
class GradePage : public Page
{

public:
GradePage* createGradeClass(string& html);
static GradePage* createGradeClass(string& html);
~GradePage();
list<COURSE*> getList();
list<Course*> getList();
int getRows(){return rowSize;}
int getCols(){return colSize;}
void printCourses();
private:

GradePage(string& html);
void genList();
list<COURSE*> courses;
list<Course*> courses;
int rowSize, colSize;
bool isMoreToList(int index);
int goToNextArg(int index);
void tokenToLines();
void tokenToCourseArgs(string& line, bool& first);
void linkCourse();
vector<string> lines;
vector<string> courseArgs;

};

Expand Down
6 changes: 4 additions & 2 deletions jce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ void jce::makeFirstVisit()

//std::cout << pag;

Page p(*recieverPage);
std::cout << p.getString();
GradePage* gp = GradePage::createGradeClass(*recieverPage);
std::cout << gp->getList().size() << " Courses in your Grade Sheet:"<< endl;
gp->printCourses();

//makeFurtherRequests();
}
else
Expand Down
1 change: 1 addition & 0 deletions jce.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <sstream>
#include <fstream>
#include "GradePage.h"

enum jceErrors {
VALIDATION_ERROR,
Expand Down
Loading

0 comments on commit be4e330

Please sign in to comment.