Added pointers to active project/task and void to set active task

This commit is contained in:
Patrick Lipka 2021-12-09 13:23:30 +01:00
parent d15b8a57e4
commit 8b4b0c3432
3 changed files with 47 additions and 19 deletions

View File

@ -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; i<num_projects; i++){
size_t len = projects[i].name.size();
of.write((char*) &len, sizeof(size_t));
of.write((char*) projects[i].name.c_str(),len);
of.write((char*) &(projects[i].num_tasks), sizeof(int));
of.write((char*) &(projects[i].active_task), sizeof(int));
of.write((char*) &(projects[i].active_task_id), sizeof(int));
for (int j=0; j<projects[i].num_tasks; j++){
size_t task_name_len = projects[i].tasks[j].name.size();
of.write((char*) &task_name_len, sizeof(size_t));
@ -68,6 +79,7 @@ void ProjectList::load(std::string file_name){
inf.read((char*) &num_projects,sizeof(int));
inf.read((char*) &active, sizeof(int));
int np = num_projects;
int *active_task_arr = new int[np];
for (int i=0; i<np;i++){
size_t len;
std::string proj_name;
@ -97,9 +109,18 @@ void ProjectList::load(std::string file_name){
task.add_time(work_time);
proj.add_task(task);
}
proj.active_task = active_t;
add_project(proj);
//proj.active_task_id = active_t;
//proj.set_active_task(active_t);
//proj.set_active_task(active_t);
active_task_arr[i] = active_t;
add_project(proj);
//active_project->set_active_task(active_t);
}
active_project = active;
//active_project_id = active;
set_active_project(active);
for (int i=0; i<np ; i++){
projects[i].set_active_task(active_task_arr[i]);
}
delete [] active_task_arr;
inf.close();
}

View File

@ -13,7 +13,9 @@ class Project{
std::vector<Task> 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<Project> 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);
};

View File

@ -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;
}