From 8b4b0c343286579225a38d17fb10826814afc742 Mon Sep 17 00:00:00 2001 From: Patrick Lipka Date: Thu, 9 Dec 2021 13:23:30 +0100 Subject: [PATCH] Added pointers to active project/task and void to set active task --- src/project.cpp | 41 +++++++++++++++++++++++++++++++---------- src/project.h | 8 ++++++-- src/tt.cpp | 17 ++++++++++------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/project.cpp b/src/project.cpp index 8845e82..9c0385e 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -7,33 +7,44 @@ Project::Project(std::string name){ Project::name = name; Project::num_tasks = 0; - Project::active_task = 0; + Project::active_task_id = 0; } void Project::add_task(Task task){ tasks.push_back(task); - active_task = num_tasks; + set_active_task(num_tasks); num_tasks++; } void Project::remove_task(int id){ tasks.erase(tasks.begin()+id); - active_task = std::max(id-1,0); + set_active_task(std::max(id-1,0)); num_tasks--; } +void Project::set_active_task(int id){ + active_task_id = id; + active_task = &tasks[id]; +} + void ProjectList::add_project(Project proj){ projects.push_back(proj); - active_project = num_projects; + set_active_project(num_projects); num_projects++; } void ProjectList::remove_project(int id){ projects.erase(projects.begin()+id); - active_project = std::max(id-1,0); + set_active_project(std::max(id-1,0)); num_projects--; } +void ProjectList::set_active_project(int id){ + active_project_id = id; + active_project = &projects[id]; +} + + void ProjectList::save(std::string file_name){ std::ofstream of(file_name, std::ios::binary); if (!of){ @@ -41,13 +52,13 @@ void ProjectList::save(std::string file_name){ exit(1); } of.write((char*) &num_projects, sizeof(int)); - of.write((char*) &active_project, sizeof(int)); + of.write((char*) &active_project_id, sizeof(int)); for (int i=0; iset_active_task(active_t); } - active_project = active; + //active_project_id = active; + set_active_project(active); + for (int i=0; i tasks; void add_task(Task task); void remove_task(int id); - int active_task; + int active_task_id; + Task *active_task; + void set_active_task(int id); }; class ProjectList{ @@ -22,7 +24,9 @@ class ProjectList{ std::vector projects; void add_project(Project proj); void remove_project(int id); - int active_project; + int active_project_id; + Project *active_project; + void set_active_project(int id); void save(std::string file_name); void load(std::string file_name); }; diff --git a/src/tt.cpp b/src/tt.cpp index 67d70eb..b65ac12 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -19,10 +19,10 @@ int main(){ */ ProjectList proj_list; proj_list.add_project(proj); - std::cout << "Project List no of projects: " << proj_list.num_projects << " Name of active proj: " << proj_list.projects[proj_list.active_project].name << std::endl; + std::cout << "Project List no of projects: " << proj_list.num_projects << " Name of active proj: " << proj_list.projects[proj_list.active_project_id].name << std::endl; std::cout << "Write Project List to file" << std::endl; - std::cout << "Active Project: " << proj_list.active_project << std::endl; + std::cout << "Active Task: " << proj_list.active_project->active_task->name << std::endl; proj_list.save("test.bin"); std::cout << "Read Project List from file" << std::endl; @@ -30,10 +30,13 @@ int main(){ list2.load("test.bin"); std::cout << "Project List no of projects: " << list2.num_projects << std::endl; - std::cout << "active Project: " << list2.active_project << std::endl; - std::cout << "Name of active proj: " << list2.projects[list2.active_project].name << std::endl; - std::cout << "Name of active task: " << list2.projects[list2.active_project].tasks[list2.projects[list2.active_project].active_task].name << std::endl; - std::cout << "Work time of this task: " << list2.projects[list2.active_project].tasks[list2.projects[list2.active_project].active_task].work_time << std::endl; - + std::cout << "active Project: " << list2.active_project_id << std::endl; + std::cout << "Name of active proj: " << list2.projects[list2.active_project_id].name << std::endl; + std::cout << list2.active_project->name << std::endl; + //std::cout << "Name of active task: " << list2.projects[list2.active_project_id].tasks[list2.projects[list2.active_project_id].active_task_id].name << std::endl; + std::cout << list2.active_project->active_task->name << std::endl; + std::cout << list2.active_project->active_task->work_time << std::endl; + //std::cout << "Work time of this task: " << list2.projects[list2.active_project_id].tasks[list2.projects[list2.active_project_id].active_task_id].work_time << std::endl; + //std::cout << list2.active_project->active_task->work_time << std::endl; return 0; }