diff --git a/ActiveCommand.cpp b/ActiveCommand.cpp index d87042c..b413684 100644 --- a/ActiveCommand.cpp +++ b/ActiveCommand.cpp @@ -12,7 +12,7 @@ namespace commands { } - void ActiveCommand::run(int argc, stringstream& args) + void ActiveCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); int user_id; diff --git a/ActiveCommand.h b/ActiveCommand.h index 88d3865..1b17d96 100644 --- a/ActiveCommand.h +++ b/ActiveCommand.h @@ -8,7 +8,7 @@ namespace commands { class ActiveCommand : public SubCommand { public: ActiveCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/AddCommand.cpp b/AddCommand.cpp index bcb0cec..2206b33 100644 --- a/AddCommand.cpp +++ b/AddCommand.cpp @@ -14,7 +14,7 @@ namespace commands { } - void AddCommand::run(int argc, stringstream& args) + void AddCommand::run(stringstream& args) { const string data_type = Util::read_string(args); Manager& mgr = Manager::get_instance(); diff --git a/AddCommand.h b/AddCommand.h index dd3730c..160a427 100644 --- a/AddCommand.h +++ b/AddCommand.h @@ -8,7 +8,7 @@ namespace commands { class AddCommand: public SubCommand { public: AddCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } #endif // ADDCOMMAND_H diff --git a/AssignCommand.cpp b/AssignCommand.cpp index 01c6bc7..edc211f 100644 --- a/AssignCommand.cpp +++ b/AssignCommand.cpp @@ -12,7 +12,7 @@ namespace commands { } - void AssignCommand::run(int argc, stringstream& args) + void AssignCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); int user_id, task_id; diff --git a/AssignCommand.h b/AssignCommand.h index e179e31..409f2a3 100644 --- a/AssignCommand.h +++ b/AssignCommand.h @@ -8,7 +8,7 @@ namespace commands { class AssignCommand : public SubCommand { public: AssignCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/DataType.h b/DataType.h index 54dcc1d..21b4e99 100644 --- a/DataType.h +++ b/DataType.h @@ -8,7 +8,7 @@ namespace util { INIT, USER, TASK, - USERTASKINDEX + ASSIGNMENT }; } // util diff --git a/DelCommand.cpp b/DelCommand.cpp index 5f315d0..9b5ca62 100644 --- a/DelCommand.cpp +++ b/DelCommand.cpp @@ -12,7 +12,7 @@ namespace commands { } - void DelCommand::run(int argc, stringstream& args) + void DelCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); const string data_type = Util::read_string(args); diff --git a/DelCommand.h b/DelCommand.h index a0d889c..a2c4b65 100644 --- a/DelCommand.h +++ b/DelCommand.h @@ -8,7 +8,7 @@ namespace commands { class DelCommand : public SubCommand { public: DelCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/HelpCommand.cpp b/HelpCommand.cpp index 9148a24..d1a1fda 100644 --- a/HelpCommand.cpp +++ b/HelpCommand.cpp @@ -3,7 +3,7 @@ namespace commands { HelpCommand::HelpCommand(): SubCommand("help", 100, false) {} - void HelpCommand::run(int argc, stringstream& args) { + void HelpCommand::run(stringstream& args) { cout << "Projekt 5\n"; cout << "epr24pr5-ojanssen2 \n"; } diff --git a/HelpCommand.h b/HelpCommand.h index 6c56847..d09bdbb 100644 --- a/HelpCommand.h +++ b/HelpCommand.h @@ -8,7 +8,7 @@ namespace commands { class HelpCommand: public SubCommand { public: HelpCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands #endif // HELPCOMMAND_H diff --git a/ListCommand.cpp b/ListCommand.cpp index b74a50c..d9ff660 100644 --- a/ListCommand.cpp +++ b/ListCommand.cpp @@ -15,18 +15,18 @@ namespace commands { } - void ListCommand::run(int argc, stringstream& args) + void ListCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); const string data_type = Util::read_string(args); if (data_type == "user") - for (User* u : mgr.get_users()) + for (const User* u : mgr.get_users()) cout << *u; else if (data_type == "task") - for (Task* t : mgr.get_tasks()) + for (const Task* t : mgr.get_tasks()) cout << *t; else if (data_type == "assignment") - for (Assignment* a : mgr.get_assignments()) + for (const Assignment* a : mgr.get_assignments()) cout << *a; else throw Error(101, "Befehl ist unbekannt."); diff --git a/ListCommand.h b/ListCommand.h index 393ce9a..5a683be 100644 --- a/ListCommand.h +++ b/ListCommand.h @@ -8,7 +8,7 @@ namespace commands { class ListCommand : public SubCommand{ public: ListCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/Manager.cpp b/Manager.cpp index 88094bd..b107a92 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -36,7 +36,7 @@ namespace util else if (line == "[users]") current_type = DataType::USER; else if (line == "[assignments]") - current_type = DataType::USERTASKINDEX; + current_type = DataType::ASSIGNMENT; else throw Error(602, "Datei hat ein unbekanntes Format."); continue; @@ -50,35 +50,37 @@ namespace util in.close(); // Reihenfolge der Verabeitung - vector sections = {DataType::USER, DataType::TASK, DataType::USERTASKINDEX}; + vector sections = { DataType::USER, DataType::TASK, DataType::ASSIGNMENT }; for (DataType type : sections) { // Keine Data -> Continue - if (buffer.find(type) == buffer.end()) continue; + if (buffer.find(type) == buffer.end()) + continue; const vector& lines = buffer[type]; //Iteriere über die Gebufferten Strings. - for (const string& bufferd_line : lines) + for (const string& buffered_line : lines) { + stringstream string_stream(buffered_line); switch (type) { case DataType::TASK: { Task task; - in >> task; + string_stream >> task; this->tasks[task.get_id()] = new Task(task); } break; case DataType::USER: { User user; - in >> user; + string_stream >> user; this->users[user.get_id()] = new User(user); } break; - case DataType::USERTASKINDEX: + case DataType::ASSIGNMENT: { Assignment assignment; - in >> assignment; + string_stream >> assignment; this->assignments.push_back(new Assignment(assignment)); } break; @@ -136,52 +138,141 @@ namespace util INSTANCE = new Manager(); return *INSTANCE; } - vector const Manager::get_users() { + + vector Manager::get_users() const + { vector user_vector; - for (const auto& pair : this->users) { - if (pair.second) { // Ensure it's not a nullptr + for (const auto& pair : this->users) + if (pair.second) // Ensure it's not a nullptr user_vector.push_back(pair.second); + return user_vector; + } + + vector Manager::get_tasks() const + { + vector task_vector; + for (const auto& pair : this->tasks) + if (pair.second) // Ensure it's not a nullptr + task_vector.push_back(pair.second); + return task_vector; + } + + vector Manager::get_assignments() + { + return this->assignments; + } + + User* Manager::get_user(const int& id) + { + if (this->users.count(id) == 0) + return nullptr; + return this->users[id]; + } + + + Task* Manager::get_task(const int& id) + { + if (this->tasks.count(id) == 0) + return nullptr; + return this->tasks[id]; + } + + Vector Manager::get_assignments_for_user(const int& id) + { + Vector user_assignments; + if (this->get_user(id) == nullptr) + throw Error(401, "Eine solche BenutzerIn existiert nicht."); + for (Assignment* as : this->assignments) + if (as->get_user_id() == id) + user_assignments.push_back(as); + return user_assignments; + } + + bool Manager::assignment_exists(const int& user_id, const int& task_id) + { + for ( Assignment* as : this->assignments) + if (as->get_user_id() == user_id && as->get_task_id() == task_id) + return true; + return false; + } + + void Manager::add_user(User* user) + { + if (this->users.count(user->get_id()) == 1) + return; + this->users[user->get_id()] = user; + } + + + void Manager::add_task(Task* task) + { + if (this->tasks.count(task->get_id()) == 1) + return; + this->tasks[task->get_id()] = task; + } + + void Manager::add_assignment(Assignment* as) + { + if (this->get_user(as->get_user_id()) == nullptr) + throw Error(401, "Eine solche BenutzerIn existiert nicht."); + + if (this->get_task(as->get_task_id()) == nullptr) + throw Error(402, "Eine solche Aufgabe existiert nicht."); + + this->assignments.push_back(as); + } + + void Manager::del_user(const int& id) + { + if (this->users.count(id) == 0) + throw Error(401, "Eine solche BenutzerIn existiert nicht."); + + this->users.erase(id); + } + + void Manager::del_task(const int& id) + { + if (this->tasks.count(id) == 0) + throw Error(402, "Eine solche Aufgabe existiert nicht."); + + this->tasks.erase(id); + } + + void Manager::del_assignment(Assignment& as) + { + if (!this->assignment_exists(as.get_user_id(), as.get_task_id())) + throw Error(301, "Eine solche Zuordnung existiert nicht."); + int assignment_nr = 0; + for (int i = 0; i < this->assignments.size() - 1; ++i) { + if (this->assignments[i]->get_user_id() == as.get_user_id() && this->assignments[i]->get_task_id() == as.get_task_id()) { + assignment_nr = i; + break; } } - return user_vector; - + this->assignments.erase(this->assignments.begin() + assignment_nr); }; - vector get_tasks(); - vector get_assignments(); + void Manager::save() { ofstream out = ofstream(this->filename, ios_base::trunc); if (!out) throw Error(603, "Datei kann nicht geschrieben werden."); + out << "[tasks]\n"; - for (const auto& task_pair : this->users) - { - //Nullpointer Check + for (const auto& task_pair : this->tasks) if (task_pair.second) - { task_pair.second->write(out); - } - } + out << "\n[users]\n"; for (const auto& user_pair : this->users) - { - //Nullpointer Check if (user_pair.second) - { user_pair.second->write(out); - } - } out << "\n[assignments]\n"; for (Assignment* assignment : this->assignments) - { - //Nullpointer Check if (assignment) - { assignment->write(out); - } - } + out.close(); } - } // util diff --git a/Manager.h b/Manager.h index 797431c..6fc23be 100644 --- a/Manager.h +++ b/Manager.h @@ -37,8 +37,8 @@ namespace util int get_user_id(); int get_task_id(); - vector get_users(); - vector get_tasks(); + vector get_users() const; + vector get_tasks() const; vector get_assignments(); User* get_user(const int& id); @@ -46,13 +46,13 @@ namespace util vector get_assignments_for_user(const int& id); bool assignment_exists(const int& user_id, const int& task_id); - void add_user(const User* user); - void add_task(const Task* task); - void add_assignment(const Assignment* as); + void add_user(User* user); + void add_task(Task* task); + void add_assignment(Assignment* as); void del_user(const int& id); void del_task(const int& id); - void del_assignment(const Assignment& as); + void del_assignment(Assignment& as); }; } // util #endif // ENVIRONMENT_H diff --git a/ShowCommand.cpp b/ShowCommand.cpp index 223e68f..646bc6e 100644 --- a/ShowCommand.cpp +++ b/ShowCommand.cpp @@ -16,7 +16,7 @@ namespace commands { } - void ShowCommand::run(int argc, stringstream& args) + void ShowCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); const string data_type = Util::read_string(args); diff --git a/ShowCommand.h b/ShowCommand.h index 5b02372..91fab80 100644 --- a/ShowCommand.h +++ b/ShowCommand.h @@ -8,7 +8,7 @@ namespace commands { class ShowCommand : public SubCommand { public: ShowCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/SubCommand.cpp b/SubCommand.cpp index 3d47485..be59e54 100644 --- a/SubCommand.cpp +++ b/SubCommand.cpp @@ -4,7 +4,7 @@ namespace commands { SubCommand::SubCommand(const string& name, const int& return_value, const bool show_result): name(name), return_value(return_value), show_result(show_result) {} - void SubCommand::run(int argc, stringstream& args) { + void SubCommand::run(stringstream& args) { cout << "Not impl!\n"; } diff --git a/SubCommand.h b/SubCommand.h index d1f20b9..b68b5cd 100644 --- a/SubCommand.h +++ b/SubCommand.h @@ -19,7 +19,7 @@ namespace commands string get_name(); int get_value() const; bool should_display_result() const; - virtual void run(int argc, stringstream& args); + virtual void run(stringstream& args); }; } // commands diff --git a/UnassignCommand.cpp b/UnassignCommand.cpp index 38364f1..1d315d8 100644 --- a/UnassignCommand.cpp +++ b/UnassignCommand.cpp @@ -11,7 +11,7 @@ namespace commands { } - void UnassignCommand::run(int argc, stringstream& args) + void UnassignCommand::run(stringstream& args) { Manager& mgr = Manager::get_instance(); int user_id, task_id; @@ -26,7 +26,8 @@ namespace commands throw Error(401, "Eine solche Zuordnung existiert nicht."); if (!mgr.assignment_exists(user_id, task_id)) throw Error(302, "Eine solche Zuordnung existiert bereits."); - mgr.del_assignment({user_id, task_id}); + Assignment as = {user_id, task_id}; + mgr.del_assignment(as); mgr.save(); } } // commands diff --git a/UnassignCommand.h b/UnassignCommand.h index 13abdb0..bc16bc6 100644 --- a/UnassignCommand.h +++ b/UnassignCommand.h @@ -8,7 +8,7 @@ namespace commands { class UnassignCommand : public SubCommand { public: UnassignCommand(); - void run(int argc, stringstream& args) override; + void run(stringstream& args) override; }; } // commands diff --git a/main.cpp b/main.cpp index 70ad143..d44c21f 100644 --- a/main.cpp +++ b/main.cpp @@ -48,15 +48,18 @@ int main(int argc, char** argv) { if (sc->get_name() == command) { - sc->run(argc, parameter); - return 0; + sc->run(parameter); + if (sc->should_display_result()) + cout << "100: Alles erfolgreich\n"; + return sc->get_value(); } } } catch (Error& e) { cout << e.get_nr() << ": " << e.what() << "\n"; + return e.get_nr(); } cout << "101: Befehl ist unbekannt.\n"; - return 0; + return 101; }