-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectory.cpp
More file actions
143 lines (123 loc) · 5.14 KB
/
Directory.cpp
File metadata and controls
143 lines (123 loc) · 5.14 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
#include "Directory.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <filesystem>
#include <string_view>
#include "UIUtils.h"
using namespace std;
bool Directory::idExists(string_view id) const noexcept {
return any_of(students.begin(), students.end(), [id](const Student& s) {
return s.getId() == id;
});
}
void Directory::insertStudent(Student s) {
if (idExists(s.getId())) {
UI::printError("Student with ID " + string(s.getId()) + " already exists!");
return;
}
students.push_back(move(s));
UI::printSuccess("Student added successfully.");
}
void Directory::updateStudent(string_view id) {
auto it = find_if(students.begin(), students.end(), [id](const Student& s) {
return s.getId() == id;
});
if (it != students.end()) {
string temp;
cout << " Enter new address (leave blank to keep current): ";
getline(cin, temp);
if (!temp.empty()) it->setAddress(temp);
cout << " Enter new phone number (leave blank to keep current): ";
getline(cin, temp);
if (!temp.empty()) it->setPhone(temp);
UI::printSuccess("Record updated.");
} else {
UI::printError("Student with ID " + string(id) + " not found.");
}
}
void Directory::deleteStudent(string_view id) {
auto it = remove_if(students.begin(), students.end(), [id](const Student& s) {
return s.getId() == id;
});
if (it != students.end()) {
students.erase(it, students.end());
UI::printSuccess("Record deleted.");
} else {
UI::printError("Student with ID " + string(id) + " not found.");
}
}
void Directory::searchStudent(string_view query) const {
bool found = false;
cout << UI::CYAN << "\n [ Search Results for: " << string(query) << " ]" << UI::RESET << endl;
for (const auto& s : students) {
if (s.getName().find(query) != string_view::npos || s.getId() == query) {
cout << UI::CYAN << " ┌──────────────────────────────────┐" << endl;
cout << " │ " << UI::BOLD << "Name: " << UI::RESET << setw(24) << string(s.getName()) << UI::CYAN << " │" << endl;
cout << " │ " << UI::BOLD << "ID: " << UI::RESET << setw(24) << string(s.getId()) << UI::CYAN << " │" << endl;
cout << " │ " << UI::BOLD << "Phone: " << UI::RESET << setw(24) << string(s.getPhone()) << UI::CYAN << " │" << endl;
cout << " └──────────────────────────────────┘" << UI::RESET << endl;
found = true;
}
}
if (!found) UI::printError("No matching records found.");
}
void Directory::listDirectory() const {
if (students.empty()) {
UI::printError("Registry is empty.");
return;
}
cout << "\n" << UI::CYAN << UI::BOLD << " " << left << setw(10) << "ID" << setw(20) << "Name" << setw(15) << "Phone" << "Address" << UI::RESET << endl;
cout << UI::CYAN << " " << string(65, '-') << UI::RESET << endl;
for (const auto& s : students) {
cout << " " << left << setw(10) << string(s.getId()) << setw(20) << string(s.getName())
<< setw(15) << string(s.getPhone()) << string(s.getAddress()) << endl;
}
cout << UI::CYAN << " " << string(65, '-') << UI::RESET << endl;
cout << UI::MAGENTA << " Total Records: " << students.size() << UI::RESET << endl;
}
void Directory::sortByName() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.getName() < b.getName();
});
UI::printSuccess("Registry sorted by Name.");
}
void Directory::sortByID() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.getId() < b.getId();
});
UI::printSuccess("Registry sorted by ID.");
}
void Directory::saveToFile(string_view filename) const {
ofstream file((string(filename)));
if (!file) {
UI::printError("Error opening file for saving.");
return;
}
for (const auto& s : students) {
file << string(s.getName()) << "|" << string(s.getId()) << "|" << string(s.getAddress()) << "|" << string(s.getPhone()) << "\n";
}
UI::printSuccess("Saved " + to_string(students.size()) + " records to " + string(filename));
}
void Directory::loadFromFile(string_view filename) {
if (!filesystem::exists(filename)) return;
ifstream file((string(filename)));
students.clear();
string line;
while (getline(file, line)) {
if (line.empty()) continue;
size_t p1 = line.find('|');
size_t p2 = line.find('|', p1 + 1);
size_t p3 = line.find('|', p2 + 1);
if (p1 != string::npos && p2 != string::npos && p3 != string::npos) {
students.emplace_back(
line.substr(0, p1),
line.substr(p1 + 1, p2 - p1 - 1),
line.substr(p2 + 1, p3 - p2 - 1),
line.substr(p3 + 1)
);
}
}
UI::printSuccess("Loaded " + to_string(students.size()) + " records from " + string(filename));
}