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

Commit

Permalink
Runtime: add is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme-ds-matos committed Nov 2, 2023
1 parent 614f0c1 commit 5abee22
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
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
44 changes: 44 additions & 0 deletions src/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,50 @@ void Runtime::handle_process(Process p) {
return;
}

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;
for (auto sched : this->classes) {
if (sched.get_id() == id) {
target = &sched;
}
}
if (auto itr = students.find(Student(student_code, "")); itr != students.end()) {
Student s = *itr;
switch (s.verify_add(target)) {
case OperationResult::Success:
students.erase(s);
s.add_to_class(target);
students.insert(s);
history.push(p);
break;
case OperationResult::Error:
std::cerr << "ERROR: Critical conflicts found: the schedule is not valid. Skipping." << std::endl;
break;
case 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;
if (answer == "y") {
students.erase(s);
s.add_to_class(target);
students.insert(s);
history.push(p);
}
break;
}
}

}

// handle print student
if (p.get_type() == TypeOfRequest::Print_Student) {
uint32_t student_code;
Expand Down

0 comments on commit 5abee22

Please sign in to comment.