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

Commit

Permalink
Merge pull request #42 from jvdcf/verification_processes
Browse files Browse the repository at this point in the history
Verification for add() and switch()
  • Loading branch information
jvdcf authored Nov 2, 2023
2 parents 1b93406 + d4ab9da commit 2191697
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 114 deletions.
14 changes: 11 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,30 @@ int main(int argc, char** argv) {

for (int i = 1; i < argc; i+=2) {
if (std::string(argv[i]) == std::string("-cpu")) {
done_cpu = std::string(argv[i+1]) != "-c" && std::string(argv[1]) != "-sc";
done_cpu = std::string(argv[i+1]) != "-c" && std::string(argv[i+1]) != "-sc";
cpu = CSVClassPerUC(argv[i+1]);
}
if (std::string(argv[i]) == std::string("-c")) {
done_c = std::string(argv[i+1]) != "-cpu" && std::string(argv[1]) != "-sc";
done_c = std::string(argv[i+1]) != "-cpu" && std::string(argv[i+1]) != "-sc";
c = CSVClasses(argv[i+1]);
}
if (std::string(argv[i]) == std::string("-sc")) {
done_sc = std::string(argv[i+1]) != "-cpu" && std::string(argv[1]) != "-c";
done_sc = std::string(argv[i+1]) != "-cpu" && std::string(argv[i+1]) != "-c";
sc = CSVStudentsClasses(argv[i+1]);
}
}

if (done_c && done_sc && done_cpu) {
Runtime rt(sc, cpu, c);
rt.run();
} else {
std::cerr << "USAGE: The program takes three files, with their specific flags prepending them. Example:\n\n"
<< " schdulEd -cpu classes_per_uc.csv -c classes.csv -sc students_classes.csv\n\n"
<< "Where '-cpu' is the flag that specifies a CSV file which contains the list of classes per each UC\n"
<< "Where '-c' is the flag that specifies a CSV file which contains the list of classes an their schedules\n"
<< "Where '-sc' is the flag that specifies a CSV file which contains the list of students and their association with each class\n"
<< std::endl;
std::exit(1);
}

return 0;
Expand Down
18 changes: 1 addition & 17 deletions src/ClassesPerUC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,7 @@ uint16_t ClassPerUC::parse_uc(std::string uc_code) {
* @return class_code as uint16_t
*/
uint16_t ClassPerUC::parse_class(std::string class_code) {
uint8_t year = class_code[0] - '0';
std::string classnum;
for (int i = 1; i < class_code.size(); ++i) {
if (isnum(class_code[i])) {
classnum.push_back(class_code[i]);
}
}
try {
uint8_t num = 0;
if (classnum != "") {
num = std::stoi(classnum);
}
return ((uint16_t)year << 8) + num;
} catch (std::invalid_argument &e) {
std::cerr << e.what() << " class: failed to parse" << '\n';
std::exit(1);
}
return parse_class_gen(class_code);
}

/**
Expand Down
117 changes: 115 additions & 2 deletions src/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <cstdlib>
#include <exception>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <ostream>
#include <queue>
#include <sstream>
Expand Down Expand Up @@ -165,9 +167,17 @@ void Runtime::run() {

void Runtime::process_args(std::vector<std::string> args) {
if (args[0] == "quit") {
char answer;
std::cout << "Do you wish to save any changes you have made? [y/N]" << std::endl;
std::cin >> std::noskipws >> answer;
if (answer == 'y') {
std::cout << "Saving..." << std::endl;
this->save_all();
this->students_classes_->write_to_file();
}
std::cout << "Quitting..." << std::endl;
// TODO: Call saving functions
std::exit(1);
std::exit(0);
}
if (args[0] == "remove") {
if (args.size() != 3) {
Expand Down Expand Up @@ -379,6 +389,109 @@ void Runtime::handle_process(Process p) {
return;
}

// handle add--------------------------------------------------------------------------------------------------------
if (p.get_type() == TypeOfRequest::Add) {
uint32_t student_code;
try {
student_code = std::stoi(ops[0]);
} catch (std::exception e) {
std::cerr << "ERROR: The string " << ops[0] << " is not a student_code."
<< std::endl;
return;
}
uint32_t id = (((uint32_t)parse_uc_gen(ops[1])) << 16) + parse_class_gen(ops[2]);
ClassSchedule* target = find_class(id);
if (target == nullptr) {
std::cerr << "ERROR: Did not find class with id: " << id << std::endl;
return;
}
if (auto itr = students.find(Student(student_code, "")); itr != students.end()) {
Student s = *itr;
OperationResult res = s.verify_add(target);
if (res == OperationResult::Success) {
students.erase(s);
s.add_to_class(target);
students.insert(s);
history.push(p);
return;
}
if (res == OperationResult::Conflicts) {
std::string answer;
std::cout << "WARNING: Conflict found, some classes overlap non critically. Do you wish to proceed adding? [y/N] ";
std::cin >> std::noskipws >> answer;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (answer == "y") {
students.erase(s);
s.add_to_class(target);
students.insert(s);
history.push(p);
}
return;
}
if (res == OperationResult::Error) {
std::cerr << "ERROR: Critical conflicts found: the schedule is not valid. Skipping." << std::endl;
return;
}
}
}

if (p.get_type() == TypeOfRequest::Switch) {
uint32_t student_code1;
uint32_t student_code2;
try {
student_code1 = std::stoi(ops[0]);
} catch (std::exception e) {
std::cerr << "ERROR: The string " << ops[0] << " is not a student_code."
<< std::endl;
return;
}
try {
student_code2 = std::stoi(ops[1]);
} catch (std::exception e) {
std::cerr << "ERROR: The string " << ops[1] << " is not a student_code."
<< std::endl;
return;
}
uint16_t uc_code = parse_uc_gen(ops[2]);
if (auto itr = students.find(Student(student_code1, "")); itr != students.end()) {
if (auto itr2 = students.find(Student(student_code2, "")); itr != students.end()) {
Student s1 = *itr;
Student s2 = *itr2;
OperationResult res = s1.verify_switch(s2, uc_code);
if (res == OperationResult::Success) {
students.erase(s1);
students.erase(s2);
s1.switch_class_with(s2, uc_code);
history.push(p);
students.insert(s1);
students.insert(s2);
return;
}
if (res == OperationResult::Conflicts) {
std::string answer;
std::cout << "WARNING: Conflict found, some classes overlap non critically. Do you wish to proceed switching? [y/N] ";
std::cin >> std::noskipws >> answer;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (answer == "y") {
students.erase(s1);
students.erase(s2);
s1.switch_class_with(s2, uc_code);
students.insert(s1);
students.insert(s2);
history.push(p);
}
return;
}
if (res == OperationResult::Error) {
std::cerr << "ERROR: Critical conflicts found: at least one schedule is not valid. Skipping." << std::endl;
return;
}
}
}
}

// handle print student-----------------------------------------------------------------------------------------------
if (p.get_type() == TypeOfRequest::Print_Student) {
uint32_t student_code;
Expand Down Expand Up @@ -453,7 +566,7 @@ void Runtime::handle_process(Process p) {
for (auto i : classes_found) {
std::string class_code;
i->class_to_str(class_code);
std::cout << class_code << std::endl;
std::cout << class_code << ": " << i->get_student_count() << " students." << std::endl;
}
return;
}
Expand Down
Loading

0 comments on commit 2191697

Please sign in to comment.