epr24pr5-ojanssen2/Task.cpp

113 lines
2.7 KiB
C++

#include "Task.h"
#include "Util.h"
#include "Manager.h"
#include "Error.h"
using err::Error;
using util::Util;
using util::Manager;
namespace models
{
Task::Task(const int& id, const string& name, const string& description, const vector<int>& children):
id(id), name(name), description(description), children(children)
{
}
Task::Task(): id(0), name(""), description(""), children({})
{
}
ostream& Task::write(ostream& stream) const
{
stream << *this;
return stream;
}
int Task::get_id() const
{
return this->id;
}
string Task::get_name() const
{
return this->name;
}
string Task::get_description() const
{
return this->description;
}
vector<int> Task::get_children() const
{
return this->children;
}
AddTask::AddTask(): Task()
{
}
AddTask::AddTask(Task& t): Task(t)
{
}
ostream& operator<<(ostream& os, const Task& t)
{
os << t.get_id() << " %" << t.get_name() << "% %" << t.get_description();
for (const int id : t.children)
os << " " << id << " ";
os << "\n";
return os;
}
istream& operator>>(istream& is, Task& t)
{
int id;
is >> id;
if (!is)
throw Error(602, "Datei hat ein unbekanntes Format.");
try
{
string name = Util::read_string_between_percent(is);
string description = Util::read_string_between_percent(is);
vector<int> children = Util::read_numbers(is);
t = {id, name, description, children};
return is;
}
catch (Error& _)
{
throw Error(602, "Datei hat ein unbekanntes Format.");
}
}
ostream& operator<<(ostream& os, const AddTask& t)
{
os << t.get_name() << " " << t.get_description() << "\n";
return os;
}
istream& operator>>(istream& is, AddTask& t)
{
Manager& mgr = Manager::get_instance();
int id = mgr.get_task_id();
try
{
const string name = Util::read_string_between_percent(is);
if (name == "")
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
const string description = Util::read_string_between_percent(is);
const vector<int> children = Util::read_numbers(is);
Task ft = {id, name, description, children};
t = {ft};
return is;
}
catch (Error& _)
{
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
}
}
} // models