Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Listing for Student, Class and UC #37

Merged
merged 11 commits into from
Nov 2, 2023
170 changes: 16 additions & 154 deletions src/CSVClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @file CSVClasses.cpp
*/
#include "CSVClasses.hpp"
#include "Utils.hpp"
#include "Lesson.hpp"
#include <algorithm>
#include <iostream>
Expand All @@ -27,176 +26,39 @@ CSVClasses::CSVClasses(const std::string& csv) {
std::string line;
this->entries = std::vector<Lesson>();
getline(s, line, '\n');
std::vector<std::string> bufs;
parse_csv_line(line, bufs);
this->uc_cath_name = bufs[0];
this->class_cath_name = bufs[1];
this->weekday_cath_name = bufs[2];
this->start_hour_cath_name = bufs[3];
this->duration_cath_name = bufs[4];
this->type_cath_name = bufs[5];
line.clear();
while (std::getline(s, line, '\n')) {
this->entries.push_back(Lesson(line));
}
}

/**
* @brief Erases the contents of classes.csv and saves there the updated values.
*/
CSVClasses::~CSVClasses() {
std::ofstream ofs;
ofs.open("../schedule/classes.csv", std::ofstream::out | std::ofstream::trunc);
ofs << class_cath_name << ',' << uc_cath_name << ',' << weekday_cath_name << ',' << start_hour_cath_name
<< ',' << duration_cath_name << ',' << type_cath_name << '\n';
for (Lesson entry: entries) {
std::string value;
entry.class_to_str(value);
ofs << value << ',';
entry.uc_to_str(value);
ofs << value << ',';
entry.day_to_str(value);
ofs << value << ',';
ofs << std::setprecision(3) << entry.get_start_hour() << ',';
ofs << std::setprecision(2) << entry.get_duration() << ',';
entry.type_to_str(value);
ofs << value << ',' << '\n';
}
ofs.close();
}

/**
* @brief Displays the contents of the class.
*/
void CSVClasses::display() {
std::cout << this->uc_cath_name << ',' << this->class_cath_name << ','
<< this->weekday_cath_name << ',' << this->start_hour_cath_name
<< ',' << this->duration_cath_name << ',' << this->type_cath_name
<< '\n';
std::cout << "ClassCode" << ',' << "UcCode" << ',' << "WeekDay" << ',' << "StartHour"
<< ',' << "Duration" << ',' << "Type" << '\n';
for (auto e : this->entries) {
e.display();
}
}

/**
* @brief Sorts the entries by the category passed as parameter.
* @details Available categories: id, ClassCode, UcCode, Weekday, StartHour, Duration, Type.
* Theoretical Complexity: O(n log n), n being the number of lines in the csv file.
* @param category
*/
void CSVClasses::sort_by(std::string category) {
if (category == uc_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
std::string first_uc, second_uc;
first.uc_to_str(first_uc);
second.uc_to_str(second_uc);
return first_uc < second_uc;
});
} else if (category == class_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_class_code() < second.get_class_code();
});
} else if (category == weekday_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_day() < second.get_day();
});
} else if (category == start_hour_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_start_hour() < second.get_start_hour();
});
} else if (category == duration_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_duration() < second.get_duration();
});
} else if (category == type_cath_name) {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_type() < second.get_type();
});
} else if (category == "id") {
std::stable_sort(this->entries.begin(), this->entries.end(),
[](const Lesson &first, const Lesson &second) {
return first.get_id() < second.get_id();
});
} else {
std::cerr << "Error: invalid category" << '\n';
std::exit(1);
}
}

/**
* @deprecated
* @brief Search the lines for the first class with the given uc_code.
* @param uc_code
* @return Iterator to the first class with the given uc_code. If not found, returns a past-the-end pointer.
* @brief Sorts the entries.
* @details Theoretical Complexity: O(n log n), n being the number of lines in the csv file.
*/
std::vector<Lesson>::iterator CSVClasses::search_by_uc(
uint16_t
uc_code) { // Sorts the entries by UC and returns the iterator of the
// first found. If not found, returns a past-the-end pointer
sort_by(uc_cath_name);
std::vector<Lesson>::iterator ret = entries.end();
size_t mid = entries.size() / 2;
while (true) { // Binary search
if (mid == entries.size()) {
return ret;
} else if (entries[mid].get_uc_code() == uc_code) {
ret = entries.begin() + mid;
break;
} else if (entries[mid].get_uc_code() > uc_code) {
mid = mid / 2;
} else {
mid = mid + mid / 2;
}
}
// ret is an iterator to a class that has the desired uc_code. Let's now find
// the first one in entries.
while (true) {
if ((ret - 1)->get_uc_code() != uc_code) {
return ret;
} else
--ret;
}
return ret;
}

/**
* @deprecated
* @brief Search the lines for the first class with the given class_code.
* @param uc_code
* @param class_code
* @return Iterator to the first class with the given class_code. If not found, returns a past-the-end pointer.
*/
std::vector<Lesson>::iterator CSVClasses::search_by_class(uint16_t uc_code, uint16_t class_code) {
sort_by(class_cath_name);
std::vector<Lesson>::iterator ret = entries.end();
size_t mid = entries.size() / 2;
while (true) { // Binary search
if (mid == entries.size()) {
return ret;
} else if (entries[mid].get_uc_code() == uc_code && entries[mid].get_class_code() == class_code) {
ret = entries.begin() + mid;
break;
} else if (entries[mid].get_class_code() > class_code) {
mid = mid / 2;
} else {
mid = mid + mid / 2;
}
}
// ret is an iterator to a class that has the desired class_code. Let's now
// find the first one in entries.
while (true) {
if ((ret - 1)->get_class_code() != class_code) {
return ret;
} else
--ret;
}
return ret;
void CSVClasses::sort() {
std::sort(this->entries.begin(), this->entries.end(), [](const Lesson &a, const Lesson &b) {
if(a.get_id() == b.get_id()) {
if (a.get_day() == b.get_day()) {
if (a.get_start_hour() == b.get_start_hour()) {
if (a.get_duration() == b.get_duration()) {
return a.get_type() < b.get_type();
} else return a.get_duration() < b.get_duration();
} else return a.get_start_hour() < b.get_start_hour();
} else return a.get_day() < b.get_day();
} else return a.get_id() < b.get_id();
;});
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/CSVClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@
*/
class CSVClasses {
private:
std::string class_cath_name;
std::string uc_cath_name;
std::string weekday_cath_name;
std::string start_hour_cath_name;
std::string duration_cath_name;
std::string type_cath_name;
/// Vector with every line of the file
std::vector<Lesson> entries;

public:
CSVClasses(const std::string& csv);
~CSVClasses();
std::vector<Lesson>* get_lessons();
void display();
void sort_by(std::string category);
std::vector<Lesson>::iterator search_by_uc(uint16_t uc_code);
std::vector<Lesson>::iterator search_by_class(uint16_t uc_code, uint16_t class_code);
void sort();
};


Expand Down
6 changes: 4 additions & 2 deletions src/CSVStudentsClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ class CSVStudentsClasses {
// Constructor
CSVStudentsClasses(const std::string& csv);

//Destructor
// Destructor
virtual ~CSVStudentsClasses();

// Getter
std::vector<StudentsClasses>* get_students();

// Methods
void sort_by(const std::string& category);
std::vector<StudentsClasses>::iterator search_by_student(uint32_t student_code);
std::vector<StudentsClasses>::iterator search_by_uc(uint16_t uc_code);
std::vector<StudentsClasses>::iterator search_by_class(uint16_t class_code);
std::vector<StudentsClasses>* get_students();

// Debug
void display() const;
Expand Down
20 changes: 18 additions & 2 deletions src/ClassSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <cstdio>
#include <vector>
#include <algorithm>

/**
* @brief Constructor of the class.
Expand Down Expand Up @@ -70,8 +71,23 @@ uint64_t ClassSchedule::get_student_count() const {
* @brief Getter for class_schedule.
* @return class_schedule
*/
std::vector<Lesson*>* ClassSchedule::get_class_schedule() {
return &this->classes;
const std::vector<Lesson*> & ClassSchedule::get_class_schedule() {
return this->classes;
}

void ClassSchedule::sort() {
std::sort(this->classes.begin(), this->classes.end(), [](Lesson *a, Lesson *b) {
if (a->get_day() == b->get_day()) {
if (a->get_start_hour() == b->get_start_hour()) {
if (a->get_duration() == b->get_duration()) {
return a->get_type() < b->get_type();
}
else return a->get_duration() < b->get_duration();
}
else return a->get_start_hour() < b->get_start_hour();
}
else return a->get_day() < b->get_day();
});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/ClassSchedule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ClassSchedule : public ClassPerUC {
void add_student();
void remove_student();
uint64_t get_student_count() const;
std::vector<Lesson*>* get_class_schedule();
const std::vector<Lesson*> & get_class_schedule();
void sort();
void display() const;
};

Expand Down
1 change: 1 addition & 0 deletions src/Lesson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum class WeekDay {
THURSDAY,
FRIDAY,
SATURDAY,
NONE,
};

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ enum class TypeOfRequest {
Add,
Remove,
Switch,
Print,
Print_Student,
Print_Student_Count,
Print_UC,
Print_Class,
Print_Student_List,
Batch,
};

Expand All @@ -15,6 +19,7 @@ class Process {
private:
TypeOfRequest type;
std::vector<std::string> operands;

public:
Process(TypeOfRequest t);
void add_operand(std::string s);
Expand Down
Loading