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

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme-ds-matos committed Nov 2, 2023
1 parent 0dc143e commit 6c8aaaf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
10 changes: 9 additions & 1 deletion src/CSVClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "CSVClasses.hpp"
#include "Lesson.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
Expand All @@ -27,7 +28,14 @@ CSVClasses::CSVClasses(const std::string& csv) {
std::stringstream s(contents);
std::string line;
this->entries = std::vector<Lesson>();
getline(s, line, '\n');
if (!getline(s, line, '\n')) {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSES" << std::endl;
std::exit(1);
}
if (line != "ClassCode,UcCode,Weekday,StartHour,Duration,Type") {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSES" << std::endl;
std::exit(1);
}
line.clear();
while (std::getline(s, line, '\n')) {
this->entries.push_back(Lesson(line));
Expand Down
78 changes: 43 additions & 35 deletions src/CSVClassesPerUC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,81 @@
*/
#include "CSVClassesPerUC.hpp"
#include "ClassesPerUC.hpp"
#include <sstream>
#include <string>
#include <algorithm>
#include <fstream>

#include <sstream>
#include <string>

/**
* @brief This constructor receives a string containing all the lines of a csv file and creates the AppClassPerUC from it.
* @brief This constructor receives a string containing all the lines of a csv
* file and creates the AppClassPerUC from it.
* @details The cap parameter is the capacity of each class (30 by default).
* Theoretical complexity: O(n), where n is the number of lines in the csv file.
* @param csv
* @param cap
*/
CSVClassPerUC::CSVClassPerUC(const std::string& csv, uint8_t cap) {
this->cap = cap;
CSVClassPerUC::CSVClassPerUC(const std::string &csv, uint8_t cap) {
this->cap = cap;

// CSV file into memory
std::ifstream file = std::ifstream(csv);
std::string contents;
std::ostringstream sstr;
sstr << file.rdbuf();
contents = sstr.str();
// CSV file into memory
std::ifstream file = std::ifstream(csv);
std::string contents;
std::ostringstream sstr;
sstr << file.rdbuf();
contents = sstr.str();

// Parse string
std::stringstream s(contents);
std::string line;
this->entries = std::vector<ClassPerUC>();
getline(s, line, '\n');
line.clear();
while (std::getline(s, line, '\n')) {
this->entries.push_back(ClassPerUC(line));
}
}
// Parse string
std::stringstream s(contents);
std::string line;
this->entries = std::vector<ClassPerUC>();
if (!getline(s, line, '\n')) {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSESPERUC" << std::endl;
std::exit(1);
}
if (line != "UcCode,ClassCode") {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSESPERUC" << std::endl;
std::exit(1);
}

CSVClassPerUC::CSVClassPerUC() {
this->cap = 30;
line.clear();
while (std::getline(s, line, '\n')) {
this->entries.push_back(ClassPerUC(line));
}
}

CSVClassPerUC::CSVClassPerUC() { this->cap = 30; }

/**
* @brief This method prints the csv file.
*/
void CSVClassPerUC::display() {
std::cout << "UcCode" << ',' << "ClassCode" << '\n';
for (auto e : this->entries) {
e.display();
}
std::cout << "UcCode" << ',' << "ClassCode" << '\n';
for (auto e : this->entries) {
e.display();
}
}

/**
* @brief Sort the entries vector.
* @details Order (from most important to least important): uc_code > class_code.
* Theoretical complexity: O(n log n), where n is the number of entries in the vector.
* @details Order (from most important to least important): uc_code >
* class_code. Theoretical complexity: O(n log n), where n is the number of
* entries in the vector.
*/
void CSVClassPerUC::sort() {
std::sort(this->entries.begin(), this->entries.end(), [](const ClassPerUC &first, const ClassPerUC &second) {
return first.get_id() < second.get_id();
});
std::sort(this->entries.begin(), this->entries.end(),
[](const ClassPerUC &first, const ClassPerUC &second) {
return first.get_id() < second.get_id();
});
}

/**
* @brief Getter for the vector of ClassPerUC.
* @return Pointer to entries.
*/
std::vector<ClassPerUC> *CSVClassPerUC::get_classes() {return &this->entries;}
std::vector<ClassPerUC> *CSVClassPerUC::get_classes() { return &this->entries; }

/**
* @brief Getter for the capacity of every class.
* @return cap
*/
uint8_t CSVClassPerUC::get_cap() const {return cap;}
uint8_t CSVClassPerUC::get_cap() const { return cap; }
9 changes: 8 additions & 1 deletion src/CSVStudentsClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ CSVStudentsClasses::CSVStudentsClasses(const std::string &csv) {
std::stringstream s(contents);
std::string line;
this->entries = std::vector<StudentsClasses>();
getline(s, line, '\n');
if (!getline(s, line, '\n')) {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVSTUDENTCLASSES" << std::endl;
std::exit(1);
}
if (line != "StudentCode,StudentName,UcCode,ClassCode") {
std::cerr << "ERROR: CRITICAL: INVALID FILE CSVSTUDENTCLASSES" << std::endl;
std::exit(1);
}
line.clear();
while (std::getline(s, line, '\n')) {
this->entries.push_back(StudentsClasses(line));
Expand Down

0 comments on commit 6c8aaaf

Please sign in to comment.