-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtodo_list.cpp
More file actions
124 lines (96 loc) · 1.83 KB
/
todo_list.cpp
File metadata and controls
124 lines (96 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "todo_list.h"
TodoList::TodoList()
{
filename = nullptr;
}
TodoList::TodoList(const char* filename)
{
this->filename = filename;
}
TodoList::~TodoList()
{
// do nothing
}
void TodoList::read()
{
fstream fs(filename, fstream::in);
string line;
list.clear();
while(getline(fs, line))
{
if (line == "") continue;
Item item(line);
list.push_back(item);
}
fs.close();
}
void TodoList::display()
{
cout << "Your todo list: " << endl << endl;
const int W = 40;
cout << " " << setw(W) << left << "TASK" << "DONE" << endl;
cout << " " << setw(W) << left << "----" << "----" << endl;
for (int i = 0; i < list.size(); ++i)
cout << i + 1 << ") " << setw(W) << left << list[i].text() << (list[i].is_done() ? "Done" : "" ) << endl;
}
void TodoList::create()
{
bool is_finished = false;
int count = 1;
string task;
list.clear();
while(!is_finished)
{
cout << count << ": ";
getline(cin, task);
if (task == "") is_finished = true;
Item item(task);
list.push_back(item);
count++;
}
}
void TodoList::save()
{
fstream fs(filename, fstream::out);
for (Item item : list)
{
if (item.text().empty()) continue;
fs << item.text() << " " << (item.is_done() ? "true" : "false") << endl;
}
fs.close();
}
int TodoList::get_count()
{
return list.size();
}
void TodoList::add()
{
int index = list.size();
while (true)
{
cout << ++index << ": ";
string task;
getline(cin, task);
if (task == "") break;
Item item(task);
list.push_back(item);
}
save();
}
void TodoList::clear()
{
list.clear();
remove(filename);
}
void TodoList::check()
{
cout << "Enter number of task: ";
string choice;
getline(cin, choice);
if (choice.empty()) return;
for (char c : choice) if (isalpha(c)) return;
int index = stoi(choice) - 1;
if (index > list.size()) return;
list[index].done();
save();
}