├── .gitignore ├── README.md ├── item.cpp ├── item.h ├── main.cpp ├── todo_list.cpp └── todo_list.h /.gitignore: -------------------------------------------------------------------------------- 1 | todo 2 | list.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TODO C++ 2 | Small TODO list C++ app 3 | 4 | # Classes 5 | ## TodoList 6 | Contains items and has various manipulation methods 7 | 8 | ## Item 9 | Has information about a specific task 10 | 11 | # Libraries 12 | ## iostream 13 | Input and output from the user 14 | ## iomanip 15 | Nice display 16 | ## string 17 | Formating user input 18 | ## fstream 19 | Manipulation of list.txt file 20 | ## stdio 21 | Ability to remove list.txt file 22 | -------------------------------------------------------------------------------- /item.cpp: -------------------------------------------------------------------------------- 1 | #include "item.h" 2 | 3 | Item::Item() 4 | { 5 | _text = ""; 6 | _done = false; 7 | } 8 | 9 | Item::Item(string item) 10 | { 11 | int startf = item.find("false"); 12 | int startt = item.find("true"); 13 | string str; 14 | 15 | if (startf > -1 && startt == -1) // item has false 16 | { 17 | str = item.substr(0, startf - 1); 18 | _done = false; 19 | } else if (startt > -1 && startf == -1) { // item has true 20 | str = item.substr(0, startt - 1); 21 | _done = true; 22 | } else if (startf == -1 && startt == -1) { // item is user input (no true or false) 23 | str = item; 24 | _done = false; 25 | } 26 | 27 | _text = str; 28 | } 29 | 30 | Item::~Item() 31 | { 32 | // do nothing 33 | } 34 | 35 | 36 | string Item::text() 37 | { 38 | return _text; 39 | } 40 | 41 | void Item::done() 42 | { 43 | _done = true; 44 | } 45 | 46 | bool Item::is_done() 47 | { 48 | return _done; 49 | } 50 | 51 | string Item::str_tolower(string str) 52 | { 53 | string temp = str; 54 | for (int i = 0; i < temp.size(); ++i) 55 | temp[i] = tolower(temp[i]); 56 | return temp; 57 | } 58 | 59 | bool Item::operator==(Item& other) 60 | { 61 | bool text_equal = str_tolower(_text) == str_tolower(other.text()); 62 | bool done_equal = _done == other.is_done(); 63 | return text_equal && done_equal; 64 | } 65 | -------------------------------------------------------------------------------- /item.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Item 7 | { 8 | public: 9 | Item(); 10 | Item(string); 11 | ~Item(); 12 | 13 | string text(); 14 | void done(); 15 | bool is_done(); 16 | 17 | bool operator==(Item&); 18 | 19 | private: 20 | string _text; 21 | bool _done; 22 | 23 | private: 24 | string str_tolower(string); 25 | }; 26 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "todo_list.h" 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | TodoList tasks("list.txt"); 9 | 10 | while(true) 11 | { 12 | tasks.read(); 13 | 14 | if (tasks.get_count() > 0) 15 | { 16 | tasks.display(); 17 | cout << endl; 18 | 19 | cout << "1) Add" << endl; 20 | cout << "2) Clear" << endl; 21 | cout << "3) Check" << endl; 22 | cout << "Choice: "; 23 | 24 | string choice; 25 | getline(cin, choice); 26 | 27 | if (choice == "1") 28 | { 29 | tasks.add(); 30 | } else if (choice == "2") { 31 | tasks.clear(); 32 | } else if (choice == "3") { 33 | tasks.check(); 34 | } else { 35 | break; 36 | } 37 | 38 | } else { 39 | cout << "Created a new list .." << endl; 40 | tasks.create(); 41 | 42 | cout << endl; 43 | tasks.display(); 44 | 45 | tasks.save(); 46 | } 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /todo_list.cpp: -------------------------------------------------------------------------------- 1 | #include "todo_list.h" 2 | 3 | TodoList::TodoList() 4 | { 5 | filename = nullptr; 6 | } 7 | 8 | TodoList::TodoList(const char* filename) 9 | { 10 | this->filename = filename; 11 | } 12 | 13 | TodoList::~TodoList() 14 | { 15 | // do nothing 16 | } 17 | 18 | void TodoList::read() 19 | { 20 | fstream fs(filename, fstream::in); 21 | string line; 22 | 23 | list.clear(); 24 | 25 | while(getline(fs, line)) 26 | { 27 | if (line == "") continue; 28 | Item item(line); 29 | list.push_back(item); 30 | } 31 | 32 | fs.close(); 33 | } 34 | 35 | void TodoList::display() 36 | { 37 | cout << "Your todo list: " << endl << endl; 38 | 39 | const int W = 40; 40 | cout << " " << setw(W) << left << "TASK" << "DONE" << endl; 41 | cout << " " << setw(W) << left << "----" << "----" << endl; 42 | for (int i = 0; i < list.size(); ++i) 43 | cout << i + 1 << ") " << setw(W) << left << list[i].text() << (list[i].is_done() ? "Done" : "" ) << endl; 44 | } 45 | 46 | void TodoList::create() 47 | { 48 | bool is_finished = false; 49 | int count = 1; 50 | string task; 51 | 52 | list.clear(); 53 | 54 | while(!is_finished) 55 | { 56 | cout << count << ": "; 57 | 58 | getline(cin, task); 59 | if (task == "") is_finished = true; 60 | 61 | Item item(task); 62 | list.push_back(item); 63 | count++; 64 | } 65 | } 66 | 67 | void TodoList::save() 68 | { 69 | fstream fs(filename, fstream::out); 70 | 71 | for (Item item : list) 72 | { 73 | if (item.text().empty()) continue; 74 | fs << item.text() << " " << (item.is_done() ? "true" : "false") << endl; 75 | } 76 | 77 | fs.close(); 78 | } 79 | 80 | int TodoList::get_count() 81 | { 82 | return list.size(); 83 | } 84 | 85 | void TodoList::add() 86 | { 87 | int index = list.size(); 88 | while (true) 89 | { 90 | cout << ++index << ": "; 91 | 92 | string task; 93 | getline(cin, task); 94 | if (task == "") break; 95 | 96 | Item item(task); 97 | list.push_back(item); 98 | } 99 | 100 | save(); 101 | } 102 | 103 | void TodoList::clear() 104 | { 105 | list.clear(); 106 | remove(filename); 107 | } 108 | 109 | void TodoList::check() 110 | { 111 | cout << "Enter number of task: "; 112 | 113 | string choice; 114 | getline(cin, choice); 115 | 116 | if (choice.empty()) return; 117 | for (char c : choice) if (isalpha(c)) return; 118 | 119 | int index = stoi(choice) - 1; 120 | if (index > list.size()) return; 121 | list[index].done(); 122 | 123 | save(); 124 | } -------------------------------------------------------------------------------- /todo_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "item.h" 8 | 9 | using namespace std; 10 | 11 | class TodoList 12 | { 13 | public: 14 | TodoList(); 15 | TodoList(const char*); 16 | ~TodoList(); 17 | 18 | void read(); 19 | void display(); 20 | void create(); 21 | void save(); 22 | void add(); 23 | void clear(); 24 | void check(); 25 | 26 | int get_count(); 27 | 28 | private: 29 | const char* filename; 30 | vector list; 31 | }; --------------------------------------------------------------------------------