-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteManager.cpp
More file actions
146 lines (131 loc) · 4.63 KB
/
Copy pathNoteManager.cpp
File metadata and controls
146 lines (131 loc) · 4.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "NoteManager.hpp"
#include "SimpleNote.hpp"
#include "ToDoNote.hpp"
#include "ProjectNote.hpp"
#include "DiaryNote.hpp"
#include "PinNote.hpp"
#include <iostream>
#include <filesystem>
#include <fstream>
#include <ctime>
using namespace std;
namespace fs = filesystem;
NoteManager::NoteManager(const string& folder) : userFolder(folder) {
loadNotes();
}
void NoteManager::loadNotes() {
for (const auto& entry : fs::directory_iterator(userFolder)) {
ifstream file(entry.path());
if (!file.is_open()) {
cerr << "Failed to open file: " << entry.path() << endl;
continue;
}
string type;
getline(file, type); // First line contains type
shared_ptr<Note> note;
if (type == "SimpleNote") note = make_shared<SimpleNote>();
else if (type == "ToDoNote") note = make_shared<ToDoNote>();
else if (type == "ProjectNote") note = make_shared<ProjectNote>();
else if (type == "DiaryNote") note = make_shared<DiaryNote>();
else if (type == "PinNote") note = make_shared<PinNote>();
else {
cerr << "Unknown note type in file: " << entry.path() << endl;
continue;
}
note->loadFromFile(file); // Populate note content
notes.push_back(note);
}
}
void NoteManager::addNote() {
cout << "----------------------------------------------\n";
cout << "Select note type:\n"
<< "----------------------------------------------\n"
<< "----------------------------------------------\n"
<< "1. Simple Note\n"
<< "----------------------------------------------\n"
<< "2. To-Do List\n"
<< "----------------------------------------------\n"
<< "3. Project\n"
<< "----------------------------------------------\n"
<< "4. Dear Diary\n"
<< "----------------------------------------------\n"
<< "5. Pins\n"
<< "----------------------------------------------\n"
<< "Choice: ";
int choice;
cin >> choice;
cin.ignore();
auto note = createNoteByType(choice);
if (note) {
note->create();
// Generate filename with timestamp
time_t t = time(nullptr);
string filename = note->getType() + "_" + to_string(t) + ".txt";
note->saveToFile(userFolder, filename);
notes.push_back(note);
cout << "----------------------------------------------\n";
cout << "Note saved successfully.\n";
}
}
void NoteManager::viewNotes() {
cout << "----------------------------------------------\n";
cout << "Your Notes:\n";
for (size_t i = 0; i < notes.size(); ++i) {
cout << i + 1 << ". ";
notes[i]->display();
}
}
void NoteManager::markFavorite() {
cout << "----------------------------------------------\n";
cout << "Enter note number to mark as favorite: ";
int num;
cin >> num;
cin.ignore();
if (num > 0 && num <= notes.size()) {
favorites[to_string(num)] = true;
cout << "----------------------------------------------\n";
cout << "Note marked as favorite.\n";
}
}
void NoteManager::viewFavorites() {
cout << "----------------------------------------------\n";
cout << "Favorite Notes:\n";
for (const auto& [key, fav] : favorites) {
if (fav) {
int idx = stoi(key) - 1;
if (idx >= 0 && idx < notes.size()) {
notes[idx]->display();
}
}
}
}
void NoteManager::viewToDoLists() {
cout << "----------------------------------------------\n";
cout << "Your To-Do Lists:\n";
for (const auto& note : notes) {
if (note->getType() == "ToDoNote") {
note->display();
}
}
}
void NoteManager::viewPins() {
cout << "----------------------------------------------\n";
cout << "Your Pins:\n";
for (const auto& note : notes) {
if (note->getType() == "PinNote") {
note->display();
}
}
}
shared_ptr<Note> NoteManager::createNoteByType(int type) {
// Create the appropriate note type based on user input
switch (type) {
case 1: return make_shared<SimpleNote>();
case 2: return make_shared<ToDoNote>();
case 3: return make_shared<ProjectNote>();
case 4: return make_shared<DiaryNote>();
case 5: return make_shared<PinNote>();
cout << "----------------------------------------------\n";
default: cout << "Invalid type.\n"; return nullptr;
}
}