From 6b9f45f616950a573e732c9ccff9e97950f7772d Mon Sep 17 00:00:00 2001 From: DuarteSAssuncao Date: Thu, 2 Nov 2023 19:07:50 +0000 Subject: [PATCH] Undo --- src/Process.hpp | 2 ++ src/Runtime.cpp | 87 ++++++++++++++++++++++++++++++++++++------------- src/Student.hpp | 2 +- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/Process.hpp b/src/Process.hpp index 5e79e33..c144da2 100644 --- a/src/Process.hpp +++ b/src/Process.hpp @@ -15,6 +15,8 @@ enum class TypeOfRequest { Print_Student_List, Batch, Save, + Undo, + PopHistory, }; /** diff --git a/src/Runtime.cpp b/src/Runtime.cpp index 8c1735f..c7d409e 100644 --- a/src/Runtime.cpp +++ b/src/Runtime.cpp @@ -295,36 +295,42 @@ void Runtime::process_args(std::vector args) { return; } + if (args[0] == "undo") { + if (args.size() != 0) { + std::cerr << "ERROR: USAGE: undo" << std::endl; + } else { + Process t(TypeOfRequest::Undo); + procs.push(t); + } + return; + } + if (args[0] == "help") { std::cout << "The available commands are:\n" - << " print student: takes 1 argument: print student " - "\n" + << " print student: takes 1 argument: print student \n" << " Prints the student name, enrolled classes and schedule.\n\n" - << " print uc: takes 1 argument: print uc \n" + << " print uc: takes 1 argument: print uc \n" << " Prints all the classes associated with this UC.\n\n" - << " print class: takes 2 argument: print class " - "\n" - << " Prints the number of students enrolled and the " - "schedule.\n\n" - << " add: takes 3 arguments: add " - " \n" + << " print class: takes 2 argument: print class \n" + << " Prints the number of students enrolled and the schedule.\n\n" + << " add: takes 3 arguments: add \n" << " Adds a student to a class if possible.\n\n" - << " remove: takes 2 arguments: remove " - " \n" + << " remove: takes 2 arguments: remove \n" << " Removes a student from a class if possible.\n\n" - << " switch: takes 3 arguments: switch " - " \n" + << " switch: takes 3 arguments: switch \n" << " Switches the class of two students.\n\n" - << " student_count: takes 0 arguments: student_count\n" + << " student_count: takes 0 arguments: student_count\n" << " Displays the number of students enrolled.\n\n" - << " student_list: takes 0 or 2 arguments: student_count [ ]\n" + << " student_list: takes 0 or 2 arguments: student_count [ ]\n" << " Displays the students enrolled with the option (denoted in []) of specifying a beginning and number of students to display.\n\n" - << " quit: takes 0 arguments: quit\n" + << " undo: takes 0 arguments: undo\n" + << " Reverts the last change.\n\n" + << " quit: takes 0 arguments: quit\n" << " Quits the program.\n\n" - << " save: takes 0 or 1 arguments: save []\n" + << " save: takes 0 or 1 arguments: save []\n" << " Saves the changes to the csv which contains the students and their relations to the classes, and optionally takes a filename.\n\n" - << " help: takes 0 arguments: help\n" + << " help: takes 0 arguments: help\n" << " Prints this help.\n\n"; return; } @@ -337,7 +343,7 @@ void Runtime::process_args(std::vector args) { void Runtime::handle_process(Process p) { std::vector ops = p.get_ops(); - // handle remove + // handle remove------------------------------------------------------------------------------------------------------ if (p.get_type() == TypeOfRequest::Remove) { uint32_t student_code; try { @@ -356,9 +362,12 @@ void Runtime::handle_process(Process p) { if (a->get_uc_code() == uc_code) { // std::cout << "Lookup: " << uc_code << "\nFound: " << // a->get_uc_code() << std::endl; + std::string class_code; + a->class_to_str(class_code); s.remove_from_class(a); students.erase(s); students.insert(s); + p.add_operand(class_code); history.push(p); return; } @@ -370,7 +379,7 @@ void Runtime::handle_process(Process p) { return; } - // handle print student + // handle print student----------------------------------------------------------------------------------------------- if (p.get_type() == TypeOfRequest::Print_Student) { uint32_t student_code; try { @@ -430,7 +439,7 @@ void Runtime::handle_process(Process p) { return; } - // handle print uc + // handle print uc---------------------------------------------------------------------------------------------------- if (p.get_type() == TypeOfRequest::Print_UC) { uint16_t uc_code = parse_uc_gen(ops[0]); std::vector classes_found = find_uc(uc_code); @@ -449,7 +458,7 @@ void Runtime::handle_process(Process p) { return; } - // handle print class + // handle print class------------------------------------------------------------------------------------------------- if (p.get_type() == TypeOfRequest::Print_Class) { uint16_t uc_code = parse_uc_gen(ops[0]); uint16_t class_code = parse_class_gen(ops[1]); @@ -468,12 +477,14 @@ void Runtime::handle_process(Process p) { return; } + // handle student_count----------------------------------------------------------------------------------------------- if (p.get_type() == TypeOfRequest::Print_Student_Count) { std::cout << "There are " << this->students.size() << " students enrolled." << std::endl; return; } + // handle student_list------------------------------------------------------------------------------------------------ if (p.get_type() == TypeOfRequest::Print_Student_List) { std::vector args = p.get_ops(); uint64_t start = 0; @@ -505,13 +516,45 @@ void Runtime::handle_process(Process p) { return; } + // handle save-------------------------------------------------------------------------------------------------------- if(p.get_type() == TypeOfRequest::Save) { if (p.get_ops().size() != 0) { this->students_classes_->set_filename(p.get_ops()[0]); } this->save_all(); this->students_classes_->write_to_file(); + return; } + + // handle undo-------------------------------------------------------------------------------------------------------- + if (p.get_type() == TypeOfRequest::Undo) { + Process to_revert = history.top(); + history.pop(); + if (to_revert.get_type() == TypeOfRequest::Remove) { + Process t = Process(TypeOfRequest::Add); + t.add_operand(to_revert.get_ops()[0]); + t.add_operand(to_revert.get_ops()[1]); + t.add_operand(to_revert.get_ops()[2]); + procs.push(t); + } + if (to_revert.get_type() == TypeOfRequest::Add) { + Process t = Process(TypeOfRequest::Remove); + t.add_operand(to_revert.get_ops()[0]); + t.add_operand(to_revert.get_ops()[1]); + procs.push(t); + } + if (to_revert.get_type() == TypeOfRequest::Switch) { + procs.push(to_revert); + } + Process pop = Process(TypeOfRequest::PopHistory); + procs.push(pop); + return; + } + + // handle pop + if (p.get_type() == TypeOfRequest::PopHistory) { + history.pop(); + } } void Runtime::print_schedule(const std::vector &schedule) const { diff --git a/src/Student.hpp b/src/Student.hpp index 5c3e2ea..e38b71d 100644 --- a/src/Student.hpp +++ b/src/Student.hpp @@ -1,4 +1,4 @@ -/** + /** * @file Student.hpp */ #ifndef STUDENT_H